Saturday, 19 August 2017

Sorting C Language Keyword Program

Q : Write a ‘C’ program to list of keywords which must be sorted in increasing order in the table.

Solution : 


#include<stdio.h>
int power(int,int);
void main()
{

     char keyword[][15]={"char","do","extern","if","return","static","union","while","case","default","enum","goto","register","
sizeof",
             "typedef","volatile","break","continue","else","for","long","signed","switch","void","auto","const","double",
              "float","int","short","struct","unsigned"};
     int i,j;
     char tmp[15];
     printf("\nUnsorted Keyword Array\n\n");
     for(i=0;i<32;i++)
          printf("[%s] ",keyword[i]);

     for(i=0;i<32;i++)
     {
          for(j=0;j<32-i-1;j++)
          {
               if(strcmp(keyword[j],keyword[j+1])>0)
               {
                    strcpy(tmp,keyword[j]);
                    strcpy(keyword[j],keyword[j+1]);
                    strcpy(keyword[j+1],tmp);
               }
          }
     }
     printf("\n\nSorted Keyword Array\n\n");
     for(i=0;i<32;i++)
          printf("[%s] ",keyword[i]);
}