a C program that declare an array of five characters using a WHILE loop and sort the elements in row one in alphabetical order using a nested FOR loop and IF statement and print the sorted characters using a WHILE loop
#include <stdio.h>
int main()
{
int i, j;
char charray[5];
char temp;
i = 0;
printf("Enter five chars:\n");
while (i < 5)
{
charray[i]=getchar();
getchar();
i++;
}
for(i = 0; i < 4; i++)
for(j = i+1; j < 5; j++)
{
if (charray[i] > charray[j])
{
temp = charray[i];
charray[i] = charray[j];
charray[j] = temp;
}
}
printf("\n\nSorted chars:\n");
i = 0;
while (i < 5)
{
printf("%c ", charray[i]);
i++;
}
return 0;
}
Comments
Leave a comment