Str库系列函数合集(strlen、strcpy、strcmp、strcat、strchr等)
生活随笔
收集整理的這篇文章主要介紹了
Str库系列函数合集(strlen、strcpy、strcmp、strcat、strchr等)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
關于Str函數,網上五花八門,使初學者很容易迷失方向,筆者在這里做一個常用函數的總結。希望對讀者起到些許幫助。
后續會持續更新特殊函數~
想了解mem系列函數的,請猛戳這里→mem系列函數
代碼
//頭文件:#include<string.h> #include <iostream> #include <string.h> #include <string> #include <cstdio>using namespace std ;int main() {char a[10] = "123456789" ; //如此定義只能定義10-1個。最后一個放\0 ,最后一個需自己定義。 char b[10] = "qwertyuio" ;// 1、strlen (求長度) // cout << strlen(a1) ; // 2、strcpy 、strncpy (復制) // strcpy(a,b) ; // strncpy(a,b+3,3) ; 意思是將b字符串的第4-7個值賦給a字符串。// 3、strcmp 、strncmp (比較大小) // cout << strcmp(a,b) ; // cout << strncmp(a,b+3,3) ; 同上// 4、strcat 、 strncat (字符串連接) // strcat(a,b) ; // strncat(a,b+3,3) ; 同上// 5、strchr 、 strrchr (查找、反向查找)//一定、必須要用以下三行代碼才能返回該字符的位置。 strrchr同理 // char *p ; // p = (char*)(strchr(a,'5')) ; // cout << "要查詢的數在數組第" <<(int)(p-a+1) << "位。" << endl ; //6、特殊查找string a , b ;cin >> a >> b ; // 返回b串中第一個與a串不匹配字符的位置 // int found = a.find_first_not_of(b) ; // int found = a.find_first_not_of(b,1) ; //從b串的第二個開始找 // int found = a.find_first_not_of(b,1,4) ; //從b串的第二個開始找,到第5個結束 cout << found+1 ; //返回值需+1.// 返回b串中第一個與a串匹配字符的位置 // int found = a.find_first_of(b) ; return 0 ;} 超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生總結
以上是生活随笔為你收集整理的Str库系列函数合集(strlen、strcpy、strcmp、strcat、strchr等)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: UVa1588 | 算法竞赛入门经典(第
- 下一篇: mem库系列函数合集(memset、me