Answer the question
In order to leave comments, you need to log in
How to count spaces in a string?
There is a string entered from the keyboard
1 2 3 abc
Stored in the variable test
How to calculate how many spaces are in this string and put the result in a variable?
Answer the question
In order to leave comments, you need to log in
#include <stdio.h>
#include <string.h>
int main(){
char s[] = "2 3 1 2 3 1 2 3";
char key = '\x20';
char *p;
int result = 0;
p = strchr(s, key);
while(p != NULL){
result++;
p = strchr(p + 1, key);
}
printf("%d\n", result);
return 0;
}
char str[100];
fgets(str, 100, stdin);
int i, qnt = 0;
for (i = 0; str[i] != '\0'; i++)
if (str[i] == ' ')
qnt++;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question