2017-03-21 9 views
-1

最後に大文字を持つ単語だけを表示するprogrammを書くべきです。例えば間違いが見つかりません。 C++、strings

: 「ちょうどそこにいくつかの単語」 - >「いくつかの単語右」

しかし、そこに私のコードに何か問題があると私は間違いを見つけることができません。 (このコードのため申し訳ありませんが、それは本当に台無しにされる可能性があります)

string LastUpperSymbol(string text) { 

    int i = 0, space, next, sortEl = 0; 
    string textcopy = text; 
    int size = textcopy.size(); 
    string sorted; 
    string alph = "abcdefghijklmnopqrstuvwxyz "; 
    if (textcopy[0] != alph[26]) { 
     space = text.find(" "); 
     if(isupper(textcopy[space-1])) { 
      sorted.append(textcopy, 0, space+1); 
     } 
     while(space < textcopy.size()) { 
      next = space+1; 
      space = text.find(" ", next); 
      if(space == -1) { 
       if(isupper(textcopy[size])) { 
        sorted.append(textcopy, next, textcopy[size]); 
       } 
       break; 
      } 
      else if(isupper(textcopy[space-1])) { 
       sorted.append(textcopy, next, space+1); 
      } 
     } 
    } 
    else { 
     //something 
    } 
    cout << sorted << endl; 
    return text; 
} 
int main() { 

    string text = "somE wordS just RighT there"; 
    cout << LastUpperSymbol(text); 
    system("PAUSE"); 
} 
+0

文字列の実装にはデバッグフラグが付いているため、範囲外チェックのようなものを有効にすることができますが、特に 'string'ではなく他のライブラリ部分について考えることができます。それ以外の場合は、 '[] 'の代わりに' at'を使って範囲外を大きくすることができます。また、サニタイザツールを試すこともできます。 – chris

+6

私のアドバイスは、デバッガを使用して各ステップで変数を調べるコードをステップごとに実行する方法を学ぶことです。 – drescherjm

+2

'find'の結果を常にチェックして、' std :: string :: npos'でないことを確認してください。 – NathanOliver

答えて

1

標準ライブラリは、すでにあなたがここで何をする必要があるかの大多数のためのコードを提供し、それはおそらく簡単だが、そこの代わりに何を使用します既存のコードの問題を把握しようとしています。私は

else if(isupper(textcopy[space-1])) { sorted.append(textcopy, next, **space+1**); }

スペース+ 1を追加する文字列の数で見ることができる限り

std::string silly_filter(std::string const &in) { 

    std::istringstream buff(in); 
    std::ostringstream out; 

    std::copy_if(std::istream_iterator<std::string>(buff), 
     std::istream_iterator<std::string>(), 
     std::ostream_iterator<std::string>(out, " "), 
     [](std::string const &s) { 
      return ::isupper((unsigned char)*s.crbegin()); 
     }); 
    return out.str(); 
} 
0

は、私はおそらく、このような仕事に何かをしたいです。したがって、next = 5の場合、文字列はwになります。したがって、2番目のパラメータは 'wordS'の長さ、つまり5の場合は2番目のパラメータが増加し続けます。

関連する問題