I am trying to alphabetize a list from user input. However when it prints the ordered list I get like letters, it's not completely junk, but it still isn't right. It's like a combination of the words I just input and input from the last time I ran the program, or the words kinda jumbled up. I'm new to programming and was just wondering if someone saw something I wasn't. Thank you
Code:
#include <stdio.h>
#include <string.h>
int main()
{
int i, j;
char ch, string[10][20], temp[20];
int num;
j=0;
i=0;
while(1)
{
ch = getchar();
if(ch <= 0x39 && ch >= 0x30)
{
putchar(ch);
num = ch - 0x30;
}
if(ch == 0x0D)
printf("\n\r");
break;
}
while(i <= num)
{
ch = getchar();
if (ch <= 0x7A && ch >= 0x61)
{
putchar(ch);
string[i][j] = ch;
j++;
}
if(ch == 0x0D)
{
string[i][j] = '\0';
printf("\n\r");
i++;
}
}
for (i = 0; i < num - 1 ; i++)
{
for (j = i + 1; j < num; j++)
{
if (strcmp(string[i], string[j]) > 0)
{
strcpy(temp, string[i]);
strcpy(string[i], string[j]);
strcpy(string[j], temp);
}
}
}
printf("\nSorted Names\r");
for (i = 0; i <= num; i++)
{
printf("%s\n\r", string[i]);
}
return 0;
}