+ 3
What will be the program to print the number of spaces in a string
using if statement
10 Antworten
+ 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 ' '. :))
+ 7
i came too late for being the best answer, this is so sad dude
+ 3
doesn't work
+ 2
it isn't working
+ 1
hey Luka it is showing the error that empty character constant
+ 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
- 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 🙂