亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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
  • 5 回答
  • 0 關注
  • 1555 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號