对比两个字符串数组完全一样的代码
时间: 2024-03-26 07:34:18 浏览: 19
可以使用C语言标准库函数strcmp()来比较两个字符串数组是否完全一样。strcmp()函数的原型如下:
```c
int strcmp(const char *s1, const char *s2);
```
其中,参数s1和s2是要比较的两个字符串数组。如果两个字符串相等,则返回值为0;如果s1大于s2,则返回值大于0;如果s1小于s2,则返回值小于0。
下面是一段使用strcmp()函数比较两个字符串数组是否完全一样的示例代码:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello, world!";
char str2[] = "Hello, world!";
if (strcmp(str1, str2) == 0) {
printf("The two strings are exactly the same.\n");
} else {
printf("The two strings are different.\n");
}
return 0;
}
```
在这个示例代码中,我们定义了两个字符串数组str1和str2,它们的内容完全一样。然后使用strcmp()函数比较它们,如果返回值为0,则输出"The two strings are exactly the same.",否则输出"The two strings are different."。在这个示例中,由于两个字符串数组完全一样,因此输出"The two strings are exactly the same."。