QUESTION:- sorting the number of students
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
int n, i, j;
char str[20][20], temp [20]; // temp can be only one array because we are going to use it for a time only.
printf("enter a number of students: ");
scanf("%d", &n);
for(i =0; i<n; i++)
{
printf("enter the name of students %d: \n", i+1);
scanf("%s", str[i]);
}
for(i=0; i<n; i++)
{
for(j =i+1; j<n; j++)
{
if(strcmp(str[i], str[j])>0)
{
strcpy(temp, str[i]); // temp = num[i]
strcpy(str[i], str[j]); // num[i] = num[j]
strcpy(str[j], temp); // num[j] = temp
// but we are doing string function so we have to sue copy and do accordingly.
}
}
}
// display the sum
printf("the sorted name of the student are: \n" );
for(i = 0; i<n; i++)
{
printf("%s ", str[i]);
}
getch();
}
OUTPUT
enter the name of student 1:
Yogesh
enter the name of student 2:
Mahesh
enter the name of student 3:
Kusal
enter the name of student 4:
Deeksha
the sorted name of the students are
Deeksha Kusal Mahesh Yogesh
0 Comments