I have a small program which reverses the words when inputted however for some reason the first input is being outputted as a blank without allowing the user to enter any input for it. This is happening for any amount of n, and I have been playing around with the for loops (were I think the error is) however I have not yet managed to eliminate the problem.
Thanks In Advance and Happy Holidays!
CODE:
int main()
{
int n;
printf("How many strings would you like to insert?\n");
scanf("%d",&n);
n=n+1;
char s[n][100];
printf("Insert %d strings\n", n-1);
for(int i=0; i<n; i++) {
gets(s[i]);
}
for (int i = 0; i <n ; i++) {
reverseWords(s[i]);
printf("The number %d Sentence is %s\n", i+1, s[i]);
}
return 0;
}
Input/Output:
How many strings would you like to insert?
3
Insert 3 strings
hello whats up
bye dude
The number 1 Sentence is
The number 2 Sentence is up whats hello
The number 3 Sentence is dude bye
Process finished with exit code 0
EDIT:
I managed to find a fix myself. Thoughts on this fix?
int main()
{
int n;
printf("How many strings would you like to insert?\n");
scanf("%d",&n);
char s[n][100];
printf("Insert %d strings\n", n);
for(int i=0; i<n+1; i++) {
gets(s[i]);
}
for (int i = 1; i <n+1 ; i++) {
reverseWords(s[i]);
printf("The number %d sentence in reverse is %s\n", i, s[i]);
}
return 0;
}
New Output: "G:\UoM\Second Year\Programming Principles in C\Question 1\cmake-build-debug\CLion.exe" How many strings would you like to insert? 3
Insert 3 strings
Hello Carl whats up?
Dude Whats giong on?
wow nice!
The number 1 sentence is up? whats Carl Hello
The number 2 sentence is on? giong Whats Dude
The number 3 sentence is nice! wow