Header Ads Widget

program to arrange word in dictionary method in c.

here we use a function strcmp() which is present in string.h header file.It return 0 when string is match . it return negative value when string in dictonary formate otherwise return positive value


  • program

#include <stdio.h>
#include<string.h>
int main()
{
char s[5][19],str[19];
int i,j,l,r;
printf("Enter five strings\n");
for(i=0;i<=4;i++)
scanf("%s",s[i]);
for(i=1;i<=4;i++)
{
for(j=0;j<=4-i;j++)
{
r=strcmp(s[j],s[j+1]);
if(r>0)
{
strcpy(str,s[j]);
strcpy(s[j],s[j+1]);
strcpy(s[j+1],str);
}
}
}
printf("\nstrings in dectionary formate are:-\n");
for(i=0;i<=4;i++)
puts(s[i]);
return(0);
}



input;-

                   Enter five strings
                   rahul                 
                   ajay                     
                   ravi                    
                   mohit                  
                   anmol                

output:-

                strings in dectionary formate  are:-
                 ajay                                               
                 anmol                                            
                 mohit                                            
                 rahul                                            
                  ravi                                                  
                  

Post a Comment

2 Comments