C語言字符串函數(shù)大全_第1頁
C語言字符串函數(shù)大全_第2頁
C語言字符串函數(shù)大全_第3頁
C語言字符串函數(shù)大全_第4頁
C語言字符串函數(shù)大全_第5頁
已閱讀5頁,還剩9頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、C語言字符串函數(shù)大全1.函數(shù)名: strcpy功 能: 拷貝一個字符串到另一個用 法: char *stpcpy(char *destin, char *source);程序例:#include <stdio.h>#include <string.h>int main(void)char string10;char *str1 = "abcdefghi"strcpy(string, str1);printf("%sn", string);return 0;2.函數(shù)名: strcat功 能: 字符串拼接函數(shù)用 法: char *st

2、rcat(char *destin, char *source);程序例:#include <string.h>#include <stdio.h>int main(void)char destination25;char *blank = " ", *c = "C+", *Borland = "Borland"strcpy(destination, Borland);strcat(destination, blank);strcat(destination, c);printf("%sn"

3、, destination);return 0;3.函數(shù)名: strchr功 能: 在一個串中查找給定字符的第一個匹配之處用 法: char *strchr(char *str, char c);程序例:#include <string.h>#include <stdio.h>int main(void)char string15;char *ptr, c = 'r'strcpy(string, "This is a string");ptr = strchr(string, c);if (ptr)printf("The c

4、haracter %c is at position: %dn", c, ptr-string);elseprintf("The character was not foundn");return 0;4.函數(shù)名: strcmp功 能: 串比較用 法: int strcmp(char *str1, char *str2);看Asic碼,str1>str2,返回值 > 0;兩串相等,返回0程序例:#include <string.h>#include <stdio.h>int main(void)char *buf1 = &quo

5、t;aaa", *buf2 = "bbb", *buf3 = "ccc"int ptr;ptr = strcmp(buf2, buf1);if (ptr > 0)printf("buffer 2 is greater than buffer 1n");elseprintf("buffer 2 is less than buffer 1n");ptr = strcmp(buf2, buf3);if (ptr > 0)printf("buffer 2 is greater than bu

6、ffer 3n");elseprintf("buffer 2 is less than buffer 3n");return 0;5.函數(shù)名: strncmpi功 能: 將一個串中的一部分與另一個串比較, 不管大小寫用 法: int strncmpi(char *str1, char *str2, unsigned maxlen);程序例:#include <string.h>#include <stdio.h>int main(void)char *buf1 = "BBB", *buf2 = "bbb&quo

7、t;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;6.函數(shù)名: strcpy功 能: 串拷貝用 法: char *strcpy(char *str1, char *str

8、2);程序例:#include <stdio.h>#include <string.h>int main(void)char string10;char *str1 = "abcdefghi"strcpy(string, str1);printf("%sn", string);return 0;7.函數(shù)名: strcspn功 能: 在串中查找第一個給定字符集內(nèi)容的段用 法: int strcspn(char *str1, char *str2);程序例:#include <stdio.h>#include <st

9、ring.h>#include <alloc.h>int main(void)char *string1 = "1234567890"char *string2 = "747DC8"int length;length = strcspn(string1, string2);printf("Character where strings intersect is at position %dn", length);return 0;8.函數(shù)名: strdup功 能: 將串拷貝到新建的位置處用 法: char *strd

10、up(char *str);程序例:#include <stdio.h>#include <string.h>#include <alloc.h>int main(void)char *dup_str, *string = "abcde"dup_str = strdup(string);printf("%sn", dup_str);free(dup_str);return 0;9.函數(shù)名: stricmp功 能: 以大小寫不敏感方式比較兩個串用 法: int stricmp(char *str1, char *str2

11、);程序例:#include <string.h>#include <stdio.h>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

12、 = 0)printf("buffer 2 equals buffer 1n");return 0;10.函數(shù)名: strerror功 能: 返回指向錯誤信息字符串的指針用 法: char *strerror(int errnum);程序例:#include <stdio.h>#include <errno.h>int main(void)char *buffer;buffer = strerror(errno);printf("Error: %sn", buffer);return 0;11.函數(shù)名: strcmpi功 能: 將

13、一個串與另一個比較, 不管大小寫用 法: int strcmpi(char *str1, char *str2);程序例:#include <string.h>#include <stdio.h>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)prin

14、tf("buffer 2 is less than buffer 1n");if (ptr = 0)printf("buffer 2 equals buffer 1n");return 0;12.函數(shù)名: strncmp功 能: 串比較用 法: int strncmp(char *str1, char *str2, int maxlen);程序例:#include <string.h>#include <stdio.h>int main(void)char *buf1 = "aaabbb", *buf2 = &

15、quot;bbbccc", *buf3 = "ccc"int ptr;ptr = strncmp(buf2,buf1,3);if (ptr > 0)printf("buffer 2 is greater than buffer 1n");elseprintf("buffer 2 is less than buffer 1n");ptr = strncmp(buf2,buf3,3);if (ptr > 0)printf("buffer 2 is greater than buffer 3n");

16、elseprintf("buffer 2 is less than buffer 3n");return(0);13.函數(shù)名: strncmpi功 能: 把串中的一部分與另一串中的一部分比較, 不管大小寫用 法: int strncmpi(char *str1, char *str2);程序例:#include <string.h>#include <stdio.h>int main(void)char *buf1 = "BBBccc", *buf2 = "bbbccc"int ptr;ptr = strncm

17、pi(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;14.函數(shù)名: strncpy功 能: 串拷貝用 法: char *strncpy(char *destin, char *source, int maxlen

18、);程序例:#include <stdio.h>#include <string.h>int main(void)char string10;char *str1 = "abcdefghi"strncpy(string, str1, 3);string3 = '0'printf("%sn", string);return 0;15.函數(shù)名: strnicmp功 能: 不注重大小寫地比較兩個串用 法: int strnicmp(char *str1, char *str2, unsigned maxlen);程序例:

19、#include <string.h>#include <stdio.h>int main(void)char *buf1 = "BBBccc", *buf2 = "bbbccc"int ptr;ptr = strnicmp(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

20、(ptr = 0)printf("buffer 2 equals buffer 1n");return 0;16.函數(shù)名: strnset功 能: 將一個串中的所有字符都設(shè)為指定字符用 法: char *strnset(char *str, char ch, unsigned n);程序例:#include <stdio.h>#include <string.h>int main(void)char *string = "abcdefghijklmnopqrstuvwxyz"char letter = 'x'pri

21、ntf("string before strnset: %sn", string);strnset(string, letter, 13);printf("string after strnset: %sn", string);return 0;17.函數(shù)名: strpbrk功 能: 在串中查找給定字符集中的字符用 法: char *strpbrk(char *str1, char *str2);程序例:#include <stdio.h>#include <string.h>int main(void)char *string1

22、 = "abcdefghijklmnopqrstuvwxyz"char *string2 = "onm"char *ptr;ptr = strpbrk(string1, string2);if (ptr)printf("strpbrk found first character: %cn", *ptr);elseprintf("strpbrk didn't find character in setn");return 0;18.函數(shù)名: strrchr功 能: 在串中查找指定字符的最后一個出現(xiàn)用 法: c

23、har *strrchr(char *str, char c);程序例:#include <string.h>#include <stdio.h>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);elseprintf(

24、"The character was not foundn");return 0;19.函數(shù)名: strrev功 能: 串倒轉(zhuǎn)用 法: char *strrev(char *str);程序例:#include <string.h>#include <stdio.h>int main(void)char *forward = "string"printf("Before strrev(): %sn", forward);strrev(forward);printf("After strrev(): %s

25、n", forward);return 0;20.函數(shù)名: strset功 能: 將一個串中的所有字符都設(shè)為指定字符用 法: char *strset(char *str, char c);程序例:#include <stdio.h>#include <string.h>int main(void)char string10 = "123456789"char symbol = 'c'printf("Before strset(): %sn", string);strset(string, symbol)

26、;printf("After strset(): %sn", string);return 0;21.函數(shù)名: strspn功 能: 在串中查找指定字符集的子集的第一次出現(xiàn)用 法: int strspn(char *str1, char *str2);程序例:#include <stdio.h>#include <string.h>#include <alloc.h>int main(void)char *string1 = "1234567890"char *string2 = "123DC8"i

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

28、d International", *str2 = "nation", *ptr;ptr = strstr(str1, str2);printf("The substring is: %sn", ptr);return 0;23.函數(shù)名: strtod功 能: 將字符串轉(zhuǎn)換為double型值用 法: double strtod(char *str, char *endptr);程序例:#include <stdio.h>#include <stdlib.h>int main(void)char input80, *end

29、ptr;double value;printf("Enter a floating point number:");gets(input);value = strtod(input, &endptr);printf("The string is %s the number is %lfn", input, value);return 0;24.函數(shù)名: strtok功 能: 查找由在第二個串中指定的分界符分隔開的單詞用 法: char *strtok(char *str1, char *str2);程序例:#include <string.

30、h>#include <stdio.h>int main(void)char input16 = "abc,d"char *p;p = strtok(input, ",");if (p) printf("%sn", p);p = strtok(NULL, ",");if (p) printf("%sn", p);return 0;25.函數(shù)名: strtol功 能: 將串轉(zhuǎn)換為長整數(shù)用 法: long strtol(char *str, char *endptr, int ba

31、se);程序例:#include <stdlib.h>#include <stdio.h>int main(void)char *string = "87654321", *endptr;long lnumber;lnumber = strtol(string, &endptr, 10);printf("string = %s long = %ldn", string, lnumber);return 0;26.函數(shù)名: strupr功 能: 將串中的小寫字母轉(zhuǎn)換為大寫字母用 法: char *strupr(char *s

32、tr);程序例:#include <stdio.h>#include <string.h>int main(void)char *string = "abcdefghijklmnopqrstuvwxyz", *ptr;ptr = strupr(string);printf("%sn", ptr);return 0;27.函數(shù)名: swab功 能: 交換字節(jié)用 法: void swab (char *from, char *to, int nbytes);程序例:#include <stdlib.h>#include &

33、lt;stdio.h>#include <string.h>char source15 = "rFna koBlrna d"char target15;int main(void)swab(source, target, strlen(source);printf("This is target: %sn", target);return 0;28.函數(shù)名:strlen功能:計算字符串長度,計算字符串以零結(jié)尾的長度用 法:  int strlen(char *s);#include <string.h>#inclu

34、de<stdio.h> int main(void) char *s="Golden Global View" printf("%s has %d chars",s,strlen(s); getchar(); return 0;   WINDOWS 字符串處理函數(shù)  API/lstrcmp 區(qū)分大小寫;返回值: -1、0、1, 其中 0 表示相同if(lstrcmp(str1,str2)!=0)str1,str2是兩個字符串;  lstrcmpi 不區(qū)分大小寫返回值: -1、0、1, 其中 0 表示相同if(lstrcmpi(str1,str2)!=0)str1,str2是兩個字符串; 

溫馨提示

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

最新文檔

評論

0/150

提交評論