Newish to C programming. Was given an assignment that reads "Take in a string from the user that is at most 100 characters long. Count how many space characters ' ' are within the string.". I actually want to understand this, but I have had no luck. A lot of the answers I could find online give solutions that include adding a new directory, but for the purposes of this class i'll have to stick with <stdio.h> and <stdlib.h>.
WHAT I'VE TRIED
So far, my first thought was to create an array.
#include <stdio.h>
#include <stdlib.h>
int main (void){
int counter = 0, x = 0;
char myArray[1][100];
scanf("%s", &myArray[0]);
while (x <= 100){
if (myArray[0][x] = ' '){
counter++;
}
x++;
}
printf("You have %i spaces within the string", counter);
}
Although after compiling (It only compiles through online IDEs, on Visual Studio this always fails to compile), i notice that the print statement always displays "You have 101 spaces within the string". I'm probably thinking about this wrong, in fact he hasn't shown us any example of this sort, just kinda left us to search this up ourselves. Please explain this to me, I don't want only the answer. I want to understand this. Even just a hint would be great!