C語言字符串函數大全

C語言字符串函數大全

183人閱讀 評論(0) 收藏 舉報
  1.                 C語言字符串函數大全  
  2.   
  3. 函數名: stpcpy  
  4. 功  能: 拷貝一個字符串到另一個  
  5. 用  法: char *stpcpy(char *destin, char *source);  
  6. 程序例:  
  7.   
  8. #include <stdio.h>  
  9. #include <string.h>  
  10.   
  11. int main(void)  
  12. {  
  13.    char string[10];  
  14.    char *str1 = "abcdefghi";  
  15.   
  16.    stpcpy(string, str1);  
  17.    printf("%s\n", string);  
  18.    return 0;  
  19. }  
  20.    
  21.    
  22.    
  23.   
  24. 函數名: strcat  
  25. 功  能: 字符串拼接函數  
  26. 用  法: char *strcat(char *destin, char *source);  
  27. 程序例:  
  28.   
  29. #include <string.h>  
  30. #include <stdio.h>  
  31.   
  32. int main(void)  
  33. {  
  34.    char destination[25];  
  35.    char *blank = " ", *c = "C++", *Borland = "Borland";  
  36.   
  37.    strcpy(destination, Borland);  
  38.    strcat(destination, blank);  
  39.    strcat(destination, c);  
  40.   
  41.    printf("%s\n", destination);  
  42.    return 0;  
  43. }  
  44.    
  45.    
  46.    
  47.   
  48. 函數名: strchr  
  49. 功  能: 在一個串中查找給定字符的第一個匹配之處\  
  50. 用  法: char *strchr(char *str, char c);  
  51. 程序例:  
  52.   
  53. #include <string.h>  
  54. #include <stdio.h>  
  55.   
  56. int main(void)  
  57.  {  
  58.     char string[15];  
  59.     char *ptr, c = 'r';  
  60.   
  61.     strcpy(string, "This is a string");  
  62.     ptr = strchr(string, c);  
  63.     if (ptr)  
  64.        printf("The character %c is at position: %d\n", c, ptr-string);  
  65.     else  
  66.        printf("The character was not found\n");  
  67.     return 0;  
  68.  }  
  69.    
  70.    
  71.    
  72.   
  73. 函數名: strcmp  
  74. 功  能: 串比較  
  75. 用  法: int strcmp(char *str1, char *str2);  
  76. 看Asic碼,str1>str2,返回值 > 0;兩串相等,返回0  
  77. 程序例:  
  78.   
  79. #include <string.h>  
  80. #include <stdio.h>  
  81.   
  82. int main(void)  
  83.  {  
  84.     char *buf1 = "aaa", *buf2 = "bbb", *buf3 = "ccc";  
  85.     int ptr;  
  86.   
  87.     ptr = strcmp(buf2, buf1);  
  88.     if (ptr > 0)  
  89.        printf("buffer 2 is greater than buffer 1\n");  
  90.     else  
  91.        printf("buffer 2 is less than buffer 1\n");  
  92.   
  93.     ptr = strcmp(buf2, buf3);  
  94.     if (ptr > 0)  
  95.        printf("buffer 2 is greater than buffer 3\n");  
  96.     else  
  97.        printf("buffer 2 is less than buffer 3\n");  
  98.   
  99.     return 0;  
  100.  }  
  101.    
  102.    
  103.    
  104.   
  105. 函數名: strncmpi  
  106. 功  能: 將一個串中的一部分與另一個串比較, 不管大小寫  
  107. 用  法: int strncmpi(char *str1, char *str2, unsigned maxlen);  
  108. 程序例:  
  109.   
  110. #include <string.h>  
  111. #include <stdio.h>  
  112.   
  113. int main(void)  
  114. {  
  115.    char *buf1 = "BBB", *buf2 = "bbb";  
  116.    int ptr;  
  117.   
  118.    ptr = strcmpi(buf2, buf1);  
  119.   
  120.    if (ptr > 0)  
  121.       printf("buffer 2 is greater than buffer 1\n");  
  122.   
  123.    if (ptr < 0)  
  124.       printf("buffer 2 is less than buffer 1\n");  
  125.   
  126.    if (ptr == 0)  
  127.       printf("buffer 2 equals buffer 1\n");  
  128.   
  129.    return 0;  
  130. }  
  131.    
  132.    
  133.    
  134.   
  135. 函數名: strcpy  
  136. 功  能: 串拷貝  
  137. 用  法: char *strcpy(char *str1, char *str2);  
  138. 程序例:  
  139.   
  140. #include <stdio.h>  
  141. #include <string.h>  
  142.   
  143. int main(void)  
  144.  {  
  145.     char string[10];  
  146.     char *str1 = "abcdefghi";  
  147.   
  148.     strcpy(string, str1);  
  149.     printf("%s\n", string);  
  150.     return 0;  
  151.  }  
  152.    
  153.    
  154.    
  155.   
  156. 函數名: strcspn  
  157. 功  能: 在串中查找第一個給定字符集內容的段  
  158. 用  法: int strcspn(char *str1, char *str2);  
  159. 程序例:  
  160.   
  161. #include <stdio.h>  
  162. #include <string.h>  
  163. #include <alloc.h>  
  164.   
  165. int main(void)  
  166.  {  
  167.     char *string1 = "1234567890";  
  168.     char *string2 = "747DC8";  
  169.     int length;  
  170.   
  171.     length = strcspn(string1, string2);  
  172.     printf("Character where strings intersect is at position %d\n", length);  
  173.   
  174.     return 0;  
  175.  }  
  176.    
  177.    
  178.    
  179.   
  180. 函數名: strdup  
  181. 功  能: 將串拷貝到新建的位置處  
  182. 用  法: char *strdup(char *str);  
  183. 程序例:  
  184.   
  185. #include <stdio.h>  
  186. #include <string.h>  
  187. #include <alloc.h>  
  188.   
  189. int main(void)  
  190.  {  
  191.     char *dup_str, *string = "abcde";  
  192.   
  193.     dup_str = strdup(string);  
  194.     printf("%s\n", dup_str);  
  195.     free(dup_str);  
  196.   
  197.     return 0;  
  198.  }  
  199.    
  200.    
  201.    
  202.   
  203. 函數名: stricmp  
  204. 功  能: 以大小寫不敏感方式比較兩個串  
  205. 用  法: int stricmp(char *str1, char *str2);  
  206. 程序例:  
  207.   
  208. #include <string.h>  
  209. #include <stdio.h>  
  210.   
  211. int main(void)  
  212. {  
  213.    char *buf1 = "BBB", *buf2 = "bbb";  
  214.    int ptr;  
  215.   
  216.    ptr = stricmp(buf2, buf1);  
  217.   
  218.    if (ptr > 0)  
  219.       printf("buffer 2 is greater than buffer 1\n");  
  220.   
  221.    if (ptr < 0)  
  222.       printf("buffer 2 is less than buffer 1\n");  
  223.   
  224.    if (ptr == 0)  
  225.       printf("buffer 2 equals buffer 1\n");  
  226.   
  227.    return 0;  
  228. }  
  229.    
  230.    
  231.   
  232. 函數名: strerror  
  233. 功  能: 返回指向錯誤信息字符串的指針  
  234. 用  法: char *strerror(int errnum);  
  235. 程序例:  
  236.   
  237. #include <stdio.h>  
  238. #include <errno.h>  
  239.   
  240. int main(void)  
  241. {  
  242.    char *buffer;  
  243.    buffer = strerror(errno);  
  244.    printf("Error: %s\n", buffer);  
  245.    return 0;  
  246. }  
  247.    
  248.    
  249.    
  250.   
  251. 函數名: strcmpi  
  252. 功  能: 將一個串與另一個比較, 不管大小寫  
  253. 用  法: int strcmpi(char *str1, char *str2);  
  254. 程序例:  
  255.   
  256. #include <string.h>  
  257. #include <stdio.h>  
  258.   
  259. int main(void)  
  260. {  
  261.    char *buf1 = "BBB", *buf2 = "bbb";  
  262.    int ptr;  
  263.   
  264.    ptr = strcmpi(buf2, buf1);  
  265.   
  266.    if (ptr > 0)  
  267.       printf("buffer 2 is greater than buffer 1\n");  
  268.   
  269.    if (ptr < 0)  
  270.       printf("buffer 2 is less than buffer 1\n");  
  271.   
  272.    if (ptr == 0)  
  273.       printf("buffer 2 equals buffer 1\n");  
  274.   
  275.    return 0;  
  276. }  
  277.    
  278.    
  279.    
  280.   
  281. 函數名: strncmp  
  282. 功  能: 串比較  
  283. 用  法: int strncmp(char *str1, char *str2, int maxlen);  
  284. 程序例:  
  285.   
  286. #include <string.h>  
  287. #include <stdio.h>  
  288.   
  289. int  main(void)  
  290.   
  291. {  
  292.    char *buf1 = "aaabbb", *buf2 = "bbbccc", *buf3 = "ccc";  
  293.    int ptr;  
  294.   
  295.    ptr = strncmp(buf2,buf1,3);  
  296.    if (ptr > 0)  
  297.       printf("buffer 2 is greater than buffer 1\n");  
  298.    else  
  299.       printf("buffer 2 is less than buffer 1\n");  
  300.   
  301.    ptr = strncmp(buf2,buf3,3);  
  302.    if (ptr > 0)  
  303.       printf("buffer 2 is greater than buffer 3\n");  
  304.    else  
  305.       printf("buffer 2 is less than buffer 3\n");  
  306.   
  307.    return(0);  
  308. }  
  309.    
  310.    
  311.   
  312. 函數名: strncmpi  
  313. 功  能: 把串中的一部分與另一串中的一部分比較, 不管大小寫  
  314. 用  法: int strncmpi(char *str1, char *str2);  
  315. 程序例:  
  316.   
  317. #include <string.h>  
  318. #include <stdio.h>  
  319.   
  320. int main(void)  
  321. {  
  322.    char *buf1 = "BBBccc", *buf2 = "bbbccc";  
  323.    int ptr;  
  324.   
  325.    ptr = strncmpi(buf2,buf1,3);  
  326.   
  327.    if (ptr > 0)  
  328.       printf("buffer 2 is greater than buffer 1\n");  
  329.   
  330.    if (ptr < 0)  
  331.       printf("buffer 2 is less than buffer 1\n");  
  332.   
  333.    if (ptr == 0)  
  334.       printf("buffer 2 equals buffer 1\n");  
  335.   
  336.    return 0;  
  337. }  
  338.    
  339.    
  340.   
  341. 函數名: strncpy  
  342. 功  能: 串拷貝  
  343. 用  法: char *strncpy(char *destin, char *source, int maxlen);  
  344. 程序例:  
  345.   
  346. #include <stdio.h>  
  347. #include <string.h>  
  348.   
  349. int main(void)  
  350. {  
  351.    char string[10];  
  352.    char *str1 = "abcdefghi";  
  353.   
  354.    strncpy(string, str1, 3);  
  355.    string[3] = '\0';  
  356.    printf("%s\n", string);  
  357.    return 0;  
  358. }  
  359.    
  360.    
  361.   
  362. 函數名: strnicmp  
  363. 功  能: 不注重大小寫地比較兩個串  
  364. 用  法: int strnicmp(char *str1, char *str2, unsigned maxlen);  
  365. 程序例:  
  366.   
  367. #include <string.h>  
  368. #include <stdio.h>  
  369.   
  370. int main(void)  
  371. {  
  372.    char *buf1 = "BBBccc", *buf2 = "bbbccc";  
  373.    int ptr;  
  374.   
  375.    ptr = strnicmp(buf2, buf1, 3);  
  376.   
  377.    if (ptr > 0)  
  378.       printf("buffer 2 is greater than buffer 1\n");  
  379.   
  380.    if (ptr < 0)  
  381.       printf("buffer 2 is less than buffer 1\n");  
  382.   
  383.    if (ptr == 0)  
  384.       printf("buffer 2 equals buffer 1\n");  
  385.   
  386.    return 0;  
  387. }  
  388.    
  389.    
  390.    
  391.   
  392. 函數名: strnset  
  393. 功  能: 將一個串中的所有字符都設爲指定字符  
  394. 用  法: char *strnset(char *str, char ch, unsigned n);  
  395. 程序例:  
  396.   
  397. #include <stdio.h>  
  398. #include <string.h>  
  399.   
  400. int main(void)  
  401. {  
  402.    char *string = "abcdefghijklmnopqrstuvwxyz";  
  403.    char letter = 'x';  
  404.   
  405.    printf("string before strnset: %s\n", string);  
  406.    strnset(string, letter, 13);  
  407.    printf("string after  strnset: %s\n", string);  
  408.   
  409.    return 0;  
  410. }  
  411.    
  412.    
  413.   
  414. 函數名: strpbrk  
  415. 功  能: 在串中查找給定字符集中的字符  
  416. 用  法: char *strpbrk(char *str1, char *str2);  
  417. 程序例:  
  418.   
  419. #include <stdio.h>  
  420. #include <string.h>  
  421.   
  422. int main(void)  
  423. {  
  424.    char *string1 = "abcdefghijklmnopqrstuvwxyz";  
  425.    char *string2 = "onm";  
  426.    char *ptr;  
  427.   
  428.    ptr = strpbrk(string1, string2);  
  429.   
  430.    if (ptr)  
  431.       printf("strpbrk found first character: %c\n", *ptr);  
  432.    else  
  433.       printf("strpbrk didn't find character in set\n");  
  434.   
  435.    return 0;  
  436. }  
  437.    
  438.    
  439.    
  440.   
  441. 函數名: strrchr  
  442. 功  能: 在串中查找指定字符的最後一個出現  
  443. 用  法: char *strrchr(char *str, char c);  
  444. 程序例:  
  445.   
  446. #include <string.h>  
  447. #include <stdio.h>  
  448.   
  449. int main(void)  
  450. {  
  451.    char string[15];  
  452.    char *ptr, c = 'r';  
  453.   
  454.    strcpy(string, "This is a string");  
  455.    ptr = strrchr(string, c);  
  456.    if (ptr)  
  457.       printf("The character %c is at position: %d\n", c, ptr-string);  
  458.    else  
  459.       printf("The character was not found\n");  
  460.    return 0;  
  461. }  
  462.    
  463.    
  464.    
  465.   
  466. 函數名: strrev  
  467. 功  能: 串倒轉  
  468. 用  法: char *strrev(char *str);  
  469. 程序例:  
  470.   
  471. #include <string.h>  
  472. #include <stdio.h>  
  473.   
  474. int main(void)  
  475. {  
  476.    char *forward = "string";  
  477.   
  478.    printf("Before strrev(): %s\n", forward);  
  479.    strrev(forward);  
  480.    printf("After strrev():  %s\n", forward);  
  481.    return 0;  
  482. }  
  483.    
  484.   
  485. 函數名: strset  
  486. 功  能: 將一個串中的所有字符都設爲指定字符  
  487. 用  法: char *strset(char *str, char c);  
  488. 程序例:  
  489.   
  490. #include <stdio.h>  
  491. #include <string.h>  
  492.   
  493. int main(void)  
  494. {  
  495.    char string[10] = "123456789";  
  496.    char symbol = 'c';  
  497.   
  498.    printf("Before strset(): %s\n", string);  
  499.    strset(string, symbol);  
  500.    printf("After strset():  %s\n", string);  
  501.    return 0;  
  502. }  
  503.    
  504.    
  505.    
  506.   
  507. 函數名: strspn  
  508. 功  能: 在串中查找指定字符集的子集的第一次出現  
  509. 用  法: int strspn(char *str1, char *str2);  
  510. 程序例:  
  511.   
  512. #include <stdio.h>  
  513. #include <string.h>  
  514. #include <alloc.h>  
  515.   
  516. int main(void)  
  517. {  
  518.    char *string1 = "1234567890";  
  519.    char *string2 = "123DC8";  
  520.    int length;  
  521.   
  522.    length = strspn(string1, string2);  
  523.    printf("Character where strings differ is at position %d\n", length);  
  524.    return 0;  
  525. }  
  526.    
  527.    
  528.   
  529. 函數名: strstr  
  530. 功  能: 在串中查找指定字符串的第一次出現  
  531. 用  法: char *strstr(char *str1, char *str2);  
  532. 程序例:  
  533.   
  534. #include <stdio.h>  
  535. #include <string.h>  
  536.   
  537. int main(void)  
  538. {  
  539.    char *str1 = "Borland International", *str2 = "nation", *ptr;  
  540.   
  541.    ptr = strstr(str1, str2);  
  542.    printf("The substring is: %s\n", ptr);  
  543.    return 0;  
  544. }  
  545.    
  546.    
  547.   
  548. 函數名: strtod  
  549. 功  能: 將字符串轉換爲double型值  
  550. 用  法: double strtod(char *str, char **endptr);  
  551. 程序例:  
  552.   
  553. #include <stdio.h>  
  554. #include <stdlib.h>  
  555.   
  556. int main(void)  
  557. {  
  558.    char input[80], *endptr;  
  559.    double value;  
  560.   
  561.    printf("Enter a floating point number:");  
  562.    gets(input);  
  563.    value = strtod(input, &endptr);  
  564.    printf("The string is %s the number is %lf\n", input, value);  
  565.    return 0;  
  566. }  
  567.    
  568.    
  569.    
  570.   
  571. 函數名: strtok  
  572. 功  能: 查找由在第二個串中指定的分界符分隔開的單詞  
  573. 用  法: char *strtok(char *str1, char *str2);  
  574. 程序例:  
  575.   
  576. #include <string.h>  
  577. #include <stdio.h>  
  578.   
  579. int main(void)  
  580. {  
  581.    char input[16] = "abc,d";  
  582.    char *p;  
  583.   
  584.    /* strtok places a NULL terminator 
  585.    in front of the token, if found */  
  586.    p = strtok(input, ",");  
  587.    if (p)   printf("%s\n", p);  
  588.   
  589.    /* A second call to strtok using a NULL 
  590.    as the first parameter returns a pointer 
  591.    to the character following the token  */  
  592.    p = strtok(NULL, ",");  
  593.    if (p)   printf("%s\n", p);  
  594.    return 0;  
  595. }  
  596.    
  597.    
  598.    
  599.   
  600. 函數名: strtol  
  601. 功  能: 將串轉換爲長整數  
  602. 用  法: long strtol(char *str, char **endptr, int base);  
  603. 程序例:  
  604.   
  605. #include <stdlib.h>  
  606. #include <stdio.h>  
  607.   
  608. int main(void)  
  609. {  
  610.    char *string = "87654321", *endptr;  
  611.    long lnumber;  
  612.   
  613.    /* strtol converts string to long integer  */  
  614.    lnumber = strtol(string, &endptr, 10);  
  615.    printf("string = %s  long = %ld\n", string, lnumber);  
  616.   
  617.    return 0;  
  618. }  
  619.    
  620.   
  621. 函數名: strupr  
  622. 功  能: 將串中的小寫字母轉換爲大寫字母  
  623. 用  法: char *strupr(char *str);  
  624. 程序例:  
  625.   
  626. #include <stdio.h>  
  627. #include <string.h>  
  628.   
  629. int main(void)  
  630. {  
  631.    char *string = "abcdefghijklmnopqrstuvwxyz", *ptr;  
  632.   
  633.    /* converts string to upper case characters */  
  634.    ptr = strupr(string);  
  635.    printf("%s\n", ptr);  
  636.    return 0;  
  637. }  
  638.    
  639.    
  640.    
  641.   
  642. 函數名: swab  
  643. 功  能: 交換字節  
  644. 用  法: void swab (char *from, char *to, int nbytes);  
  645. 程序例:  
  646.   
  647. #include <stdlib.h>  
  648. #include <stdio.h>  
  649. #include <string.h>  
  650.   
  651. char source[15] = "rFna koBlrna d";  
  652. char target[15];  
  653.   
  654. int main(void)  
  655. {  
  656.    swab(source, target, strlen(source));  
  657.    printf("This is target: %s\n", target);  
  658.    return 0;  
  659. }   
  660.   
  661. 轉載自:http://www.yuanma.org/data/2006/1029/article_1738.htm  
                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;
} 

轉載自:http://www.yuanma.org/data/2006/1029/article_1738.htm


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章