C program to read N names, store them in the form of an array and sort them in alphabetical order. Output the give names and the sorted names in two columns side by side with suitable heading

by Nideesh C on April 12, 2011 · 0 comments

in C Programming




/* Write a C program to read N names, store them in the form of an array and sort them in alphabetical order. Output the give names and the sorted names in two columns side by side with suitable heading */

#include <stdio.h>
#include <conio.h>
#include <string.h>

void main()
{
char name[10][8], Tname[10][8], temp[8];
int i, j, N;

clrscr();

printf(“Enter the value of N\n”);
scanf(“%d”, &N);

printf(“Enter %d names\n”, N);
for(i=0; i< N ; i++)
{
scanf(“%s”,name[i]);
strcpy (Tname[i], name[i]);
}

for(i=0; i < N-1 ; i++)
{
for(j=i+1; j< N; j++)
{
if(strcmpi(name[i],name[j]) > 0)
{
strcpy(temp,name[i]);
strcpy(name[i],name[j]);
strcpy(name[j],temp);
}
}
}

printf(“\n—————————————-\n”);
printf(“Input Names\tSorted names\n”);
printf(“——————————————\n”);
for(i=0; i< N ; i++)
{
printf(“%s\t\t%s\n”,Tname[i], name[i]);
}
printf(“——————————————\n”);

}                /* End of main() */

/*——————————–
Output
Enter the value of N
3
Enter 3 names
Monica
Laxmi
Anand

—————————————-
Input Names     Sorted names
—————————————-
Monica          Anand
Laxmi           Laxmi
Anand           Monica
—————————————-
—————————————-   */

Not Satisfied ? Just search & get the result

Related Posts Plugin for WordPress, Blogger...
Be Sociable, Share!

Related posts:

  1. C program to accept N numbers sorted in ascending order and to search for a given number using binary search. Report success or failure in the form of suitable messages
  2. C program to sort N numbers in ascending order array with suitable headings
  3. C program to read in four integer numbers into an array and find the average of largest two of the given numbers without sorting the array. The program should output the given four numbers and the average with suitable headings.
  4. C program to reverse a given integer number and check whether it is a palindrome. Output the given numbers with suitable message
  5. C program to input N numbers (integers or reals) and store them in an array. Conduct a linear search for a given key number and report success or failure in the form of a suitable message

Leave a Comment

Previous post:

Next post: