Censor 4 letter words String problem
May 16, 2014 at 8:11pm UTC
Hi i have a problem with my code.
You are given a sentence and you should censor the 4 letter words within the sentence.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
#include<iostream>
#include<string>
using namespace std;
int main ()
{
int N;
cin>>N;
int k,j,counter=0;
string text;
getline(cin,text);
for (int y=0;y<text.length();y++){
counter=0;
while (text[y]!=' ' )
{
counter++;
y++;
}
if (counter==4)
text.replace(y-4,4,"****" );
}
for (int i=0;i<text.length();i++)
cout<<text[i]<<endl;
return 0;
}
Also i would like to ask what are some of the most helpful string functions???
Thank you
May 16, 2014 at 9:08pm UTC
Helpful string functions (Depends on what you need):
http://www.cplusplus.com/reference/string/string/
To solve your problem, don't read the entire sentence in at once. Read it word by word and for each word that is of length 4, print stars otherwise print the word
Topic archived. No new replies allowed.