小弟C++有問題想要請各位大大幫我解題 關於STRING的題目?

小弟C++有問題想要請各位大大幫我解題

如下:

You know that the == operator can be used to test if two string objects are equal.

However, you will recall that they are not considered equal, even when they hold the exact

same letters, if the cases of any letters are different. So, for example, if name1 = "Jack"

and name2 = "JACK", they are not considered the same. Write a program that asks the

user to enter two names and stores them in string objects. It should then report whether

or not, ignoring case, they are the same.

To help the program accomplish its task, it should use two functions in addition to main,

upperCaseIt() and sameString(). Here are their function headers.

string upperCaseIt(string s)

Boolean sameString (string s1, string s2)

The sameString function, which receives the two strings to be compared, will need to call

upperCaseIt for each of them before testing if they are the same. The upperCaseIt

function should use a loop so that it can call the toupper function for every character in

the string it receives before returning it back to the sameString function.

以上是課本的題目 但是由於才剛學而已 又想要看看解答來了解題目 還有答案

不知各位大大是否能提供程式碼參考QQ

1 個解答

評分
  • 4 年前
    最佳解答

    // super easy: 超簡易的.小弟弟若是看不懂,就應該發問啊!

    #include<ctype.h>

    #include<iostream>

    using namespace std;

    bool sameStr(

    const string s0,

    const string s1

    ){

    int i;

    char c0, c1;

    for(i=0; (c0=toupper((int)s0[i])) == (c1=toupper((int)s1[i])) && c0; ++i);

    return c0==c1;

    }

    int main(void){

    cout << "compare result is " << (sameStr("abc","AbC") ? "true" : "false") <<endl;

    cout << "compare result is " << (sameStr("aBc","AbC") ? "true" : "false") <<endl;

    cout << "compare result is " << (sameStr("aBcx","AbC") ? "true" : "false") <<endl;

    return 0;

    }

    • 請教別人,是要求"指點迷津"
      人說"舉一魚必以山芋反"
      你是說我要舉四魚嗎?

還有問題?馬上發問,尋求解答。