+ 2

How do I find the number of ‘s’ character in the code

https://code.sololearn.com/ckcj9608N97i/?ref=app

5th Dec 2018, 12:46 PM
farhan bhatti
farhan bhatti - avatar
3 RĂ©ponses
+ 8
while(str[i] =='s'); Its the same of saying "iterate the string until you find first s" You need to put a condition inside while to iterate all the string and using a if test (str[i] =='s'); edit here : https://code.sololearn.com/cd9KJWofJs3q/?ref=app
5th Dec 2018, 1:02 PM
Anya
Anya - avatar
+ 4
I'd use this: int count = 0; char s[] = "So this is our little test string."; for (int i=0; s[i]!='\0' ; ++i) if (s[i] == 's' || s[i] == 'S') ++count; printf("%d", count);
5th Dec 2018, 2:18 PM
HonFu
HonFu - avatar
+ 1
thanks for the help Anya , i also tried int main() { int count = 0; int i; char str [30]="Saturday afternoon is sunny\0"; for (i=0; str[i] != '\0';i++) //for (i= 0; str[i]== 's';i++) //while (str=='s'|| str == 'S'); if (str[i] == 's' || str[i] == 'S') count++ ; printf("Number of s characters in the string is %d\n", count); //output: 3 return 0; }
5th Dec 2018, 1:06 PM
farhan bhatti
farhan bhatti - avatar