Showing posts with label alphabetically. Show all posts
Showing posts with label alphabetically. Show all posts

Saturday, August 9, 2014

Program to alphabetically sort names.

Program to alphabetically sort names. If you need help, comment below

#include<stdio.h>
#include<string.h>
void sort(int);
void disp(int);
struct student
       {char name[25];
       }a[50],*ptr;
int main()
       {ptr=a;
        int i,j,n;
        printf("\nEnter the number of students:");
        scanf("%d",&n);
        n++;
        printf("\nEnter the names:\n");
        for(i=0;i<n;i++)
               gets((ptr+i)->name);
        sort(n);
        disp(n);
        return 0;
       }
void sort(int n)
       {

        int i,j;
        char copy[25];
        for(i=0;i<n;i++)
               for(j=0;j<strlen((ptr+i)->name);j++)
                       {if((ptr+i)->name[j]<97)
                               (ptr+i)->name[j]+=32;
                       }

        for(i=0;i<n;i++)
               for(j=0;j<n-1;j++)
                       if(strcmp((ptr+j)->name,(ptr+j+1)->name)>0)
                               {strcpy(copy,(ptr+j)->name);
                                strcpy((ptr+j)->name,(ptr+j+1)->name);
                                strcpy((ptr+j+1)->name,copy);
                               }
       }







void disp(int n)
       {
        int i;
        for(i=0;i<n;i++)
               (ptr+i)->name[0]-=32;
        printf("\n\nThe sorted list is:\n");
        for(i=1;i<n;i++)
               puts((ptr+i)->name);

       }