c++ 中如何比较两个string是否一样?简单写个例子。

来源:百度知道 编辑:UC知道 时间:2024/06/06 11:16:53
主意是比较,楼上给我写的是拷贝了。

int strcmp(s1, s2)
register const char *s1, *s2;
{
while (*s1 == *s2++)
if (*s1++ == 0)
return (0);
return (*(const unsigned char *)s1 - *(const unsigned char *)(s2 - 1));
}

我学C的,给你个C的strcmp()函数吧
#include <string.h>
#include <stdio.h>

int main(void)
{
char *buf1 = "aaa", *buf2 = "bbb", *buf3 = "ccc";
int ptr;

ptr = strcmp(buf2, buf1);
if (ptr > 0)
printf("buffer 2 is greater than buffer 1\n");
else
printf("buffer 2 is less than buffer 1\n");

ptr = strcmp(buf2, buf3);
if (ptr > 0)
printf("buffer 2 is greater than buffer 3\n");
else
printf("buffer 2 is less than buffer 3\n");

return 0;