C語言字符串函數大全

轉載自http://www.360doc.com/content/08/0723/22/26860_1462024.shtml#

C語言字符串函數大全

函數名: 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;

?

??? /* strtok places a NULL terminator

??? ?in front of the token, if found */

??? p = strtok(input, ",");

??? if (p) printf("%s\n", 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("%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;

?

??? /* strtol converts string to long integer? */

??? 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;

?

??? /* converts string to upper case characters */

??? 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;

}

轉載于:https://www.cnblogs.com/Karma-wjc/p/4023075.html

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

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

相關文章

Makefile中 -I -L -l區別

轉載自&#xff1a;http://blog.csdn.net/davion_zhang/article/details/41805641 我們用gcc編譯程序時&#xff0c;可能會用到“-I”&#xff08;大寫i&#xff09;&#xff0c;“-L”&#xff08;大寫l&#xff09;&#xff0c;“-l”&#xff08;小寫l&#xff09;等參數&am…

PLT redirection through shared object injection into a running process

PLT redirection through shared object injection into a running process

python電腦版軟件下載_Python for windows

Python是一門跨平臺的腳本語言,Python規定了一個Python語法規則,實現了Python語法的解釋程序就成為了Python的解釋器,我們用的比較多的是C版本的Python,也就是使用C語言實現的Python解釋器,除此之外還有使用Java實現的Jython和使用.NET實現的IronPython,這些實現可以使Python用…

Struts優缺點

跟Tomcat、Turbine等諸多Apache項目一樣&#xff0c;是開源軟件&#xff0c;這是它的一大優點。使開發者能更深入的了解其內部實現機制。 Struts開放源碼框架的創建是為了使開發者在構建基于Java Servlet和JavaServer Pages&#xff08;JSP&#xff09;技術的Web應用時更加容易…

由Google Protocol Buffer的小例子引起的g++編譯問題

問題 學習 Google Protocol Buffer 的使用和原理時&#xff0c;提供了一個小例子&#xff0c;講述了protobuf的使用方法。 假如已經有了如下文件&#xff1a; 其中writer.cpp如下&#xff1a;#include "lm.helloworld.pb.h" #include<iostream> #include<…

用python編寫表達式求值_用Python3實現表達式求值

Problem Description yizhen has no girlfriend due to his stupid brain that he even can’t solve a simple arithmetic roblem. Can you help him If you solve it and tell him the result, then he can find his lovers! So beautiful! Input The input一、題目描述請用 …

the first day

開博第一天&#xff0c;從此記錄我生活學習的點滴&#xff0c;加油轉載于:https://www.cnblogs.com/fkissx/p/3702132.html

驅動-問題解決

今天在網上買了一個二手的電腦&#xff0c;拿回來以后&#xff0c;發現有點問題&#xff0c;一個問題就是 1.usb插上U盤以后沒有反應 解決方法&#xff1a; 嘗試一、直接在網上下載了一個360驅動大師&#xff0c;更新了一下驅動&#xff0c;沒有解決 嘗試二、在網上下載了一個驅…

Swift 學習- 02 -- 基礎部分2

class NamedShape{ var numberOfSides: Int 0 var name: String init(name: String) { self.name name } func simpleDecription() -> String { return "A shape with \(numberOfSides) \(name) sides" } } // 除了儲存簡單的屬性之外,屬性可以有 getter 和 set…

R-CNN detection 運行問題及辦法

運行caffe官方提供的jupyter 的rcnn detection&#xff0c;總是出現各種問題。先將問題及方法匯集在此&#xff1a; 1. Selective Search 的安裝問題 按照官網&#xff0c;我下載了selective_search_ijcv_with_python&#xff0c;但是在我的linux matlab2017a上總是出現問題&…

python怎么用lambda和map函數_Python之lambda匿名函數及map和filter的用法

現有兩個元組((a),(b)),((c),(d))&#xff0c;請使用python中匿名函數生成列表[{a:c},{b:d}]t1 ((a), (c))t2 ((b), (d))print(list(map(lambda t: {t[0]: t[1]}, zip(t1, t2))))l lambda t1, t2: [{i: j} for i, j in zip(t1, t2)]print(l(t1, t2))map內置函數使用&#xf…

UVALive 5903 Piece it together(二分圖匹配)

給你一個n*m的矩陣&#xff0c;每個點為B或W或.。然后你有一種碎片。碎片可以旋轉&#xff0c;問可否用這種碎片精確覆蓋矩陣。N,M<500 WB 《碎片 W 題目一看&#xff0c;感覺是精確覆蓋&#xff08;最近被覆蓋洗腦了&#xff09;&#xff0c;但是仔細分析可以知道&#xf…

將undefault和null的數據轉換成bool類型的數據 使用!!

<script> var o{}; var anull; console.info(!!o.name); </script> 輸出false 此方法是將undefault和null的數據轉換成bool類型的數據. var model avalon.define({ $id: model, defaultvalue {},});<span ms-if"!!defaultvalue .cost" >測試</…

springcloud(五):熔斷監控Hystrix Dashboard和Turbine

Hystrix-dashboard是一款針對Hystrix進行實時監控的工具&#xff0c;通過Hystrix Dashboard我們可以在直觀地看到各Hystrix Command的請求響應時間, 請求成功率等數據。但是只使用Hystrix Dashboard的話, 你只能看到單個應用內的服務信息, 這明顯不夠. 我們需要一個工具能讓我們…

如何修改PKG_CONFIG_PATH環境變量

兩種情況&#xff0c;如果你只是想加上某庫的pkg&#xff0c;則選擇下面其一&#xff1a;export PKG_CONFIG_PATH/usr/lib/pkgconfig/ 或者 export PKG_CONFIG_LIBDIR/usr/lib/pkgconfig/ 如果你想覆蓋掉原來的pkg,選擇后者。因為&#xff1a;PKG_CONFIG_LIBDIR的優先級比 PKG_…

python跨包導入包_python引入跨模塊包

人生苦短&#xff0c;我學python。最近學習python&#xff0c;由于包的模塊分的比較多。所以要用到跨模塊引入 且調用中間的方法整體目錄結構如下。需求&#xff1a;在 API模塊 user.py 中 調用 plugin 模塊中 douyin_login 下的方法。貼一下最終解決方案&#xff1a;from plug…

jdk1.8版本已經不包含jdbc.odbc連接

連接access的時候發現報錯&#xff0c;無法加載jdbc.odbc類文件&#xff0c;到Java安裝目錄上jre/lib/rt.jar上找jdbcodbc類也沒有了。 找個jdk1.7安裝就ok啦。轉載于:https://www.cnblogs.com/dohn/p/3707254.html

位運算問題

位運算 位運算是把數字用二進制表示之后&#xff0c;對每一位上0或者1的運算。 理解位運算的第一步是理解二進制。二進制是指數字的每一位都是0或者1.比如十進制的2轉化為二進制之后就是10。在程序員的圈子里有一個流傳了很久的笑話&#xff0c;說世界上有10種人&#xff0c;一…

conda環境管理介紹

我們可以使用conda 來切換不同的環境&#xff0c;主要的用法如下&#xff1a; 1. 創建環境 # 指定python版本為2.7&#xff0c;注意至少需要指定python版本或者要安裝的包 # 后一種情況下&#xff0c;自動安裝最新python版本conda create -n env_name python2.7# 同時安裝必…

unable to execute dex: multiple dex files Cocos2dxAccelerometer

原文轉載&#xff1a;http://discuss.cocos2d-x.org/t/conversion-to-dalvik-format-failed-unable-to-execute-dex-multiple-dex-files-define-lorg-cocos2dx-lib-cocos2dxaccelerometer/6652/4 用cocos2dx2.2.3沒問題&#xff0c;用了3.1.1出現這個問題。確實夠蛋疼。還要有這…