I am just wondering why this code produces different results
#include <stdio.h>
#include <string.h>
char names[5][25],temp[25];
int x;
void main()
{
for (x=0;x<=4;x++)
{
scanf("%s",names[x]);
}
for (x = 0;x<=4;x++)
{
printf("%s",names[x]);
}
// the above works well
//but this one didn't produces the correct output
for (x=0;x<=4x++)
{
if(strcmp(names[x],names[x+1]))
{
temp = names[x];
names[x] = names[x+1];
names[x+1] = temp;
}
}
for (x=0;x<=5;x++)
{
//here i suppose it would print names
//in ascending order but instead characters became
//garbled.
printf("%s",names[x]);
}
a sample result after inputting names: Antonio tony Arman Rex
would produce Antonio nyArm etc. depending on input
can anyone help me correct this code? I want to sort names using strcmp function.
Thanks in advance.
#include <stdio.h>
#include <string.h>
char names[5][25],temp[25];
int x;
void main()
{
for (x=0;x<=4;x++)
{
scanf("%s",names[x]);
}
for (x = 0;x<=4;x++)
{
printf("%s",names[x]);
}
// the above works well
//but this one didn't produces the correct output
for (x=0;x<=4x++)
{
if(strcmp(names[x],names[x+1]))
{
temp = names[x];
names[x] = names[x+1];
names[x+1] = temp;
}
}
for (x=0;x<=5;x++)
{
//here i suppose it would print names
//in ascending order but instead characters became
//garbled.
printf("%s",names[x]);
}
a sample result after inputting names: Antonio tony Arman Rex
would produce Antonio nyArm etc. depending on input
can anyone help me correct this code? I want to sort names using strcmp function.
Thanks in advance.