开发者

How to Reverse a given sentence (string) in C++?

Example: if the input was DOGS LIKE CATS output- CATS LIKE DOGS

consider that I have to use only : If-else conditions, while & for loops, Arrays, strings and Functions. NOT strings functions, Pointers & Dynamic memory allocation & structures. Spaces need to be the same as the example as well.

I tried to do the following but it doesnt work can you help please?

void revSent(char str[]){
char temp[100];
int k;
for (i=sentenceSize ; i>0 ; i--)
    for (k=0 ; k<sentenceSize ; k++)
        temp[k]=str[i];

for (k=0 ; k<sentenceSize ; k++)
    if (temp[k]!=' ')
        for (开发者_如何转开发i=k ; i>0 ; i--)
            printf("%c", temp[i]);

}


It's easy to do this in-place, without any additional data structures:

  1. reverse the whole string: DOGS LIKE CATS -> STAC EKIL SGOD

  2. reverse each word in the string: STAC EKIL SGOD -> CATS LIKE DOGS

Hint: you can use the same function for both (1) and (2).


You could implement the following to arrive at a solution:

  1. Separate the sentence into a list of words.
  2. Reverse this list.
  3. Concat this list together to form the sentence.


If you define a word as a whitespace-delimited token then the following will do:

std::vector<std::string> sentence;
std::copy(std::istream_iterator<std::string>(std::cin),
          std::istream_iterator<std::string>(),
          std::back_inserter(sentence));
std::reverse(sentence.begin(), sentence.end());

In essence you want to start with the definition of a word, then place in a container your words, and finally use std::reverse() to reverse them.

For an algorithms homework your instructor probably won't be satisfied with this. You want to create a function that splits a sentence into words. You can, of course, work with pointers within the same string -- and that may well be the intent of your instructor, but if that isn't what you must then I personally find working with a container easier.


I'll give a hint: since you can't use data structures you can't directly use Paul or OJ's method. BUT, recursive function calling would form a stack.


  • Break the sentence into words
  • In order, push each word onto a stack
  • Pop each item off the stack and print out/add to list/write to file/whatever.

Voila!


who says the STL isn't useful?

Depending on how you do this, there are different ways to attack this.

my way is just:

while (original_string isn't empty){    
   take first word  
   prepend to reversed string
}

Here's the C++ solution (using find and substr, two very useful string functions)

    using namespace std;
string word_reverse(string original){
    string reverse_string;
    if (original.find(' ')!=string::npos){
        do{
            int pos=original.find(' ');
            //prepend word
            reverse_string=original.substr(0,pos)+' '+reverse_string;
            //remove word from original,considering the found whitespace
            original=original.substr(pos+1,original.length()-(pos+1));
        }while(original.find(' ')!=string::npos);
        //don't forget the last word!
        return original+' '+reverse_string;
    }
    else{//no whitespace: return original text
        return original;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜