2017-06-24 144 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; 
     } 
    } 
} 

返回大寫的每個單詞的第一個字母。例如,如果我寫的Hello World程序返回:

H 
W 

有人能告訴我什麼,我做錯了,如何解決它。

+1

刪除'(std :: string)'cast - 它什麼都不做。 –

+0

*我試圖編寫一個簡單的函數,將給定輸入中每個單詞的每個字母都用大寫* - 如果您考慮學習算法函數「C++基礎知識」,您可以簡單地使用'std :: transform(words [j] .begin(),words [j] .end(),words [j] .begin(),toupper);'而不是'i'循環。 – 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; 
    } 
} 

r = toupper(r[i]);,要覆蓋r是長度爲1所以,你的內心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

你的每一個字的處理是錯誤的:

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

你真正需要的是隻修改的第一個字母:

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

作爲simplfication,你的循環:

std::vector<std::string>::size_type size; 
size = words.size(); 
for (int j = 0; j != size; j++) { 
    std::string &r = words[j]; 

可以更簡潔:

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; 
} 

注意: - 這會對傳入的字符串進行全部字符串操作。如果您嘗試在字符串中執行特定字符,你可能需要做一些不同的事情,但這是如何使用<algorithm>'sstd::transform()::toupper::tolower::tolower