Pages

Friday, 9 February 2018

per line. Also give the total number of words in the list. Output format should be: C Program to read a list of words, sort the words in alphabetical order and display them one word per line. Also give the total number of words in the list. Output format should be: Total Number of words in the list is _______ Alphabetical listing of words is:



Write a program to read a list of words, sort the words in alphabetical order and display them one word per line. Also give the total number of words in the list. Output format should be:
C Program to read a list of words, sort the words in alphabetical order and display them one word per line. Also give the total number of words in the list. Output format should be:
Total Number of words in the list is _______
Alphabetical listing of words is:
#include<stdio.h>
#include<conio.h>
#define N 100

struct word
{
     char wrd [30];
};

int main()
{
                struct word w[N];
    FILE *fp;
                char s1[30];
                int i=0,count=0;
                void srt_wrd();
    fp=fopen("sample.txt","rb+");
    while(fgets(s1,30,fp)!=NULL)
                {
                                strcpy(w[i].wrd,s1);
                                i++;
                                count++;
                }
                printf("Total words in file = %3d\n",count);
                printf("\t\tList in alphabetical order:\n");
                srt_wrd(&w,count);
                fclose(fp);
                return 0;
}
void srt_wrd( struct word *w, int n)
{
                int i,j,k=0;
                struct word temp;
                for(i=0;i<=n;i++)
                {
                    for(j=i+1;j<n;j++)
                      {
                                if(w[i].wrd[0] > w[j].wrd[0])
                                {
                                  temp=w[i];
                                      w[i]=w[j];
                                      w[j]=temp;
                                }
                              else if(w[i].wrd[0]==w[j].wrd[0] && w[i].wrd[1] > w[j].wrd[1])
                                {
                                  temp=w[i];
                                      w[i]=w[j];
                                      w[j]=temp;
                            }
                                else if(w[i].wrd[0]==w[j].wrd[0] && w[i].wrd[1]==w[j].wrd[1])
                                {
                                      if(w[i].wrd[2]>w[j].wrd[2])
                                      {
                                       temp=w[i];
                                           w[i]=w[j];
                                        w[j]=temp;
                                       }
                                }
                                else if(w[i].wrd[0]==w[j].wrd[0] && w[i].wrd[1]==w[j].wrd[1])
                                {
                        if(w[i].wrd[2]==w[j].wrd[2] && w[i].wrd[3]>w[j].wrd[2])
                                          {
                                   temp=w[i];
                                                   w[i]=w[j];
                                                   w[j]=temp;
                                           }
                                }
                     }
                }
                for(i=0;i<n;i++)
                {
                       printf("%-s\n",w[i].wrd);
                       k++;
                       if(k==10)
                     {        
                                printf("\n\nPress any key ...");
                                return 0;
                                k=1;
                        }
                }
}