Given 'N' words / strings, sort them in dictionary order or in lexicographical order without using any library functions
Implementation:
Implementation:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* Swap function to swap the strings starting addresses of the pointer to strings*/
void swap(char **x,char **y)
{
char *temp;
temp=*x;
*x=*y;
*y=temp;
}
/* Function to compare two strings */
int compare(char *s,char *v)
{
while(*s && *s==*v)
s++,v++;
return (*s-*v);
}
int main(void) {
char *s[10];
int i,j;
for(i=0;i<10;i++)
{
/* Memory allocation for 10 strings */
*(s+i)=(char*)malloc(sizeof(char));
/* Get the input word from the user */
scanf("%s",*(s+i));
}
for(i=0;i<10;i++)
{
for(j=i+1;j<10;j++)
{
/* Sort the strings in alphabetical order */
if(compare(*(s+i),*(s+j))>0)
{
swap((s+i),(s+j));
}
}
}
/* Print the sorted strings */
for(i=0;i<10;i++)
printf("%s\n",*(s+i));
return 0;
}