5 回答

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!

TA贡献1816条经验 获得超4个赞
strcmp是比较2个字符串,如果一样的话,就等于0.如果第一个大于第二个就为1,都则就为-1.
strcpy是复制字符串。将达尔戈字符串复制到第一个中去。
没有找到匹配的内容?试试慕课网站内搜索吧

TA贡献1951条经验 获得超3个赞
这两个函数都是字符串操作函数。strcmp(char *str1,char *str2)是比较两个字符串,如果str1<str2返回负数,str1=str2返回0, str1>str2返回正数。strcpy(char *str1,char *str2)是复制字符串str2的内容到str1中。

TA贡献1802条经验 获得超10个赞
strcmp 对2个字符串str1,str2进行比较 是一个字符一个字符的进行比较
返回结果 大小比较
<0 str1 小于str2
= 0 str1 等于str2
> 0 str1 大于str2
strcpy (str2,str1) 是复制字符str1 到str2 并且在字符串str2后面加字符串结束符'\0'
- 5 回答
- 0 关注
- 1513 浏览
添加回答
举报