Outputting First Word of each Sentence - Strings
Dec 20, 2014 at 4:31pm UTC
Hi everyone.
I have been trying to get this to work for a while now - with no success.
Basically I am trying to write a function which the returns the first word of each input sentence in a single string - this is part of a larger cryptography program I am working on.
So for example, if this string was passed into the function:
"This is what I mean. Is it right? A poor puppy abandoned. Secret torturing of dogs are happening. Message: be on the watch."
It should return:
This Is A Secret Message:
This is the code I have so far:
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
//declared in class "steganalyse"
string cyphertext;
string punctuation = ".?!;:'" ;
book is_first_word
//function that is going wrong
string steganalyse::first_words()
{
string letters = "" ;
bool space_override = false ;
for (int i = 0; i < cyphertext.size(); i++)
{
if (is_new_sentence == true )
{
letters = letters + cyphertext[i];
if (cyphertext[i] == ' ' )
{
if (space_override == true )
{
space_override == false ;
}
else
{
is_first_word = false ;
}
}
}
else
{
if (cyphertext[i] == punctuation[0] || cyphertext[i] == punctuation[1] ||
cyphertext[i] == punctuation[2])
{
is_first_word = true ;
if (cyphertext[i+1] == ' ' )
{
space_override == true ; /*To make
sure is_first_word is not set to false at the first
space after the . or ! or ? */
}
}
}
}
return letters;
}
But this only returns the first word:
If anyone could show me how to fix my code, or has another way to return the first word of each sentence in a string I would be very grateful.
Dec 20, 2014 at 4:58pm UTC
Maybe it has something to do with the fast that you never change the value of is_new_sentence ?
Topic archived. No new replies allowed.