比较C中两个设置位置(整数值)之间两个字符串的两个子字符串
Comparing two Sub-Strings of two Strings between two set positions (integer values) in C
我有两个字符串,想比较它们,但是是在两个变量之间。示例:字符串 a = "Whosebug" 和字符串 b = "stacknotoverflow"。我想检查两个字符串的字符串 a 的第 5 个位置(int = 5 到 int = 9)和字符串 b 的第 8 个位置(int = 8 到 int = 12)之间的每个字符是否相同。谁能帮我解决这个简单的问题?
您可以使用 strncmp
函数来比较两个字符串的最大字符数。要在字符串中间开始比较,您需要传入数组元素的地址以开始比较。
例如:
if (strncmp(&string1[4], &string2[4], 4) == 0) {
printf("characters 5 - 8 are the same\n");
} else {
printf("characters 5 - 8 are not the same\n");
}
假设你说的是 C。
https://linux.die.net/man/3/strcmp
该函数用于比较C语言中的字符串
int result = strcmp(str1, str2);
printf("Compare result: %d", result);
我有两个字符串,想比较它们,但是是在两个变量之间。示例:字符串 a = "Whosebug" 和字符串 b = "stacknotoverflow"。我想检查两个字符串的字符串 a 的第 5 个位置(int = 5 到 int = 9)和字符串 b 的第 8 个位置(int = 8 到 int = 12)之间的每个字符是否相同。谁能帮我解决这个简单的问题?
您可以使用 strncmp
函数来比较两个字符串的最大字符数。要在字符串中间开始比较,您需要传入数组元素的地址以开始比较。
例如:
if (strncmp(&string1[4], &string2[4], 4) == 0) {
printf("characters 5 - 8 are the same\n");
} else {
printf("characters 5 - 8 are not the same\n");
}
假设你说的是 C。
https://linux.die.net/man/3/strcmp
该函数用于比较C语言中的字符串
int result = strcmp(str1, str2);
printf("Compare result: %d", result);