Write a 'C' Program to accept 5 names from user and store these names into an array. Sort these array elements in alphabetical order.
#include<stdio.h> void main() { char str[5][80],*temp; int i,j; clrscr(); printf("ENTER ANY 5 NAMES ::>\n"); for( i = 0 ; i < 5 ; i++) { gets(str[i]); } printf("NAMES IN ALPHABETICAL ORDER ::>\n"); for(i = 0 ; i < 5 ; i++) { for( j = 0 ; j < 5 ; j++) { if(strcmp(str[i],str[j]) < 0) { strcpy(&temp[0],&str[i][0]); strcpy(&str[i][0],&str[j][0]); strcpy(&str[j][0],&temp[0]); } } } for(i = 0 ; i < 5 ; i++) { printf("%s\n",str[i]); } getch(); } /*OUTPUT:: ENTER ANY 5 NAMES ::> Sachin Zaheer Harbhajan Yuvraj Dhoni NAMES IN ALPHABETICAL ORDER ::> Dhoni Harbhajan Sachin Yuvraj Zaheer */
Write a 'C' Program to accept 5 names from user and store these names into an array. Sort these array elements in alphabetical order.
Reviewed by
on
April 24, 2015
Rating: