6( c) Given an Array of Strings Write a Program to Sort the String in Dictionary Order.
Algorithm
Sorting of string
1. Start
2. Read n
3. Initialize i:=0
4. Repeat steps 4 to 6 if i<n. else go to step 7
5. Read name[i]
6. i:=i+1
7. Set i:=0
8. Repeat steps 8 to 16 if i<n. else go to step 17
9. Set j:=i+1
10. Repeat steps 10 to15 if j<n. else go to step 16
11. If strcmp(name[i],name[j])>0 then go to step 12. else go to step 15
12. strcpy(temp, name[i])
13. strcpy(name[i],name[j])
14. strcpy(name[j],temp)
15. j:=j+1
16. i:=i+1
17. Initialize i:=0
18. Repeat steps 18 to 20 if i< n. else go to step 21
19. Write name[i]
20. i:=i+1
21. Stop
Flowchart:
#include<stdio.h>
#include<string.h>
main()
{
char names[50][20], temp[20];
int n,i,j;
printf("\n How many names U want:");
scanf("%d", &n);
printf("\n Enter the %d names one by one \n",n);
for(i=0; i<n; i++)
scanf("%s", names[i]);
for(i=0; i<n-1; i++)
{
for(j=i+1; j<n; j++)
{
if(strcmp(names[ i ], names[ j ]) > 0)
{
strcpy(temp, names[ i ]);
strcpy(names[ i ], names[ j ]);
strcpy(names[ j ] , temp);
}
}
}
printf("\n The Names in Dictionary Order:");
for(i=0; i<n; i++)
printf("\n %s", names[ i ]);
}

Comments
Post a Comment