比较C ++中的两个字符串
在这里,我们将看到如何在C++中比较两个字符串。C++具有字符串类。它compare()在标准库中还具有比较字符串的功能。此函数一个接一个地检查字符串字符,如果存在某些不匹配,则返回非零值。让我们看一下代码以获得更好的主意。
示例
#include<iostream>
using namespace std;
void compareStrings(string s1, string s2) {
int compare = s1.compare(s2);
if (compare != 0)
cout << s1 << " is not equal to "<< s2 << endl;
else if(compare == 0)
cout << "Strings are equal";
if (compare > 0)
cout << s1 << " is greater than "<< s2 << " difference is: " << compare << endl;
else if(compare < 0)
cout << s2 << " is greater than "<< s1 << " difference is: " << compare << endl;
}
int main() {
string s1("hello");
string s2("helLo");
compareStrings(s1, s2);
}输出结果
hello is not equal to helLo hello is greater than helLo difference is: 1
热门推荐
10 正月十五简短谜语祝福语
11 生日祝福语简短致表哥
12 学生励志祝福语大全简短
13 今天孩子高考祝福语简短
14 霸气新年祝福语大全简短
15 朋友马上生产祝福语简短
16 晚安好梦祝福语简短
17 预祝中秋简短的祝福语
18 侄儿高考试祝福语简短