c++为什么已知 char* s1= "123", *s2="34";cout<<strcmp(s1,s2);的输出结果为-1
时间: 2024-04-07 10:30:38 浏览: 26
C++中的strcmp函数用于比较两个字符串,它返回一个整数值,表示两个字符串的关系。
在给定的例子中,s1和s2分别指向字符串"123"和"34"。strcmp函数会逐个比较两个字符串中对应位置的字符。在比较过程中,如果对应位置的字符相等,则继续比较下一个位置的字符;如果对应位置的字符不相等,则返回它们的ASCII码差值。
在这个例子中,第一个字符'1'和'3'不相等,其ASCII码差值为-2。因此strcmp返回的值为-2,即-1的补码表示。这意味着s1比s2小。
注意,这里的字符串比较是基于字符的ASCII码值进行比较的。所以在比较字符串时,我们需要确保字符串以空字符('\0')结尾。否则,结果可能是不确定的。
相关问题
实现函数int compare(char*s1,char*s2),该函数功能:比较两个字符串大小,再编写主程序调用函数,在屏幕上输出比较结果
可以使用strcmp函数进行字符串比较。函数原型为:
```
int strcmp(const char *s1, const char *s2);
```
该函数会比较两个字符串 s1 和 s2,如果 s1 < s2,返回负数;如果 s1 > s2,返回正数;如果 s1 = s2,返回 0。
可以根据这个函数编写compare函数,代码如下:
```c++
#include <iostream>
#include <cstring>
using namespace std;
int compare(char* s1, char* s2) {
return strcmp(s1, s2);
}
int main() {
char s1[100], s2[100];
cout << "请输入字符串1:";
cin >> s1;
cout << "请输入字符串2:";
cin >> s2;
int result = compare(s1, s2);
if (result < 0) {
cout << "字符串1小于字符串2" << endl;
} else if (result > 0) {
cout << "字符串1大于字符串2" << endl;
} else {
cout << "字符串1等于字符串2" << endl;
}
return 0;
}
```
在主函数中,先输入两个字符串,然后调用 compare 函数进行比较,最后根据比较结果输出对应的信息。
#include <iostream> #include <cstring> #include <string> using namespace std; class String { private: char *p; public: // 你的代码在这里 } int main() { String s1, s2; s1.input(); s2.input(); s1.display(); s2.display(); cout << (s1 > s2) << endl; cout << (s1 < s2) << endl; cout << (s1 == s2) << endl; return 0; } 输入 两个不带空格的字符串 输出 按样例要求输出 样例输入 Copy Hello Book 样例输出 Copy Hello Book 1 0 0
下面是实现 String 类的代码:
```c++
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
class String {
private:
char *p;
public:
String() { // 构造函数
p = NULL;
}
~String() { // 析构函数
if (p) {
delete[] p;
}
}
void input() { // 输入字符串
string str;
cin >> str;
p = new char[str.length() + 1];
strcpy(p, str.c_str());
}
void display() { // 输出字符串
cout << p << endl;
}
bool operator>(const String &s) { // 大于运算符
return strcmp(p, s.p) > 0;
}
bool operator<(const String &s) { // 小于运算符
return strcmp(p, s.p) < 0;
}
bool operator==(const String &s) { // 等于运算符
return strcmp(p, s.p) == 0;
}
};
int main() {
String s1, s2;
s1.input();
s2.input();
s1.display();
s2.display();
cout << (s1 > s2) << endl;
cout << (s1 < s2) << endl;
cout << (s1 == s2) << endl;
return 0;
}
```
该程序中,String 类有一个指针成员变量 `p`,它指向一个字符串。构造函数将 `p` 初始化为 `NULL`,析构函数释放 `p` 指向的内存。`input()` 函数从标准输入读入一个字符串,将其存放到 `p` 指向的内存中。`display()` 函数将 `p` 指向的字符串输出到标准输出中。`operator>`, `operator<`, `operator==` 分别重载了大于、小于和等于运算符,它们使用 `strcmp` 函数比较两个字符串的大小关系。在 `main()` 函数中,先创建两个 String 类对象 s1 和 s2,分别调用它们的 `input()` 函数输入字符串,再依次调用它们的 `display()` 函数输出字符串,最后调用它们的比较运算符函数输出比较结果。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)