I'm doing this exercise:
Write a program that reverses the words of a sentence like this: My name is John --> John is name My
and I wrote this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
int word=0,character=0;
char input[50];
char output[50][50];
int InLength;
printf("Enter some words:\n");
fgets(input,50,stdin);
input[strcspn(input, "\n")] = 0;
InLength=strlen(input);
for (int i=0;i<InLength;i++){
if (input[i]==' '){
character=0;
word++;
}
else{
output[word][character]=input[i];
character++;
}
}
for (word;word>=0;word--){
printf("%s ",output[word]);
}
printf("\n");
}
The problem is that the output gives a strange character with a question mark next to some words. For example:
Enter some words:
hello how are you
you���� are how hello
Another example:
Enter some words:
first second third hello good
good� hello�� third���� second first
I don't know why the output is showing this strange characters. It's probably a dumb question but I'm a beginner.
P.S. Sorry for my bad English.