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 關注
- 1555 瀏覽
添加回答
舉報