简体   繁体   English

如何比较布尔函数中的两个字符串

[英]How to compare two strings in a Boolean function

The following is from a project description for a class:以下来自一个类的项目描述:

"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. “您知道 == 运算符可用于测试两个字符串对象是否相等。但是,您会记得,如果任何字母的大小写不同,即使它们包含完全相同的字母,它们也不会被视为相等。因此,例如,如果 name1 = "Jack" 和 name2 = "JACK",则认为它们不相同。编写一个程序,要求用户输入两个名称并将它们存储在字符串对象中。然后应报告是否,忽略大小写,它们是相同的。

To help the program accomplish this task, it should use two functions in addition to main, upperCaseIt( ) and sameString( ).为了帮助程序完成这个任务,它应该使用除 main 之外的两个函数,upperCaseIt() 和 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.接收要比较的两个字符串的 sameString 函数在测试它们是否相同之前需要为它们中的每一个调用 upperCaseIt 。 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." upperCaseIt 函数应该使用一个循环,这样它就可以在将字符串返回给 sameString 函数之前,为它接收到的字符串中的每个字符调用 toupper 函数。”

Here is my code:这是我的代码:

My code我的代码

What do I need to do for the Boolean to compare n1 and n2 in order to determine whether or not they are equal?我需要为布尔值做些什么来比较 n1 和 n2 以确定它们是否相等?

The gist of this problem is that you want to perform a case-insensitive comparison of two strings.这个问题的要点是您想要对两个字符串执行不区分大小写的比较。 The hints that have been given are pointing the way:已经给出的提示指明了方向:

bool sameString (string s1, string s2)
{
    return upperCaseIt(s1) == upperCaseIt(s2);
}

So by comparing an ALL CAPS version of the two strings, any case differences are lost.因此,通过比较两个字符串的 ALL CAPS 版本,任何大小写差异都将丢失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM