字符串处理函数

来源:互联网 发布:词频统计算法 编辑:程序博客网 时间:2024/05/16 01:34
C语言提供了丰富的字符串处理函数,大致可分为字符串的输入、输出、合并、修改、比较、转换、复制、搜索几类。 使用这些函数可大大减轻编程的负担。用于输入输出的字符串函数,在使用前应包含头文件"stdio.h",使用其它字符串函数则应包含头文件"string.h"。

下面介绍几个最常用的字符串函数。

字符串输出函数 puts

格式:  puts(字符数组名)
功能:把字符数组中的字符串输出到显示器。 即在屏幕上显示该字符串。

【例7-12】
  1. #include"stdio.h"
  2. main(){
  3. char c[]="BASIC\ndBASE";
  4. puts(c);
  5. }
从程序中可以看出puts函数中可以使用转义字符,因此输出结果成为两行。puts函数完全可以由printf函数取代。当需要按一定格式输出时,通常使用printf函数。

字符串输入函数 gets

格式:  gets (字符数组名)
功能:从标准输入设备键盘上输入一个字符串。

本函数得到一个函数值,即为该字符数组的首地址。

【例7-13】
  1. #include"stdio.h"
  2. main(){
  3. char st[15];
  4. printf("input string:\n");
  5. gets(st);
  6. puts(st);
  7. }

可以看出当输入的字符串中含有空格时,输出仍为全部字符串。说明gets函数并不以空格作为字符串输入结束的标志,而只以回车作为输入结束。这是与scanf函数不同的。

字符串拷贝函数strcpy

格式:  strcpy(字符数组名1,字符数组名2)       用  法: char *stpcpy(char *destin, char *source); 
功能:把字符数组2中的字符串拷贝到字符数组1中。串结束标志“\0”也一同拷贝。字符数名2,也可以是一个字符串常量。这时相当于把一个字符串赋予一个字符数组。

【例7-15】
  1. #include"string.h"
  2. main(){
  3. char st1[15],st2[]="C Language";
  4. strcpy(st1,st2);
  5. puts(st1);printf("\n");
  6. }
本函数要求字符数组1应有足够的长度,否则不能全部装入所拷贝的字符串。

字符串连接函数 strcat

格式:  strcat(字符数组名1,字符数组名2)
功能:把字符数组2中的字符串连接到字符数组1 中字符串的后面,并删去字符串1后的串标志“\0”。本函数返回值是字符数组1的首地址。

【例7-14】
  1. #include"string.h"
  2. main(){
  3. static char st1[30]="My name is ";
  4. int st2[10];
  5. printf("input your name:\n");
  6. gets(st2);
  7. strcat(st1,st2);
  8. puts(st1);
  9. }
本程序把初始化赋值的字符数组与动态赋值的字符串连接起来。要注意的是,字符数组1应定义足够的长度,否则不能全部装入被连接的字符串。

函数名: 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: %dn", c, ptr-string); 
    else 
       printf("The character was not foundn"); 
    return 0; 


函数名: strcmp 
功  能: 比较字符串str1和str2。
用  法: int strcmp(char *str1, char *str2); 
说  明:  当s1<s2时,返回值<0   
           当s1=s2时,返回值=0   
           当s1>s2时,返回值>0   
           即:两个字符串自左向右逐个字符相比(按ASCII值大小相比较),直到出现不同的字符或遇'\0'为止。
程序例: 
#include<stdio.h>   
#include<string.h>   
void main()   
{   
    char string[20];   
    char str[3][20];   
    int i;   
    for(i=0;i<3;i++)   
    gets(str[i]);   
    if(strcmp(str[0],str[1])>0)   
    strcpy(string,str[0]);   
    else   
    strcpy(string,str[1]);   
    if(strcmp(str[2],string)>0)   
    strcpy(string,str[2]);   
    printf("\nThe largest string is %s\n",string);   


函数名: 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 1n"); 
   if (ptr < 0) 
      printf("buffer 2 is less than buffer 1n"); 
   if (ptr == 0) 
      printf("buffer 2 equals buffer 1n"); 
   return 0; 


测字符串长度函数strlen

格式:  strlen(字符数组名)
功能:测字符串的实际长度(不含字符串结束标志‘\0’)并作为函数返回值。

【例7-17】
纯文本复制
  1. #include"string.h"
  2. main(){
  3. int k;
  4. static char st[]="C language";
  5. k=strlen(st);
  6. printf("The lenth of the string is %d\n",k);
  7. }
  




0 0
原创粉丝点击