C語(yǔ)言字符串函數(shù)例子程序大全–string相關(guān)_第1頁(yè)
C語(yǔ)言字符串函數(shù)例子程序大全–string相關(guān)_第2頁(yè)
C語(yǔ)言字符串函數(shù)例子程序大全–string相關(guān)_第3頁(yè)
C語(yǔ)言字符串函數(shù)例子程序大全–string相關(guān)_第4頁(yè)
C語(yǔ)言字符串函數(shù)例子程序大全–string相關(guān)_第5頁(yè)
已閱讀5頁(yè),還剩11頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、C語(yǔ)言字符串函數(shù)例子程序大全 string相關(guān)關(guān)于字符串函數(shù)的應(yīng)用細(xì)則,例子程序 jerny函數(shù)名: stpcpy 功 能: 拷貝一個(gè)字符串到另一個(gè) 用 法: char *stpcpy(char *destin, char *source); 程序例: #include #include int main(void) char string10; char *str1 = abcdefghi; stpcpy(string, str1); printf(%sn, string); return 0; 函數(shù)名: strcat 功 能: 字符串拼接函數(shù) 用 法: char *strcat(char

2、*destin, char *source); 程序例: #include #include int main(void) char destination25; char *blank = , *c = C+, *Borland = Borland; strcpy(destination, Borland); strcat(destination, blank); strcat(destination, c); printf(%sn, destination); return 0; 函數(shù)名: strchr 功 能: 在一個(gè)串中查找給定字符的第一個(gè)匹配之處 用 法: char *strchr(

3、char *str, char c); 程序例: #include #include int main(void) char string15; char *ptr, c = r; strcpy(string, This is a string); ptr = strchr(string, c); if (ptr) printf(The character %c is at position: %dn, c, ptr-string); else printf(The character was not foundn); return 0; 函數(shù)名: strcmp 功 能: 串比較 用 法: i

4、nt strcmp(char *str1, char *str2); 看Asic碼,str1str2,返回值 0;兩串相等,返回0程序例: #include #include int main(void) char *buf1 = aaa, *buf2 = bbb, *buf3 = ccc; int ptr; ptr = strcmp(buf2, buf1); if (ptr 0) printf(buffer 2 is greater than buffer 1n); else printf(buffer 2 is less than buffer 1n); ptr = strcmp(buf2

5、, buf3); if (ptr 0) printf(buffer 2 is greater than buffer 3n); else printf(buffer 2 is less than buffer 3n); return 0; 函數(shù)名: strncmpi 功 能: 將一個(gè)串中的一部分與另一個(gè)串比較, 不管大小寫 用 法: int strncmpi(char *str1, char *str2, unsigned maxlen); 程序例: #include #include int main(void) char *buf1 = BBB, *buf2 = bbb; int ptr;

6、 ptr = strcmpi(buf2, buf1); if (ptr 0) printf(buffer 2 is greater than buffer 1n); if (ptr 0) printf(buffer 2 is less than buffer 1n); if (ptr = 0) printf(buffer 2 equals buffer 1n); return 0; 函數(shù)名: strcpy 功 能: 串拷貝 用 法: char *strcpy(char *str1, char *str2); 程序例: #include #include int main(void) char

7、string10; char *str1 = abcdefghi; strcpy(string, str1); printf(%sn, string); return 0; 函數(shù)名: strcspn 功 能: 在串中查找第一個(gè)給定字符集內(nèi)容的段 用 法: int strcspn(char *str1, char *str2); 程序例: #include #include #include int main(void) char *string1 = 1234567890; char *string2 = 747DC8; int length; length = strcspn(string1

8、, string2); printf(Character where strings intersect is at position %dn, length); return 0; 函數(shù)名: strdup 功 能: 將串拷貝到新建的位置處 用 法: char *strdup(char *str); 程序例: #include #include #include int main(void) char *dup_str, *string = abcde; dup_str = strdup(string); printf(%sn, dup_str); free(dup_str); return

9、0; 函數(shù)名: stricmp 功 能: 以大小寫不敏感方式比較兩個(gè)串 用 法: int stricmp(char *str1, char *str2); 程序例: #include #include int main(void) char *buf1 = BBB, *buf2 = bbb; int ptr; ptr = stricmp(buf2, buf1); if (ptr 0) printf(buffer 2 is greater than buffer 1n); if (ptr 0) printf(buffer 2 is less than buffer 1n); if (ptr =

10、0) printf(buffer 2 equals buffer 1n); return 0; 函數(shù)名: strerror 功 能: 返回指向錯(cuò)誤信息字符串的指針 用 法: char *strerror(int errnum); 程序例: #include #include int main(void) char *buffer; buffer = strerror(errno); printf(Error: %sn, buffer); return 0; 函數(shù)名: strcmpi 功 能: 將一個(gè)串與另一個(gè)比較, 不管大小寫 用 法: int strcmpi(char *str1, char

11、 *str2); 程序例: #include #include int main(void) char *buf1 = BBB, *buf2 = bbb; int ptr; ptr = strcmpi(buf2, buf1); if (ptr 0) printf(buffer 2 is greater than buffer 1n); if (ptr 0) printf(buffer 2 is less than buffer 1n); if (ptr = 0) printf(buffer 2 equals buffer 1n); return 0; 函數(shù)名: strncmp 功 能: 串比較

12、 用 法: int strncmp(char *str1, char *str2, int maxlen); 程序例: #include #include int main(void) char *buf1 = aaabbb, *buf2 = bbbccc, *buf3 = ccc; int ptr; ptr = strncmp(buf2,buf1,3); if (ptr 0) printf(buffer 2 is greater than buffer 1n); else printf(buffer 2 is less than buffer 1n); ptr = strncmp(buf2,

13、buf3,3); if (ptr 0) printf(buffer 2 is greater than buffer 3n); else printf(buffer 2 is less than buffer 3n); return(0); 函數(shù)名: strncmpi 功 能: 把串中的一部分與另一串中的一部分比較, 不管大小寫 用 法: int strncmpi(char *str1, char *str2); 程序例: #include #include int main(void) char *buf1 = BBBccc, *buf2 = bbbccc; int ptr; ptr = s

14、trncmpi(buf2,buf1,3); if (ptr 0) printf(buffer 2 is greater than buffer 1n); if (ptr 0) printf(buffer 2 is less than buffer 1n); if (ptr = 0) printf(buffer 2 equals buffer 1n); return 0; 函數(shù)名: strncpy 功 能: 串拷貝 用 法: char *strncpy(char *destin, char *source, int maxlen); 程序例: #include #include int main

15、(void) char string10; char *str1 = abcdefghi; strncpy(string, str1, 3); string3 = 0; printf(%sn, string); return 0; 函數(shù)名: strnicmp 功 能: 不注重大小寫地比較兩個(gè)串 用 法: int strnicmp(char *str1, char *str2, unsigned maxlen); 程序例: #include #include int main(void) char *buf1 = BBBccc, *buf2 = bbbccc; int ptr; ptr = st

16、rnicmp(buf2, buf1, 3); if (ptr 0) printf(buffer 2 is greater than buffer 1n); if (ptr 0) printf(buffer 2 is less than buffer 1n); if (ptr = 0) printf(buffer 2 equals buffer 1n); return 0; 函數(shù)名: strnset 功 能: 將一個(gè)串中的所有字符都設(shè)為指定字符 用 法: char *strnset(char *str, char ch, unsigned n); 程序例: #include #include i

17、nt main(void) char *string = abcdefghijklmnopqrstuvwxyz; char letter = x; printf(string before strnset: %sn, string); strnset(string, letter, 13); printf(string after strnset: %sn, string); return 0; 函數(shù)名: strpbrk 功 能: 在串中查找給定字符集中的字符 用 法: char *strpbrk(char *str1, char *str2); 程序例: #include #include

18、int main(void) char *string1 = abcdefghijklmnopqrstuvwxyz; char *string2 = onm; char *ptr; ptr = strpbrk(string1, string2); if (ptr) printf(strpbrk found first character: %cn, *ptr); else printf(strpbrk didnt find character in setn); return 0; 函數(shù)名: strrchr 功 能: 在串中查找指定字符的最后一個(gè)出現(xiàn) 用 法: char *strrchr(ch

19、ar *str, char c); 程序例: #include #include int main(void) char string15; char *ptr, c = r; strcpy(string, This is a string); ptr = strrchr(string, c); if (ptr) printf(The character %c is at position: %dn, c, ptr-string); else printf(The character was not foundn); return 0; 函數(shù)名: strrev 功 能: 串倒轉(zhuǎn) 用 法: ch

20、ar *strrev(char *str); 程序例: #include #include int main(void) char *forward = string; printf(Before strrev(): %sn, forward); strrev(forward); printf(After strrev(): %sn, forward); return 0; 函數(shù)名: strset 功 能: 將一個(gè)串中的所有字符都設(shè)為指定字符 用 法: char *strset(char *str, char c); 程序例: #include #include int main(void)

21、char string10 = 123456789; char symbol = c; printf(Before strset(): %sn, string); strset(string, symbol); printf(After strset(): %sn, string); return 0; 函數(shù)名: strspn 功 能: 在串中查找指定字符集的子集的第一次出現(xiàn) 用 法: int strspn(char *str1, char *str2); 程序例: #include #include #include int main(void) char *string1 = 123456

22、7890; char *string2 = 123DC8; int length; length = strspn(string1, string2); printf(Character where strings differ is at position %dn, length); return 0; 函數(shù)名: strstr 功 能: 在串中查找指定字符串的第一次出現(xiàn) 用 法: char *strstr(char *str1, char *str2); 程序例: #include #include int main(void) char *str1 = Borland Internatio

23、nal, *str2 = nation, *ptr; ptr = strstr(str1, str2); printf(The substring is: %sn, ptr); return 0; 函數(shù)名: strtod 功 能: 將字符串轉(zhuǎn)換為double型值 用 法: double strtod(char *str, char *endptr); 程序例: #include #include int main(void) char input80, *endptr; double value; printf(Enter a floating point number:); gets(inp

24、ut); value = strtod(input, &endptr); printf(The string is %s the number is %lfn, input, value); return 0; 函數(shù)名: strtok 功 能: 查找由在第二個(gè)串中指定的分界符分隔開的單詞 用 法: char *strtok(char *str1, char *str2); 程序例: #include #include int main(void) char input16 = abc,d; char *p; /* strtok places a NULL terminator in front of the token, if found */ p = strtok(input, ,); if (p) printf(%sn, p); /* A second call to strtok using a NULL as the first parameter returns a pointer to the character following the token */ p = strtok(NULL, ,); if (p) printf

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論