Monday, May 24, 2010

In C++ how can enhance my text (string) output?

i need the code to enhance a text which i read it from consol and saved as string,how can i print this text to:


* Make one space and only one space between each word and another.Each sentence separator (ie. ? , ! , ',', ';' , ' .') should not have a space before it and should have only once after it. (and i don't know if it possible to start the word after separator with capital letter ).


EX:


string mystr;


mystr(cin.'/n');


// supose i read the string as :


hi how?are you?! im fine.


after reading i want to print (mystr) the text as:


Hi how? Are you?! Im fine.// look at spaces "only one space between each word" and notic the capital letter after seperator (ex. how? Are..)

In C++ how can enhance my text (string) output?
The trick behind this question is doing things in the right order.





1) Start with replacing each of the sentence separators for the same character followed by a space.


2) Next, replace all double spaces for a single space, and repeat this for as long as there are double spaces in the string.


3) Replace each combination of a space followed by any of the sentence separators for the sentence separators (in other words, remove any spaces before a sentence separator).


4) If they exist, remove the space at the beginning of the sentence and the space at the end of a sentence. Because of step 2, there can only be (at most) one space at the beginning or end.


5) If the last character isn't a period, add a period at the end.


6) Capitalize the first character of the string.


7) Look for sentence separators that are followed by a space; capitalize the character that follows the space.





I think that's it. I hope it helps.








PS: 'replace' is discussed on page 595 of Stroustrup's book.


 


 


 


No comments:

Post a Comment