關於c 的strcmp函式例項講解

2022-09-23 07:36:10 字數 729 閱讀 1115

函式名: strcmp

功 能: 串比較

用 法: int strcmp(char *str1, char *str2);

看asic碼,str1>str2,返回值 > 0;兩串相等,返回0

上面的標頭檔案為 strncasecmp()是忽略大小寫的。

c++ string 型別的比較可以用string的函式compare()

int compare ( const string& str ) const;

int compare ( const char* s ) const;

或直接用==比較// 比較兩個字串比較規則同 c字串比較規則

string str7, str8;

cout << "輸入字串 str7, str8 , 中間用空格格開" << endl;

cin >> str7 >> str8;

if( str7< str8 ) cout << str7 << " 小於 " << str8 << endl;

else if( str7> str8 ) cout << str7 << " 大於 " << str8 << endl;

else cout << str7 << " 等於 " << str8 << endl;可以直接把string型別和c字串型別比較:

string s("abc");

s=="abc" //true,此時c風格字串可以轉換為string型別的,

C語言strcmp函式的筆記

defined in header 在標頭檔案string.h裡,編碼時需要引用此檔案 int strcmp const char lhs,const char rhs 引數為字元指標,後續解釋指標,參考指標筆記 zero iflhsandrhscompare equal.等於 include in...

strcmp函式原型 C語言

編寫函式原型為 int strcmp char s1,char s2 的函式,該函式實現兩個字串的比較。strcmp函式是string compare 字串比較 的縮寫,用於比較兩個字串並根據比較結果返回整數。基本形式為strcmp str1,str2 若str1 str2,則返回零 若str1st...

strcmp函式的實現

6 實現strcmp函式的功能,要求按照先比字元後比長度的方式。include int stringlength char array return arraylength int stringcmp char array1,char array2 else if array1 i if i str...