字符串处理的常用函数

1函数名:strcpy
功能
:拷贝一个字符串到另一个字符串里面
用法:char *strcpy(char *destin, char *source);
程序例:

#include<stdio.h>
#include <string.h>
int main(){
  char string[10];
  char *str1 = "abcdefghi";

  strcpy(string,str1);
  printf("%s\n", string);
   return 0;
}


2函数名:strcat
功能
:字符串拼接函数,拼接的两个数组不能是相同的。
用法:char *strcat(char *destin, char *source);
程序例:

#include<string.h>
#include <stdio.h>

int main(){
   char destination[25];
  char *blank = " ", *c = "C++", *Borland ="Borland";

  strcpy(destination,Borland);
   strcat(destination, blank);
  strcat(destination, c);

   printf("%s\n",destination);
   return 0;
}


3函数名: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;
}


4函数名: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("buffer2 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 isless than buffer 3\n");

  return 0;
}


5函数名:strncmp
功能:比较字符串的前maxlen个字符

形式: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 greaterthan 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 greaterthan buffer 3\n");
   else
     printf("buffer 2 is less than buffer 3\n");

   return(0);
}




6函数名:strncpy
功能:串拷贝,将第二个字符maxlen个字符拷贝给第一个字符串

用法: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;
}


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


8 C/C++中的Split函数1C/C++中的Split函数是strtok()其函数原型如下:char * strtok (char * str, const char * delimiters);
函数说明
strtok()
用来将字符串分割成一个个片段。参数
str指向欲分割的字符串,参数delimiters则为分割字符串,当strtok()在参数str的字符串中发现到参数delimiters的分割字符时则会将该字符改为'\0'字符。在第一次调用时,strtok()必需给予参数str字符串,往后的调用则将参数str设置成NULL。每次调用成功则返回下一个分割后的字符串指针。

#include<stdio.h>
#include <string.h>
int main (){
   char str[] ="a,b,c,d*e";
   const char *split = ",";
   char * p;
   p = strtok(str,split);
   while(p!=NULL) {
        printf("%s\n",p);
        p = strtok(NULL,split);
  }
   getchar();
   return 0;
}
/*
本例中,实现对字符串'a,b,c,d*e"用逗号(,)来作界定符对字符串进行分割。
输出结果将如下所示:
a
b
c
d*e

因为delimiters支持多个分割符,我们将本示例中的语句行
constchar * split = ",";
改成constchar * split = ",*";//用逗号(,)和星号(*)对字符串进行分割

这样输出结果将如下所示:
a
b
c
d
e 
*/





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值