I am attempting to alphabetically sort user input with a maximum of 10,000 words and a max length of 25 words. I am using 'stop' to end user input prematurely which is running me into some issues. The current program results in the following as output when I try to input hello stop
▒l▒
0▒l▒
A{▒
e▒
▒&
▒▒
▒▒
▒▒
▒▒
▒l▒
▒l▒
▒▒;
▒Se▒
▒
▒
▒
▒
▒!
Ќl▒
▒
▒
▒
▒.X
▒
I am assuming this has to do with my memory allocation but I am not too sure and couldn't find some answers regarding this. Any help would be appreciated, the following is my code (feel free to ignore the lowercase pointer, still working on getting the output to turn into lowercases!)
#include<stdio.h>
#include <string.h>
//for using tolower
#include <ctype.h>
int main() {
int i, k, j;
char abc[25];
const char *stop = "stop";
char *p; //using for lowercase
//using 2d array for max of 10,000 words, max size of words 25
char str[10000][25], temp[25];
printf("Enter up to 10000 words, type stop to enter the current words:\n");
while (strncmp(abc, "stop", 5) != 0) {
scanf("%s", abc);
}
//for (i = 0; i < 10000; ++i)
//scanf("%s[^\n]", str[i]);
for (i = 0; i < 10000; ++i)
for (k = i + 1; k < 10000; ++k) {
//comparing two strings using strcmp() function is used
//using strcpy() to copy string to a temp
if (strcmp(str[i], str[k]) > 0) {
strcpy(temp, str[i]);
strcpy(str[i], str[k]);
strcpy(str[k], temp);
}
}
//using pointer to converting to lowercase
//src: https://www.daniweb.com/programming/software-development/threads/57296/how-does-one-tolower-an-entire-string
for (p = str; *p != '\0'; p++)
*p = (char) tolower(*p);
//printing words in lexi order
printf("\nWords in lexicographical order: \n");
for (i = 0; i < 10000; ++i) {
puts(str[i]);
}
printf("WARNING: Words longer than 25 in length were ignored. \n");
return 0;
}
str.abcis uninitalised the first time thewhileloop runs. Turn up your compiler warnings and fix all of them before posting a question. And learn to use a debugger. The problems should be clear if you had used a debugger.