字串的操作函式

2021-10-07 12:48:54 字數 1474 閱讀 2092

1.應用於查詢的find()函式

#include

#include

using namespace std;

int main()12

3456

78910

1112

1314

152.字串(substr()函式)

#include

#include

using namespace std;

int main()12

3456

78910

1112

結果:3.替換

#include

#include

using namespace std;

int main()12

3456

78910

1112

結果:4.插入:insert()函式

#include

#include

using namespace std;

int main()12

3456

78910

11結果:

#include

#include

using namespace std;

int main()12

3456

78910

1112

1314

15結果:

5.交換字元:swap()函式

#include

#include

using namespace std;

int main()12

3456

78910

1112

1314

1516

結果:6.字串大小

size()函式和length()函式

#include

#include

using namespace std;

int main()12

3456

78910

1112

13結果:

7.字串比較函式:compare()

#include

#include

using namespace std;

int main()

{string str1,str2;

cin>>str1>>str2;

//結果為負數,0,正數

cout<<「字串比較結果:」strcat(s1,s2) :連線s2到s1的末尾

strlen(s1) :返回字串s1的長度

strcmp(s1,s2) :若s1和s2是相同的,則返回0,s1< s2,返回值小於0,若s1>s2,返回值大於0

strchr(s1,ch) :返回乙個指標,指向字串s1中字元ch第一次出現的位置

strstr(s1,s2) : 返回乙個指標,指向字串s1中字串s2的第一次出現位置

字串操作函式

char cdecl strcat char dst,const char src wchar t cdecl wcscat wchar t dst,const wchar t src char cdecl strcpy char dst,const char src wchar t cdecl w...

字串操作函式

字串主要是了解標準庫中的函式的用法及注意事項 1 strlen char const str 函式的返回值型別為size t,這個型別是在標頭檔案stddef.h中定義的,它是乙個無符號整型型別,注意這點兒很重要,例如下面的判斷語句將永遠返回真 if strlen str1 strlen str2 ...

字串操作函式

1.strlen 計算字串長度 int strlen const char string 2.strcpy 字串拷貝.char strcpy char destination,const char source 3.strcat 字串的連線.char strcat char target,const...