sunwengang blog

developer | android display/graphics

  1. 1. strcmp函数比较字符串大小
  2. 2. strstr函数比较字符串是否相同或者存在包含关系

今天碰到一个细节bug,使用strcmp来比较两个字符串是否完全相同。但是忽略了一个问题,如果存在一个字符串包含在另一个字符串呢?此时就会发现需要用strstr函数。

strcmp函数比较字符串大小

设这两个字符串为str1、str2,

  • 若str1 == str2,则返回零;
  • 若str1 < str2,则返回负数;
  • 若str1 > str2,则返回正数。

测试类:

testFunc_strcmp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include<iostream>
#include <cstring>

#define PROPERTY_VALUE_MAX 92

int main() {
using namespace std;

char char1[PROPERTY_VALUE_MAX] = {0};
char char2[PROPERTY_VALUE_MAX] = {0};

bool con = true;
char isCon[20] = "y";

while (con) {
cout << "Compare two char by func strcmp(char1, char2):" <<endl;
cout << "Enter char1: ";
cin.getline(char1, PROPERTY_VALUE_MAX);

cout << "Enter char2: ";
cin.getline(char2, PROPERTY_VALUE_MAX);

cout << "Result: strcmp(" << char1 << ", " << char2 << "): " <<endl;
cout << strcmp(char1, char2) <<endl;

cout << "Continue? y/n" <<endl;
cin.getline(isCon, 20);
if(strcmp(isCon, "n") == 0) {
con = false;
}
}
return 0;
}

运行:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
Compare two char by func strcmp(char1, char2):
Enter char1: sun
Enter char2: wen
Result: strcmp(sun, wen):
-4
Continue? y/n
y
Compare two char by func strcmp(char1, char2):
Enter char1: wen
Enter char2: gan
Result: strcmp(wen, gan):
16
Continue? y/n
y
Compare two char by func strcmp(char1, char2):
Enter char1: wizzie
Enter char2: wizzie
Result: strcmp(wizzie, wizzie):
0
Continue? y/n
y
Compare two char by func strcmp(char1, char2):
Enter char1: wizzie_test
Enter char2: wizzie
Result: strcmp(wizzie_test, wizzie):
95
Continue? y/n
n

strstr函数比较字符串是否相同或者存在包含关系

如果两个字符串可能存在相同,并且可能会有包含关系,则需要使用strstr函数来比较字符串。如果不包含(或相同),则返回NULL。

测试类:

testFunc_strstr.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include<iostream>
#include <cstring>

#define PROPERTY_VALUE_MAX 92

int main() {
using namespace std;

char char1[PROPERTY_VALUE_MAX] = {0};
char char2[PROPERTY_VALUE_MAX] = {0};

bool con = true;
char isCon[20] = "y";

cout << "*** Determine whethe char2 is in char1. ***" << endl;

while (con) {
cout << "Input two char by func strstr(char1, char2):" <<endl;
cout << "Enter char1: ";
cin.getline(char1, PROPERTY_VALUE_MAX);

cout << "Enter char2: ";
cin.getline(char2, PROPERTY_VALUE_MAX);

cout << "Result: strstr(" << char1 << ", " << char2 << "): " <<endl;
if (strstr(char1, char2) != NULL)
cout << "Success: char2 is in char1!" << endl;
else
cout << "Error: char2 is not in char1!" << endl;

cout << "Continue? y/n" <<endl;
cin.getline(isCon, 20);
if(strcmp(isCon, "n") == 0) {
con = false;
}
}
return 0;
}

运行:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
*** Determine whethe char2 is in char1. ***
Input two char by func strstr(char1, char2):
Enter char1: sun
Enter char2: sun
Result: strstr(sun, sun):
Success: char2 is in char1!
Continue? y/n
y
Input two char by func strstr(char1, char2):
Enter char1: sunwengang
Enter char2: wen
Result: strstr(sunwengang, wen):
Success: char2 is in char1!
Continue? y/n
y
Input two char by func strstr(char1, char2):
Enter char1: sun
Enter char2: sunwengang
Result: strstr(sun, sunwengang):
Error: char2 is not in char1!

本文作者 : sunwengang
本文使用 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0) 协议
本文链接 : https://alonealive.github.io/Blog/2020/04/14/2020/200413_cpp_strstrTostrcmp/

本文最后更新于 天前,文中所描述的信息可能已发生改变