字符串处理的常用函数

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

                                          

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 
*/





目录
相关文章
|
2月前
|
C++
18字符串处理函数
18字符串处理函数
12 0
|
2月前
|
索引 Python
使用字符串处理函数的文章
在编程中,字符串处理是一项常见的任务,涉及到对文本数据的各种操作,如查找、替换、分割、连接等。为了简化这些操作,许多编程语言都提供了一系列的字符串处理函数。本文将介绍一些常用的字符串处理函数,并附上相应的代码示例。
13 0
|
11月前
|
C++
C/C++字符串处理函数
C/C++字符串处理函数
字符串处理函数总结
使用字符串处理函数 注意:在使用后字符串处理函数时,应当在程序文件的开头用
53 0
字符串函数(一)之常见用法
计算字符串长度 但应注意 使用 string.h头文件 strlen函数返回值为 unsigned int
102 0
|
Shell 编译器 iOS开发
字符串处理函数1
一、字符数组 二、字符串处理函数
字符串处理方法
字符串处理方法
89 0
字符串处理方法