linux c语言字符串比较问题
答案:2 悬赏:30 手机版
解决时间 2021-04-03 11:28
- 提问者网友:最爱你的唇
- 2021-04-03 04:06
linux c语言字符串比较问题
最佳答案
- 五星知识达人网友:平生事
- 2021-04-03 05:37
linux中,c语言字符串比较采用strcmp()函数
在linux命令行下,man strcmp,可以看到函数说明:
$ man strcmp
...
NAME
strcmp, strncmp - compare two strings
SYNOPSIS
#include
int strcmp(const char *s1, const char *s2);
DESCRIPTION
The strcmp() function compares the two strings s1 and s2. It returns an integer less than, equal to, or greater than zero if s1 is found, respectively, to be less than, to match, or be greater than s2.
strcmp函数比较两个字符串s1和s2. 函数返回小于0,等于0,或大于0的整数,分别对应s1
#include
#include
int main()
{
char *s1 = "good" , *s2="...this is good" ;
int len=strlen(s2);
if ( len >= 4 ) //如果串长超过4位,则移动指针到最后四位的位置
s2 =s2+len-4;
printf("s2=%s
", s2 ); //输出移位后的字符串内容
printf("compare s1,s2=%d
", strcmp(s1,s2) ); //输出0,表示相同
return 0;
}
在linux命令行下,man strcmp,可以看到函数说明:
$ man strcmp
...
NAME
strcmp, strncmp - compare two strings
SYNOPSIS
#include
int strcmp(const char *s1, const char *s2);
DESCRIPTION
The strcmp() function compares the two strings s1 and s2. It returns an integer less than, equal to, or greater than zero if s1 is found, respectively, to be less than, to match, or be greater than s2.
strcmp函数比较两个字符串s1和s2. 函数返回小于0,等于0,或大于0的整数,分别对应s1
#include
#include
int main()
{
char *s1 = "good" , *s2="...this is good" ;
int len=strlen(s2);
if ( len >= 4 ) //如果串长超过4位,则移动指针到最后四位的位置
s2 =s2+len-4;
printf("s2=%s
", s2 ); //输出移位后的字符串内容
printf("compare s1,s2=%d
", strcmp(s1,s2) ); //输出0,表示相同
return 0;
}
全部回答
- 1楼网友:逃夭
- 2021-04-03 06:03
用指针分别只想两个字符串的首地址,然后后面的指针加4,在用strcmp( ),进行比较!char *p1="good".char *p2="this is good". char *p3. p3=*(p+4)
strcmp (char *p1,char *p3).
strcmp (char *p1,char *p3).
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯