2016-05-09 9 views
-1

単語順に文字列を逆にしたいと考えています。文字列が "Cat is running"の場合と同様に、 "running is Cat"である必要があります。これは、ポインタと配列の若干の誤差を示す再帰によって文字列内の単語の順序を逆にする方法

#include<iostream> 
#include<string> 

using namespace std; 
void reverseString(string str); 
int length, lastLength; 

int main() { 

    string s; 
    cout << "Enter a string to reverse its words: "; 
    getline(cin, s); 
    lastLength = s.length() - 1; 
    length = lastLength; 
    cout << "\nThe string in reverse order is "; 
    cout << endl; 
} 

void reverseString(string str) { 

    if (length < 0) 
     return; 
    else { 
     if (str.at[length] == " " || length == 0) 
     { 
      if (length == 0) 
       length = -1; 
      for (int i = length + 1; i < lastLength; i++) 
       cout << str.at[length]; 
      lastLength = length - 1; 
     } 
     length--; 
     reverseString(str); 
    } 
} 

: ここコードです。私はこれを解決する方法を知らない。 本当にありがとうございます! :)

+1

それはコンパイルエラーまたは実行時エラーまたは間違った出力であるが、どうか明らかにしてください!! mainで呼び出された 'reverseString'を見ることはできません。 – piyushj

+0

再帰でグローバル変数を使わないでください。コードを読み込み/解析するのがかなり難しくなります。 –

+0

ループ不変式とその本体が正しくありません。あなたがそれで達成したいものを再検討してください。 –

答えて

0

2つの異なるエラーがあります。 .atは、.at()ではなく、.at[]というメソッドである必要があります。次に、charstring( "")を比較します。したがって、 ""を "'に置き換える必要があります。

#include<iostream> 
#include<string> 

using namespace std; 
void reverseString(string str); 
int length, lastLength; 

int main() { 

    string s; 
    cout << "Enter a string to reverse its words: "; 
    getline(cin, s); 
    lastLength = s.length() - 1; 
    length = lastLength; 
    cout << "\nThe string in reverse order is "; 
    cout << endl; 
} 

void reverseString(string str) { 

    if (length < 0) 
     return; 
    else { 
     if (str.at(length) == ' ' || length == 0) // <- note the changes here 
     { 
      if (length == 0) 
       length = -1; 
      for (int i = length + 1; i < lastLength; i++) 
       cout << str.at(length); // <- note the changes here 
      lastLength = length - 1; 
     } 
     length--; 
     reverseString(str); 
    } 
} 

ロジックを確認しませんでした。あなたは、ロジック:)

+0

エラーが発生しました。それは真剣に未成熟です。あなたのメソッドのように:) –

0

std::stringは、多くのヘルパー関数を持っている上のようなstring::findstring::rfind、あなたが代わりに個別の文字にアクセスするための、文字列の操作に使用できるstd::substrを作業を続けることもできます。たとえば:

void reverseString(std::string str, size_t end) 
{ 
    size_t pos = str.rfind(' ', end); 
    if (pos == std::string::npos) 
    { 
     cout << str.substr(0, end + 1) << endl; 
    } 
    else 
    { 
     cout << str.substr(pos + 1, end - pos) << endl; 
     reverseString(str, pos - 1); 
    } 
} 

int main() 
{ 
    std::string s = "Enter a string to reverse its words"; 
    cout << s << endl; 
    reverseString(s, s.length()); 
} 
+0

**非常に参考になった** –

0

はここで少しだけC++ <string>便利で、あなたのソリューションのロジックを維持しようとしたバージョンです:

void output_reverse_string(string str, int last, int current) { 
    /* Terminating condition: we've exhausted the input: */ 
    if (current == 0) { 
     std::cout << str.substr(current, 1 + last - current); 
     return; 
    } 
    /* Recurse until we've found a space: */ 
    if (str.at(current) != ' ') { 
     output_reverse_string(str, last, current - 1); 
     return; 
    } 
    /* Since we've found a space, output the following word: */ 
    std::cout << str.substr(current + 1, last - current); 
    /* Just for readability, can be skipped: */ 
    std::cout << " "; 

    /* Recurse on the *remaining* string contents: */ 
    output_reverse_string(str, current - 1, current - 1); 
} 
関連する問題