常用字符串處理函數匯總

***************************************************

更多精彩,歡迎進入:http://shop115376623.taobao.com

***************************************************


(一)strcmp函數??

???????? strcmp函數是比較兩個字符串的大小,返回比較的結果。一般形式是:??

???????????????? i=strcmp(字符串1,字符串2); ??

???????? 其中,字符串1、字符串2均可為字符串常量或變量;i?? 是用于存放比較結果的整型變量。比較結果是這樣規定的(可理解為:串1-串2):??

①字符串1小于字符串2,strcmp函數返回一個負值;

②字符串1等于字符串2,strcmp函數返回零;

③字符串1大于字符串2,strcmp函數返回一個正值;

那么,字符中的大小是如何比較的呢?來看一個例子。

???????? 實際上,字符串的比較是比較字符串中各對字符的ASCII碼。首先比較兩個串的第一個字符,若不相等,則停止比較并得出大于或小于的結果;如果相等就接著 比較第二個字符然后第三個字符等等。如果兩上字符串前面的字符一直相等,像"disk"和"disks"?? 那樣,?? 前四個字符都一樣,?? 然后比較第 五個字符,?? 前一個字符串"disk"只剩下結束符'/0',后一個字符串"disks"剩下's','/0'的ASCII碼小于's'的ASCII 碼,所以得出了結果。因此無論兩個字符串是什么樣,strcmp函數最多比較到其中一個字符串遇到結束符'/0'為止,就能得出結果。

注意:字符串是數組類型而非簡單類型,不能用關系運算進行大小比較。??

???????? if("ABC">"DEF")?? /*錯誤的字符串比較*/

???????? if(strcmp("ABC","DEF")?? /*正確的字符串比較*/

(二)strcpy函數??

???????? strcpy函數用于實現兩個字符串的拷貝。一般形式是:??

???????????????? strcpy(字符:串1,字符串2)

???????? 其中,字符串1必須是字符串變量,而不能是字符串常量。strcpy函數把字符串2的內容完全復制到字符串1中,而不管字符串1中原先存放的是什么。復制后,字符串2保持不變。??

例:????

???????? 注意,由于字符串是數組類型,所以兩個字符串復制不通過賦值運算進行。??

???????? t=s;?? /*錯誤的字符串復制*/

???????? strcpy(t,s);?? /*正確的字符串復制*/

函數名: stpcpy
功 能: 拷貝一個字符串到另一個
用 法: char *stpcpy(char *destin, char *source);
程序例:
#include <stdio.h>
#include <string.h>
int main(void)
{
???char string[10];
???char *str1 = "abcdefghi";
???stpcpy(string, str1);
???printf("%s\n", string);
???return 0;
}

?

函數名: strcat
功 能: 字符串拼接函數
用 法: char *strcat(char *destin, char *source);
程序例:
#include <string.h>
#include <stdio.h>
int main(void)
{
???char destination[25];
???char *blank = " ", *c = "C++", *Borland = "Borland";
???strcpy(destination, Borland);
???strcat(destination, blank);
???strcat(destination, c);
???printf("%s\n", destination);
???return 0;
}

?

函數名: strchr
功 能: 在一個串中查找給定字符的第一個匹配之處\
用 法: char *strchr(char *str, char c);
程序例:
#include <string.h>
#include <stdio.h>
int main(void)
{
????char string[15];
????char *ptr, c = 'r';
????strcpy(string, "This is a string");
????ptr = strchr(string, c);
????if (ptr)
???????printf("The character %c is at position: %d\n", c, ptr-string);
????else
???????printf("The character was not found\n");
????return 0;
}

?

函數名: strcmp
功 能: 串比較
用 法: int strcmp(char *str1, char *str2);
看Asic碼,str1>str2,返回值 > 0;兩串相等,返回0
程序例:
#include <string.h>
#include <stdio.h>
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 1\n");
????else
???????printf("buffer 2 is less than buffer 1\n");
????ptr = strcmp(buf2, buf3);
????if (ptr > 0)
???????printf("buffer 2 is greater than buffer 3\n");
????else
???????printf("buffer 2 is less than buffer 3\n");
????return 0;
}

?

函數名: strncmpi
功 能: 將一個串中的一部分與另一個串比較, 不管大小寫
用 法: int strncmpi(char *str1, char *str2, unsigned maxlen);
程序例:
#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 1\n");
???if (ptr < 0)
??????printf("buffer 2 is less than buffer 1\n");
???if (ptr == 0)
??????printf("buffer 2 equals buffer 1\n");
???return 0;
}

?

函數名: strcpy
功 能: 串拷貝
用 法: char *strcpy(char *str1, char *str2);
程序例:
#include <stdio.h>
#include <string.h>
int main(void)
{
????char string[10];
????char *str1 = "abcdefghi";
????strcpy(string, str1);
????printf("%s\n", string);
????return 0;
}

?

函數名: strcspn
功 能: 在串中查找第一個給定字符集內容的段
用 法: int strcspn(char *str1, char *str2);
程序例:
#include <stdio.h>
#include <string.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 %d\n", length);
????return 0;
}

?

函數名: strdup
功 能: 將串拷貝到新建的位置處
用 法: char *strdup(char *str);
程序例:
#include <stdio.h>
#include <string.h>
#include <alloc.h>
int main(void)
{
????char *dup_str, *string = "abcde";
????dup_str = strdup(string);
????printf("%s\n", dup_str);
????free(dup_str);
????return 0;
}

?

函數名: stricmp
功 能: 以大小寫不敏感方式比較兩個串
用 法: int stricmp(char *str1, char *str2);
程序例:
#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 1\n");
???if (ptr < 0)
??????printf("buffer 2 is less than buffer 1\n");
???if (ptr == 0)
??????printf("buffer 2 equals buffer 1\n");
???return 0;
}

?

函數名: strerror
功 能: 返回指向錯誤信息字符串的指針
用 法: char *strerror(int errnum);
程序例:
#include <stdio.h>
#include <errno.h>
int main(void)
{
???char *buffer;
???buffer = strerror(errno);
???printf("Error: %s\n", buffer);
???return 0;
}

?

函數名: strcmpi
功 能: 將一個串與另一個比較, 不管大小寫
用 法: 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 1\n");
???if (ptr < 0)
??????printf("buffer 2 is less than buffer 1\n");
???if (ptr == 0)
??????printf("buffer 2 equals buffer 1\n");
???return 0;
}

?

函數名: strncmp
功 能: 串比較
用 法: int strncmp(char *str1, char *str2, int maxlen);
程序例:
#include <string.h>
#include <stdio.h>
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 1\n");
???else
??????printf("buffer 2 is less than buffer 1\n");
???ptr = strncmp(buf2,buf3,3);
???if (ptr > 0)
??????printf("buffer 2 is greater than buffer 3\n");
???else
??????printf("buffer 2 is less than buffer 3\n");
???return(0);
}

?

函數名: strncmpi
功 能: 把串中的一部分與另一串中的一部分比較, 不管大小寫
用 法: int strncmpi(char *str1, char *str2);
程序例:
#include <string.h>
#include <stdio.h>
int main(void)
{
???char *buf1 = "BBBccc", *buf2 = "bbbccc";
???int ptr;
???ptr = strncmpi(buf2,buf1,3);
???if (ptr > 0)
??????printf("buffer 2 is greater than buffer 1\n");
???if (ptr < 0)
??????printf("buffer 2 is less than buffer 1\n");
???if (ptr == 0)
??????printf("buffer 2 equals buffer 1\n");
???return 0;
}

?

函數名: strncpy
功 能: 串拷貝
用 法: char *strncpy(char *destin, char *source, int maxlen);
程序例:
#include <stdio.h>
#include <string.h>
int main(void)
{
???char string[10];
???char *str1 = "abcdefghi";
???strncpy(string, str1, 3);
???string[3] = '\0';
???printf("%s\n", string);
???return 0;
}

?

函數名: strnicmp
功 能: 不注重大小寫地比較兩個串
用 法: int strnicmp(char *str1, char *str2, unsigned maxlen);
程序例:
#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 1\n");
???if (ptr < 0)
??????printf("buffer 2 is less than buffer 1\n");
???if (ptr == 0)
??????printf("buffer 2 equals buffer 1\n");
???return 0;
}

?

函數名: strnset
功 能: 將一個串中的所有字符都設為指定字符
用 法: char *strnset(char *str, char ch, unsigned n);
程序例:
#include <stdio.h>
#include <string.h>
int main(void)
{
???char *string = "abcdefghijklmnopqrstuvwxyz";
???char letter = 'x';
???printf("string before strnset: %s\n", string);
???strnset(string, letter, 13);
???printf("string after strnset: %s\n", string);
???return 0;
}

?

函數名: strpbrk
功 能: 在串中查找給定字符集中的字符
用 法: char *strpbrk(char *str1, char *str2);
程序例:
#include <stdio.h>
#include <string.h>
int main(void)
{
???char *string1 = "abcdefghijklmnopqrstuvwxyz";
???char *string2 = "onm";
???char *ptr;
???ptr = strpbrk(string1, string2);
???if (ptr)
??????printf("strpbrk found first character: %c\n", *ptr);
???else
??????printf("strpbrk didn't find character in set\n");
???return 0;
}

?

函數名: strrchr
功 能: 在串中查找指定字符的最后一個出現
用 法: char *strrchr(char *str, char c);
程序例:
#include <string.h>
#include <stdio.h>
int main(void)
{
???char string[15];
???char *ptr, c = 'r';
???strcpy(string, "This is a string");
???ptr = strrchr(string, c);
???if (ptr)
??????printf("The character %c is at position: %d\n", c, ptr-string);
???else
??????printf("The character was not found\n");
???return 0;
}

?

函數名: strrev
功 能: 串倒轉
用 法: char *strrev(char *str);
程序例:
#include <string.h>
#include <stdio.h>
int main(void)
{
???char *forward = "string";
???printf("Before strrev(): %s\n", forward);
???strrev(forward);
???printf("After strrev(): %s\n", forward);
???return 0;
}

?

函數名: strset
功 能: 將一個串中的所有字符都設為指定字符
用 法: char *strset(char *str, char c);
程序例:
#include <stdio.h>
#include <string.h>
int main(void)
{
???char string[10] = "123456789";
???char symbol = 'c';
???printf("Before strset(): %s\n", string);
???strset(string, symbol);
???printf("After strset(): %s\n", string);
???return 0;
}

?

函數名: strspn
功 能: 在串中查找指定字符集的子集的第一次出現
用 法: int strspn(char *str1, char *str2);
程序例:
#include <stdio.h>
#include <string.h>
#include <alloc.h>
int main(void)
{
???char *string1 = "1234567890";
???char *string2 = "123DC8";
???int length;
???length = strspn(string1, string2);
???printf("Character where strings differ is at position %d\n", length);
???return 0;
}

?

函數名: strstr
功 能: 在串中查找指定字符串的第一次出現
用 法: char *strstr(char *str1, char *str2);
程序例:
#include <stdio.h>
#include <string.h>
int main(void)
{
???char *str1 = "Borland International", *str2 = "nation", *ptr;
???ptr = strstr(str1, str2);
???printf("The substring is: %s\n", ptr);
???return 0;
}

?

函數名: strtod
功 能: 將字符串轉換為double型值
用 法: double strtod(char *str, char **endptr);
程序例:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
???char input[80], *endptr;
???double value;
???printf("Enter a floating point number:");
???gets(input);
???value = strtod(input, &endptr);
???printf("The string is %s the number is %lf\n", input, value);
???return 0;
}

?

函數名: strtok
功 能: 查找由在第二個串中指定的分界符分隔開的單詞
用 法: char *strtok(char *str1, char *str2);
程序例:
#include <string.h>
#include <stdio.h>
int main(void)
{
???char input[16] = "abc,d";
???char *p;
??
???p = strtok(input, ",");
???if (p)???printf("%s\n", p);
??
???p = strtok(NULL, ",");
???if (p)???printf("%s\n", p);
???return 0;
}

?

函數名: strtol
功 能: 將串轉換為長整數
用 法: long strtol(char *str, char **endptr, int base);
程序例:
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
???char *string = "87654321", *endptr;
???long lnumber;
??
???lnumber = strtol(string, &endptr, 10);
???printf("string = %s long = %ld\n", string, lnumber);
???return 0;
}

?

函數名: strupr
功 能: 將串中的小寫字母轉換為大寫字母
用 法: char *strupr(char *str);
程序例:
#include <stdio.h>
#include <string.h>
int main(void)
{
???char *string = "abcdefghijklmnopqrstuvwxyz", *ptr;
??
???ptr = strupr(string);
???printf("%s\n", ptr);
???return 0;
}

?

函數名: swab
功 能: 交換字節
用 法: void swab (char *from, char *to, int nbytes);
程序例:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
char source[15] = "rFna koBlrna d";
char target[15];
int main(void)
{
???swab(source, target, strlen(source));
???printf("This is target: %s\n", target);
???return 0;
}


本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/458952.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/458952.shtml
英文地址,請注明出處:http://en.pswp.cn/news/458952.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

兼容性記錄-class屬性

getAttribute獲得class屬性時,IE6,IE7的傳參是className,IE7和現代游覽器都是class全部游覽器DOMElement均有的className屬性,其在IE各版本號下的均表現良好返回屬性class值的字符串此外html5中DOMElement有個classList屬性,它返回一個類型為DOMTokenList的對象,它當中有非常多…

magenta內核與linux,谷歌將推出新操作系統Fuchsia:Magenta語言為內核

谷歌現在研發出來并且推出使用的系統有Chrome OS、Android和Chromecasts&#xff0c;這三者在操作系統的市場中占得份額很高&#xff0c;但是好像谷歌對此并不滿意&#xff0c;因為有相關消息顯示&#xff0c;谷歌正在研發新的操作系統Fuchsia&#xff0c;該系統采用Magenta語言…

BZOJ 1968: [Ahoi2005]COMMON 約數研究 水題

1968: [Ahoi2005]COMMON 約數研究 Time Limit: 20 Sec Memory Limit: 256 MB 題目連接 http://www.lydsy.com/JudgeOnline/problem.php?id1968 Description Input 只有一行一個整數 N&#xff08;0 < N < 1000000&#xff09;。 Output 只有一行輸出&#xff0c;為整數M…

VC內存對齊準則(Memory alignment)

*************************************************** 更多精彩&#xff0c;歡迎進入&#xff1a;http://shop115376623.taobao.com *************************************************** 本文所有內容在建立在一個前提下&#xff1a;使用VC編譯器。著重點在于&#xff1a;VC…

[redis設計與實現][7]基本數據結構——對象

Redis對基礎數據類型進行了封裝&#xff0c;構建出上層的對象系統&#xff0c;這個系統包含&#xff1a;字符串對象、列表對象、哈希對象、集合對象和有序集合對象。 Redis對象結構&#xff1a; [cce lang”c”] typedef struct redisObject { //類型 unsigned type:4; //編碼 …

Linux中samba的權限詳解,活用三種權限 理解Samba的權限控制

在企業內網開發環境方面&#xff0c;文件服務器是一個非常重要的環節。在這當中&#xff0c;Samba服務器由于其權限控制的高度靈活性&#xff0c;在這里撫琴煮酒會進行一些列詳細的說明。之前我們已經介紹了Samba服務器一些入門和基本的更換用戶、端口監聽、列表控制等技巧&…

安卓基礎01

安卓基礎01SDK System images 這是在創建模擬器時需要的system image&#xff0c;也就是在創建模擬器時CPU/ABI項需要選擇的&#xff0c;下載并解壓后&#xff0c;將解壓出的整個文件夾復制或者移動到 your sdk 路徑/system-images文件夾下即可&#xff0c; 如果沒有 system-im…

C++存儲區域基礎概念詳解

*************************************************** 更多精彩&#xff0c;歡迎進入&#xff1a;http://shop115376623.taobao.com *************************************************** 以下詳解來自&#xff1a; http://blog.163.com/zb_075/blog/static/3734032820115110…

創建、修改、刪除表總結

一、創建表 CREATE TABLE <表名> &#xff08;<列名><數據類型>[列級完整性約束條件] [&#xff0c;<列名> <數據類型>[列級完整性約束條件]] … [&#xff0c;表級完整性約束條件]&#xff09;&#xff1b; 如果完整性約束條件涉及到該表的多個…

linux 編譯查看鏈接庫詳情,Linux環境下的編譯,鏈接與庫的使用

鏈接參數控制鏈接器中提供了-dn -dy 參數來控制使用的是動態庫還是靜態庫&#xff0c;-dn表示后面使用的是靜態庫&#xff0c;-dy表示使用的是動態庫例:g -Lpath -Wl,-dn -lx -Wl,-dy -lpthread 這樣如果在path路徑下有libx.so和libx.a這個時候只會用到 libx.a.注意在最后的地…

在C++中用虛函數的作用是什么? 為什么要用到虛函數?

***************************************************更多精彩&#xff0c;歡迎進入&#xff1a;http://shop115376623.taobao.com***************************************************虛函數聯系到多態&#xff0c;多態聯系到繼承。所以本文中都是在繼承層次上做文章。沒了繼…

mysql 主主互備實現

今天星期天&#xff0c;么事就寫個MYSQL的主主架構的博客&#xff0c;原理如下圖&#xff0c;不是我畫的網上找的。主機作用操作系統mysql版本對應IPvip數據庫mysqlA(主)centos6.4mysql 5.5.48192.168.48.129192.168.48.126mysqlB(備)centos6.4mysql 5.5.48192.168.48.132一&am…

Linux Deepin 版本大全,Deepin下載-Deepin linuxv20.0 官方版下載-6188手游網

Deepin linux安裝準備一&#xff1a;為 Deepin 留出硬盤空間以便安裝。1.打開磁盤管理(Windows 徽標上右鍵單擊&#xff0c;選擇磁盤管理)。2.選中一個分區(空閑空間大于 30G)&#xff0c;我選擇的是 C 盤&#xff0c;因為是固態硬盤所以安裝啟動會快很多。磁盤 1 的 200G 分區…

動手實踐 Linux VLAN - 每天5分鐘玩轉 OpenStack(13)

本節我們來看如何在實驗環境中實施和配置如下 VLAN 網絡 配置 VLAN 編輯 /etc/network/interfaces&#xff0c;配置 eth0.10、brvlan10、eth0.20 和 brvlan20。 下面用 vmdiff 展示了對 /etc/network/interfaces 的修改 重啟宿主機&#xff0c;ifconfig 各個網絡接口 用 brct…

Socket的3次握手鏈接與4次斷開握手

*************************************************** 更多精彩&#xff0c;歡迎進入&#xff1a;http://shop115376623.taobao.com *************************************************** 連接握手&#xff1a; 1.客戶端發送建立連接請求 &#xff08;發送請求&#xff09;2.…

Linux桌面需要強制訪問控制,RHCSA 系列(十三): 在 RHEL 7 中使用 SELinux 進行強制訪問控制...

RHCSA 認證&#xff1a;SELinux 精要和控制文件系統的訪問盡管作為第一級別的權限和訪問控制機制是必要的&#xff0c;但它們同樣有一些局限&#xff0c;而這些局限則可以由安全增強 Linux(Security Enhanced Linux&#xff0c;簡稱為 SELinux)來處理。這些局限的一種情形是&am…

使用canvas實現擦玻璃效果

體驗效果:http://hovertree.com/texiao/html5/25/效果圖&#xff1a;代碼如下&#xff1a; <!DOCTYPE html> <html> <head lang"zh"> <meta name"viewport" content"initial-scale1.0, maximum-scale1.0, user-scalableno, widt…

如何計算時間復雜度

*************************************************** 更多精彩&#xff0c;歡迎進入&#xff1a;http://shop115376623.taobao.com *************************************************** 求解算法的時間復雜度的具體步驟是&#xff1a; ⑴ 找出算法中的基本語句&#xff1b;…

linux顯示系統信息軟件下載,linux查看系統信息軟件安裝信息命令學習筆記

查看LINUX安裝版本[rootlocalhost etc]# unameLinux[rootlocalhost etc]# uname -aLinux localhost.localdomain 2.6.32-279.11.1.el6.i686 #1 SMP Tue Oct 16 14:40:53 UTC 2012 i686 i686 i386 GNU/Linux[rootlocalhost etc]# cat /proc/versionLinux version 2.6.32-279.11.…

Bzoj 2662: [BeiJing wc2012]凍結 dijkstra,堆,分層圖,最短路

2662: [BeiJing wc2012]凍結 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 647 Solved: 348[Submit][Status][Discuss]Description “我要成為魔法少女&#xff01;” “那么&#xff0c;以靈魂為代價&#xff0c;你希望得到什么&#xff1f;” “我要將有關魔法和奇…