Get two strings from the user and Concatenate the two strings lexically without using any library function
Implementation:
Implementation:
#include <stdio.h> #include <string.h> #include <stdlib.h> /* Swap function to swap the addresses of the pointers to the character array block */ void swap(char **x,char **y) { char *temp; temp=*x; *x=*y; *y=temp; } /* Manual Compare function for strcmp() to compare two strings */ int compare(char *s,char *v) { /* work until each character in the strings are equal and character of first string not equals, '\0' null character(end of the string) */ while(*s && (*s==*v)) s++,v++; /* If the two strings are equal , ascii difference of null character in both the strings difference is returned */ /* Else the ascii difference of the mismatched characters is returned */ return (*s-*v); } /* User-defined function for strcpy() */ /* Copy the second string from the end of the first string */ char* copy(char *x,char *y) { int len=strlen(x); while(*y) { *(x+len)=*y; y++; len++; } /* Mark the end of the string */ *(x+len)='\0'; /* Return the address of the copied string */ return x; } int main(void) { /* Character pointers to store the address of the character array blocks */ char *s,*v; /* Dynamically allocate the memory block, where each cell has a sizeof character and return the starting address of the block as character pointer */ s=(char*)malloc(sizeof(char)); v=(char*)malloc(sizeof(char)); /* Get the two strings from the user */ scanf("%s",s); scanf("%s",v); /* Compare function to compare the strings */ /* If the result of the compare function is greater than zero */ /* It means that, the first string 's' has greater string and second string 'v' has smaller string */ if(compare(s,v)>0) { /* Swap if the first string is greater than the second string */ swap(&s,&v); } /* Copy the second string to first string */ s=copy(s,v); /* Display the resultant string */ printf("%s",s); return 0; }