为了账号安全,请及时绑定邮箱和手机立即绑定

strcmp函数、strcpy函数在c语言中的作用?

strcmp函数、strcpy函数在c语言中的作用?

C
慕神8447489 2019-02-04 09:05:08
strcmp函数、strcpy函数在c语言中的作用

5 回答

?
MMMHUHU

TA贡献1834条经验 获得超8个赞

strcmp函数是比较两个字符串的大小,返回比较的结果。一般形式是:
i=strcmp(字符串,字符串);
①字符串1小于字符串2,strcmp函数返回一个负值;
②字符串1等于字符串2,strcmp函数返回零;
③字符串1大于字符串2,strcmp函数返回一个正值;
strcpy函数用于实现两个字符串的拷贝。一般形式是:
strcpy(字符中1,字符串2)
其中,字符串1必须是字符串变量,而不能是字符串常量。strcpy函数把字符串2的内容完全复制到字符串1中,而不管字符串1中原先存放的是什么。复制后,字符串2保持不变。

查看完整回答
反对 回复 2019-03-22
?
郎朗坤

TA贡献1921条经验 获得超9个赞

Example of strcmp

/* STRCMP.C */

#include <string.h>
#include <stdio.h>

char string1[] = "The quick brown dog jumps over the lazy fox";
char string2[] = "The QUICK brown dog jumps over the lazy fox";

void main( void )
{
char tmp[20];
int result;
/* Case sensitive */
printf( "Compare strings:\n\t%s\n\t%s\n\n", string1, string2 );
result = strcmp( string1, string2 );
if( result > 0 )
strcpy( tmp, "greater than" );
else if( result < 0 )
strcpy( tmp, "less than" );
else
strcpy( tmp, "equal to" );
printf( "\tstrcmp: String 1 is %s string 2\n", tmp );
/* Case insensitive (could use equivalent _stricmp) */
result = _stricmp( string1, string2 );
if( result > 0 )
strcpy( tmp, "greater than" );
else if( result < 0 )
strcpy( tmp, "less than" );
else
strcpy( tmp, "equal to" );
printf( "\t_stricmp: String 1 is %s string 2\n", tmp );
}

Output

Compare strings:
The quick brown dog jumps over the lazy fox
The QUICK brown dog jumps over the lazy fox

strcmp: String 1 is greater than string 2
_stricmp: String 1 is equal to string 2

Example of Strcpy

/* STRCPY.C: This program uses strcpy
* and strcat to build a phrase.
*/

#include <string.h>
#include <stdio.h>

void main( void )
{
char string[80];
strcpy( string, "Hello world from " );
strcat( string, "strcpy " );
strcat( string, "and " );
strcat( string, "strcat!" );
printf( "String = %s\n", string );
}

Output

String = Hello world from strcpy and strcat!



查看完整回答
反对 回复 2019-03-22
?
繁华开满天机

TA贡献1816条经验 获得超4个赞

strcmp是比较2个字符串,如果一样的话,就等于0.如果第一个大于第二个就为1,都则就为-1.
strcpy是复制字符串。将达尔戈字符串复制到第一个中去。

查看完整回答
反对 回复 2019-03-22
?
饮歌长啸

TA贡献1951条经验 获得超3个赞

这两个函数都是字符串操作函数。strcmp(char *str1,char *str2)是比较两个字符串,如果str1<str2返回负数,str1=str2返回0, str1>str2返回正数。strcpy(char *str1,char *str2)是复制字符串str2的内容到str1中。

查看完整回答
反对 回复 2019-03-22
?
守候你守候我

TA贡献1802条经验 获得超10个赞

strcmp 对2个字符串str1,str2进行比较 是一个字符一个字符的进行比较
返回结果 大小比较
<0 str1 小于str2
= 0 str1 等于str2
> 0 str1 大于str2
strcpy (str2,str1) 是复制字符str1 到str2 并且在字符串str2后面加字符串结束符'\0'

查看完整回答
反对 回复 2019-03-22

添加回答

代码语言

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信