用strcmp()比较字符串
Comparing strings with strcmp()
代码为:
#include <iostream>
int main() {
const char *str1{"Jill"};
const char *str2{"Jacko"};
int result{std::strcmp(str1, str2)};
if(result < 0) {
std::cout << str1 << " is less than " << str2 << '.' << std::endl;
} else if(result == 0) {
std::cout << str1 << " is equal to " << str2 << '.' << std::endl;
} else {
std::cout << str1 << " is greater than " << str2 << '.' << std::endl;
}
return 0;
}
输出:Jill大于Jacko。
我的新手问题是:为什么它返回else语句块来确认Jill大于Jacko?strcmp中使用什么方法来比较这些字符串?是按总字节数,还是按字符数?
首先比较'J'和'J',它们是相等的。然后将i与a进行比较,'i'> 'a'>
因此,
Jill is greater than Jacko
相关文章:
- 视觉C++使用 map 来比较字符串中的每个单词
- 我正在尝试从输入文件中读取,然后使用它们的子字符串比较字符串的特定部分
- 比较字符串的 GetLine 工作一次,然后比较之后不起作用
- C++使用关系运算符比较字符串
- 通过默认复制构造函数比较 C++ 字符串是否会影响性能,原因为何?
- C++ TCP 服务器比较字符串失败
- 如何在不复制的情况下比较字符串的一部分?
- 比较 C++ 字符串打印中的两个整数数组
- 如何比较字符串形式的临时变量
- std::out_of_range 比较字符串中的符号时出错
- 比较字符串大小写不区分的简单方法是什么?
- 比较字符串忽略 NUL
- 比较字符串(arduino)时出现问题
- 如何在C++中比较字符串
- 比较字符串对象等于向量中的元素
- C 比较字符串操作速度
- 需要比较字符串的某些元素
- 比较字符串 .c_str() 和普通字符串的差异
- C 比较字符串的字符
- 通过使用两个不同的队列比较字符串