What will be the program to print the number of spaces in a string | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

What will be the program to print the number of spaces in a string

using if statement

4th Jun 2017, 8:31 AM
Sanjay Bisht
Sanjay Bisht - avatar
7 Answers
+ 8
i think you could study the string functions like isspace() . int y=0; for(x=0;x<str.length;x++) if(isspace(str[x])) /* it s a bit different from str[x]==' ' */ y++; cout<<y; edit: you can use the number in the ASCII table instead of ' '. :))
4th Jun 2017, 6:03 PM
Umbe
Umbe - avatar
+ 7
i came too late for being the best answer, this is so sad dude
4th Jun 2017, 6:32 PM
Umbe
Umbe - avatar
+ 3
doesn't work
4th Jun 2017, 9:36 AM
Roholah N
Roholah N - avatar
+ 2
it isn't working
4th Jun 2017, 9:50 AM
Sanjay Bisht
Sanjay Bisht - avatar
+ 1
hey Luka it is showing the error that empty character constant
4th Jun 2017, 3:40 PM
Sanjay Bisht
Sanjay Bisht - avatar
+ 1
now my problem is solved my main problem was that i was taking the string by cin>>a; and I totally forget that it doesn't take spaces and instead of that now I'm using gets(a); so it is helpful thanks guys for your support
4th Jun 2017, 3:47 PM
Sanjay Bisht
Sanjay Bisht - avatar
- 1
here my c++ code: #include <iostream> #include<string> using namespace std; int main() { string s="life"; for(int i=0;i<s.length();i++) { cout<<s[i]; cout<<" "; } return 0; } I hope it will help 🙂
4th Jun 2017, 11:53 AM
Anu Agrawal
Anu Agrawal - avatar