So I want to loop through infinitely and take a user string as input and then sort it. I want to continuously take input until the user enters 'EXIT'. That part of the code works. However, It won't loop infinitely properly. (This is also in strict C).
It should ask the user to input a string, sort it, print it, ask the user to input a string, sort it, etc. etc. However, what it does, is it asks the user to input a string, sorts it, prints it, then it prints it, prints it, prints it, etc. What is wrong here? Thanks.
int main(){
//Allocate a 100 character long array in the heap which will be passed and altered in place and outputted.
//I'm not fully sure how to allocate dynamically the appropriate amount of space for strings without creating a whole class for it.
//But seeing as this is c and not c++ then I'm not sure. Cause I need to have a length of how long the input is, but I need the memory allocated
//to store the string ti find the length of.
char* input = (char*)malloc(100);
//Scanf allows input for any characters including white space.
while(1){
printf("Enter an input string: ");
scanf("%[^\n]", input);
int len = strlen(input);
//Checks for exit condition. Input string being 'EXIT'
if(len == 4 && input[0] == 'E' && input[1] == 'X' && input[2] == 'I' && input[3] == 'T'){
break;
}
else{
//Function calls
remove_duplicates(input);
sort(input);
printf("\"%s\"\n", input);
}
}
free(input);
input = NULL;
return 0;
}