Advertisement
LuckyCipher

dictionary.c

Jan 30th, 2020
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.61 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3. int main()
  4. {
  5.     char arr[6][4] = {"egg","dog","cat","ba","az","ant"};
  6.    
  7.     int i;
  8.     printf("original:          ");
  9.     for(i=0;i<6;i++){
  10.         printf("%s ",arr[i]);
  11.     }
  12.     printf("\n");
  13.    
  14.     char temp[4];
  15.    
  16.     int j;
  17.     for(i=0;i<6-1;i++){
  18.         for(j=0;j<6-1;j++){
  19.             if(strcmp(arr[j],arr[j+1]) > 0){
  20.                 strcpy(temp,arr[j]);
  21.                 strcpy(arr[j],arr[j+1]);
  22.                 strcpy(arr[j+1],temp);
  23.                
  24.                 //cannot use temp = arr[j]
  25.                 //but can use strcpy(temp,arr[j]);
  26.             }
  27.         }
  28.     }
  29.    
  30.     printf("dictionary sorted: "); 
  31.     for(i=0;i<6;i++){
  32.         printf("%s ",arr[i]);
  33.     }
  34.     printf("\n");
  35.    
  36.     return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement