2015-03-31 47 views
-2

我不允許使用toupper()如何將字符串轉換爲大寫C++

這些是我的函數,一個函數必須通過引用傳遞字符串。

void Uppercase(string x) 
{ 
int y = 0; 
while (y < x.length) 
{ 
    x[y] = x[y] - 32; 
    y++; 
} 
} 

void uppercase(string &x) 
{ 
int y = 0; 
while (y < x.length) 
{ 
    x[y] = x[y] - 32; 
    y++; 
} 

我至少有正確的想法嗎?

我得到這個錯誤,當我建立它....

錯誤5錯誤C2446: '<':無論從「無符號整型(__thiscall的std :: basic_string的,性病::分配器轉換> :: * )(void)throw()const'to'int'c:\ users \ edwingomez \ documents \ visual studio 2013 \ projects \ homework \ homework \ homework6.cpp 18 1作業

+0

32是一個幻數,應至少用一個常數代替。 – chris 2015-03-31 16:47:23

+0

您需要檢查字符是否在正確的範圍'a' - 'z'中,並且第一個函數不會做任何事情,字符串在內部被修改,但結果是看不到的。 – NetVipeC 2015-03-31 16:47:53

+0

你遺漏了括號:'x.length()'。 – molbdnilo 2015-03-31 16:59:03

回答

0

這是正確的。但是你應該注意在輸入上添加檢查。您可以檢查這些字符是否爲小寫字母。

if (x[y] <= 'z') && (x[y] >= 'a') 
    x[y] = x[y] - 32; 

爲了增強可讀性,你應該'a' - 'A'取代32

另外,你的第一個函數什麼都不做。它創建一個字符串的副本。請執行所需的操作。然後丟棄它,因爲你沒有返回它,並且函數結束。

+0

也許你應該更換123和96? – Slava 2015-03-31 16:50:16

+0

@Slava:謝謝你。 – therainmaker 2015-03-31 16:51:06

1

std::transform功能與lambda expression是做到這一點的一種方法:

std::string s = "all uppercase"; 
std::transform(std::begin(s), std::end(s), std::begin(s), 
    [](const char& ch) 
    { return (ch >= 'a' && ch <= 'z' ? ch - 32 : ch) }); 
std::cout << s << '\n'; 

應該輸出

ALL UPPERCASE 

但是,如果系統正在使用的ASCII字符集(或ch - 32表達式會給出意想不到的結果)。

+0

「不允許在問題 – 2015-03-31 16:53:33

+0

中使用toupper()」對於那些沒有C++ 11的人,應該在'toupper'中包含'transform'。 – 2015-03-31 17:20:36

+0

'touper'的'transform'可能很容易導致未定義的行爲,因爲'toupper'被指定爲UB,如果參數不能表示爲'unsigned char'(其中負值爲'char' ) – 2015-03-31 18:08:15

0

示例代碼:

  • 隨着一系列正確的檢查。
  • 使用C++ 11版本(使用範圍for循環)。

注意:生產版本應該使用toupper。但是OP明確表示不能使用。

代碼:

#include <iostream> 

const char upper_difference = 32; 

void uppercaseCpp11(std::string& str) { 
    for (auto& c : str) { 
     if (c >= 'a' && c <= 'z') 
      c -= upper_difference; 
    } 
} 

std::string UppercaseCpp11(std::string str) { 
    uppercaseCpp11(str); 
    return str; 
} 

void uppercase(std::string& x) { 
    for (unsigned int i = 0; i < x.size(); ++i) { 
     if (x[i] >= 'a' && x[i] <= 'z') 
      x[i] -= upper_difference; 
    } 
} 

std::string Uppercase(std::string x) { 
    uppercase(x); 
    return x; 
} 

int main() { 
    std::cout << Uppercase("stRing") << std::endl; 
    std::string s1("StrinG"); 
    uppercase(s1); 
    std::cout << s1 << std::endl; 

    std::cout << UppercaseCpp11("stRing") << std::endl; 
    std::string s2("StrinG"); 
    uppercaseCpp11(s2); 
    std::cout << s2 << std::endl; 

    return 0; 
} 
0
string x = "Hello World"; 
    char test[255]; 
    memset(test, 0, sizeof(test)); 
    memcpy(test, x.c_str(), x.capacity()); 
    for (int i = 0; i < x.capacity(); i++) 
    { 
     if (test[i] > 96 && test[i] < 123) 
      test[i] = test[i] - 32; 
    } 

    x.assign(test); 
3

由於使用toupper限制令我傻了,適得其反,我可能會以一種方式這樣的分配隨後的禁令,而側面的信件作出答覆-stepping其意圖不言自明,是這樣的:

#include <locale> 
#include <iostream> 

struct make_upper : private std::ctype <char> { 
    void operator()(std::string &s){ 
     do_toupper(&s[0], &s[0]+s.size()); 
    } 
}; 

int main() { 
    std::string input = "this is some input."; 
    make_upper()(input); 
    std::cout << input; 
} 

這當然會產生預期的結果:

THIS IS SOME INPUT. 

與我在此處發佈的其他答案不同,這仍然具有使用toupper的所有正常優點,例如在使用EBCDIC而不是ASCII編碼的計算機上工作。

與此同時,我想我應該承認(例如)當我在小學五年級的時候,我曾被告知要做「寫作」,我不會在課堂上講話「,50次。」所以我翻過一篇文章,其中有一句話寫着:「我不會在課堂上講50次」。瑪麗艾倫姐妹(是的,天主教學校)並不特別高興。

相關問題