void crazyCaps(string& str){
size_t i = 0;
size_t strLength = str.length(); // this line violates the rule as mentioned in the title
while (i < strLength) {
if (i % 2 == 0) {
if (str[i] >= 'A' && str[i] <= 'Z') // this line violates the rule as mentioned in the title
str[i] += 32;
}
else {
if (str[i] >= 'a' && str[i] <= 'z')
str[i] -= 32;
}
i++;
}
我的意見:「嘿!!那裏!」不使用toUpper(),toLower(),length(),at()或[] - 轉換「Hey !! THERE!」 「好吧!好!」
我的輸出結果是:「好的!」!
我可以在不使用toUpper()和toLower()函數的情況下將字符轉換爲大寫和小寫。不過,我仍然使用length()和[]。所以我的問題是你如何轉換「嘿!那裏!」 「好吧!好!」而不使用length()或[]和toUpper()或toLower()。
如果我的問題是不明確的,讓我知道,我會編輯再次。 –
這是面試問題還是家庭作業? –
這是一次學校鍛鍊嗎?如果是這樣,它會教會你做錯事的方式。通過'tolower'和'toupper'功能可以正確地將字符轉換爲大寫或小寫。並非每種語言都是英語。其次,使用'std :: transform'和一個查找表。 – PaulMcKenzie