2017-06-24 11 views
-1

私はC++の基礎を学んでいます。私は与えられた入力の各単語のすべての文字を大文字にする簡単な関数を書こうとしています。私が書いたもの:関数が正しく動作しない

#include <iostream> 
#include <string> 
#include <vector> 
#include <cctype> 

int main() 
{ 
    std::cout << "Please enter a sentence: "; 
    std::vector<std::string> words; 
    std::string x; 

    while (std::cin >> x) { 
     words.push_back((std::string) x); 
    } 
    std::cout << std::endl; 
    std::vector<std::string>::size_type size; 
    size = words.size(); 

    for (int j = 0; j != size; j++) { 
     std::string &r = words[j]; 
     for (int i = 0; i != r.length(); i++) { 
      r = toupper(r[i]); 
      std::cout << r << std::endl; 
     } 
    } 
} 

大文字の各単語の最初の文字を返します。私はハロー世界を記述する場合、例えば、プログラムリターン:

H 
W 

は、誰かが私が間違っているとどのようにそれを修正するよ何を教えてくださいすることができます。

+1

<algorithm>'sstd::transform()を使用する方法の始まりである - それは何もしません。 –

+0

*私は、与えられた入力の各単語のすべての文字を大文字にする簡単な関数を書こうとしています* - アルゴリズム関数 "C++の基礎"を学ぶことを考えるなら、 'std :: transform(wordsその 'i'ループの代わりに[j] .begin()、words [j] .end()、words [j] .begin()、toupper); – PaulMcKenzie

答えて

0
for (int j = 0; j != size; j++) { 
    std::string &r = words[j]; 
    for (int i = 0; i != r.length(); i++) { 
     r = toupper(r[i]); 
     std::cout << r << std::endl; 
    } 
} 

、あなたは長さ1の文字列であることをrを上書きしているので、あなたの内側のforループ条件が偽になると、あなたは、内側のループから抜け出します。したがって、各単語の最初の文字だけが印刷されます。

これを修正するには、戻り値toupperを他の変数に保存します。

for (int j = 0; j != size; j++) { 
    std::string &r = words[j]; 
    for (int i = 0; i != r.length(); i++) { 
     char c = toupper(r[i]); 
     std::cout << c << std::endl; 
    } 
} 
0

各単語のあなたの取扱いが間違っている:

std::vector<std::string>::size_type size; 
size = words.size(); 
for (int j = 0; j != size; j++) { 
    std::string &r = words[j]; 
:simplfication、あなたのループのよう

r[0] = toupper(r[0]); 
    std::cout << r << '\n'; 

:あなたが実際に必要なもの

for (int i = 0; i != r.length(); i++) { 
     r = toupper(r[i]); 
     std::cout << r << std::endl; 
    } 

は、最初の文字のみを修正することです

もっと簡潔にすることができます:

で3210
for (std::string &r : words) { 
0

私は、文字列操作(複数可)を行うためstatic関数やメソッドのみが含まれるユーティリティクラスを持っています。ここに私のクラスはtoUppertoLower静的メソッドで次のようになります。

ユーティリティ

#ifndef UTILITY_H 
#define UTILITY_H 

#include <string> 

class Utility { 
public: 
    static std::string toUpper(const std::string& str); 
    static std::string toLower(const std::string& str); 
private: 
    Utility(); 
}; 

#endif // UTILITY_H 

#include "Utility.h" 
#include <algorithm> 

std::string Utility::toUpper(const std::string& str) { 
    std::string result = str; 
    std::transform(str.begin(), str.end(), result.begin(), ::toupper); 
    return result; 
} 

std::string Utility::toLower(const std::string& str) { 
    std::string result = str; 
    std::transform(str.begin(), str.end(), result::begin(), ::tolower); 
    return result; 
} 

用途:

#include <string> 
#include <iostream> 

#include "Utility.h" 

int main() { 
    std::string strMixedCase = std::string("hEllO WOrlD"); 
    std::string lower = Utility::toLower(strMixedCase); 
    std::string upper = Utility::toUpper(strMixedCase); 

    std::cout << lower << std::endl; 
    std::cout << upper << std::endl; 

    return 0; 
} 

注: - これは、渡された文字列の完全な文字列操作を行います。文字列内で特定の文字を実行しようとしている場合は、あなたは別の何かをする必要があるかもしれませんが、これは、 ``キャスト(STD ::文字列)を削除し::toupper::tolower

関連する問題