c庫函數——字符串函數

Code:
char   stpcpy(char*dest,const char *src)
       
將字符串src複製到dest
char   strcat(char*dest,const char *src)
       
將字符串src添加到dest末尾
char   strchr(constchar *s,int c)
       
檢索並返回字符c在字符串s中第一次出現的位置
int   strcmp(const char *s1,const char *s2)
       
比較字符串s1s2的大小,並返回s1-s2
char   strcpy(char*dest,const char *src)
       
將字符串src複製到dest
size_t strcspn(const char *s1,const char *s2)
       
掃描s1,返回在s1中有,s2中也有的字符個數
char   strdup(constchar *s)
       
將字符串s複製到最近建立的單元
int   stricmp(const char *s1,const char *s2)
       
比較字符串s1s2,並返回s1-s2
size_t strlen(const char *s)
       
返回字符串s的長度
char   strlwr(char*s)
       
將字符串s中的大寫字母全部轉換成小寫字母,並返回轉換後的字符串
char   strncat(char*dest,const char *src,size_t maxlen)
       
將字符串src中最多maxlen個字符複製到字符串dest
int   strncmp(const char *s1,const char *s2,size_t maxlen)
       
比較字符串s1s2中的前maxlen個字符
char   strncpy(char*dest,const char *src,size_t maxlen)
       
複製src中的前maxlen個字符到dest
int   strnicmp(const char *s1,const char *s2,size_t maxlen)
       
比較字符串s1s2中的前maxlen個字符
char   strnset(char*s,int ch,size_t n)
       
將字符串s的前n個字符置於ch
char  strpbrk(const char *s1,const char *s2)
       
掃描字符串s1,並返回在s1s2中均有的字符個數
char  strrchr(const char *s,int c)
       
掃描最後出現一個給定字符c的一個字符串s
char   strrev(char*s)
       
將字符串s中的字符全部顛倒順序重新排列,並返回排列後的字符串
char   strset(char*s,int ch)
       
將一個字符串s中的所有字符置於一個給定的字符ch
size_t strspn(const char *s1,const char *s2)
       
掃描字符串s1,並返回在s1s2中均有的字符個數
char   strstr(constchar *s1,const char *s2)
       
掃描字符串s2,並返回第一次出現s1的位置
char   strtok(char*s1,const char *s2)
       
檢索字符串s1,該字符串s1是由字符串s2中定義的定界符所分隔
char   strupr(char*s)
       
將字符串s中的小寫字母全部轉換成大寫字母,並返回轉換後的字符串

 

詳細例子
函數名: stpcpy
功  能: 拷貝一個字符串到另一個
用  法: char *stpcpy(char *destin, char*source);
程序例:
#include <stdio.h>
#include <string.h>
int main(void)
{
   charstring[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)
{
   chardestination[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, charc);
程序例:
#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;
}
 
 

發佈了95 篇原創文章 · 獲贊 2 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章