Search This Blog

Wednesday, October 28, 2015

sort all given strings

1:  #include <stdio.h>  
2:  #include <string.h>  
3:  int my_strcmp(char *str1,char *str2)  
4:  {  
5:    while(1)  
6:    {  
7:      if ( *str1 != *str2 )  
8:        return (*str1 > *str2 ? 1:-1);  
9:      if (str2)  
10:        break;  
11:      str1++;  
12:      str2++;  
13:    }  
14:  }  
15:  void string_sorting(char str[][20],int cnt)  
16:  {  
17:    char temp[100];  
18:    int i = 0,j=0,k=0;  
19:    while(i<cnt)  
20:    {  
21:      for(j=0;j < cnt-1;j++)  
22:      {  
23:        if ( 0 < strcmp(str[j],str[j+1]))  
24:        {  
25:          strcpy(temp,str[j]);  
26:          strcpy(str[j],str[j+1]);  
27:          strcpy(str[j+1],temp);  
28:        }  
29:      }  
30:      i++;  
31:    }  
32:  }  
33:  main()  
34:  {  
35:    int i=0;  
36:    char str[8][20] =  
37:  {"orange","apple","mango","kiwi","pear","cherry","plum"};  
38:    for(i=0;i<7;i++)  
39:    {  
40:      printf("%s\n",str[i]);  
41:    }  
42:    string_sorting(str,7);  
43:    printf("\n==========================\n");  
44:    for(i=0;i<7;i++)  
45:    {  
46:      printf("%s\n",str[i]);  
47:    }  
48:  }  

No comments:

Post a Comment