字符串处理函数

字符串处理函数

一、字符串处理函数

strlen strcpy strcat strcmp

1.1 strlen

功能:

​ 计算字符串的长度,不包含结尾的’\0’的

头文件:

​ #include <String.h>

函数原型:

​ size_t strlen(const char *s);

参数:

​ 要计算长度的字符串的首地址

返回值:

​ 字符串的长度

例:

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

int main(int argc, const char *argv[])
{
	char s1[32] = "hello world";

	//可以直接输出计算的结果
	printf("strlen(s1) = %ld\n", strlen(s1));	//11

	//也可以使用变量来保存计算的结果
	int ret = strlen(s1);
	printf("ret = %d\n", ret);	//11

	//strlen计算长度 遇到 '\0' 就结束了
	char s2[32] = "hello\0worldkjladshfkjalshf";
	printf("strlen(s2) = %ld\n", strlen(s2));	//5

	//如果不是字符串,就不能使用 strlen计算长度!!
	//char s3[5] = {'a', 'b', 'c', 'd', 'e'};
	//printf("strlen(s3) = %ld\n", strlen(s3));	//5
	
	//注意 strlen 和 sizeof 的区别
	//strlen计算的是字符串中从首地址开始到第一个'\0'的字符的个数(不包括'\0')
	//sizeof计算的是占用的内存空间的大小,定义数组是就确定好了
	char s4[16] = "12345";
	printf("strlen(s4) = %ld\n", strlen(s4));//5
	printf("sizeof(s4) = %ld\n", sizeof(s4));//16

	return 0;
}

练习:

思考,如何自己实现 strlen 函数的功能。

#include <stdio.h>

int main(int argc, const char *argv[])
{
	char s[32] = "hello world";

	int count = 0;//用来保存字符的个数
	int i = 0;
#if 0
	//for(i = 0; s[i] != '\0';){  //这种写法也可以
	while(s[i] != '\0'){	//一般长度不确定时  用 while 更何时一些
		count++;
		i++;
	}
#endif

#if 0
	while(s[i] != 0){ // 0 就是 '\0'
		count ++;
		i++;
	}
#endif
	while(s[i]){
		count++;
		i++;
	}

	printf("count = %d\n", count);//11

	return 0;
}

1.2 strcpy

功能:

​ 拷贝字符串,将src拷贝给dest,注意:要保证dest足够大

头文件:

​ #include

函数原型:

​ char *strcpy(char *dest, const char *src);

参数:

​ dest:目的字符串

src:源字符串

返回值:

​ 目标字符串

例:

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

int main(int argc, const char *argv[])
{
	char s1[32] = "hello world";
	char s2[32] = "beijing";

	printf("前 s1 = [%s]\n", s1);//hello world
	printf("前 s2 = [%s]\n", s2);//beijing

	//字符串的拷贝 将s2拷贝给s1
	//要保证s1足够大 否则可能会出现越界访问的问题
	strcpy(s1, s2);

	printf("后 s1 = [%s]\n", s1);//beijing
	printf("后 s2 = [%s]\n", s2);//beijing

	//拷贝的过程是将s2中的字符逐个的赋值给s1 (包括s2的'\0')
	//对于s1多出来的部分,还在s1里面 只不过通过字符串的方式访问不到了
	printf("%c\n", s1[8]);//r
	printf("%c\n", s1[9]);//l
	printf("%c\n", s1[10]);//d

	return 0;
}

练习:

自己尝试实现 strcpy 函数的功能

#include <stdio.h>
int main(){
    char s1[32] = "hello world";
    char s2[32] = "beijing";
    printf("前 s1 = [%s]\n", s1);//hello world
    printf("前 s2 = [%s]\n", s2);//beijing
    //你的操作
    int i = 0;
    while(s2[i] != '\0'){
        s1[i] = s2[i];
        i++;
    }
    //s1[i] = '\0';
    s1[i] = s2[i];//将s2的'\0'也赋值给s1 用上面的写法也可以
    printf("后 s1 = [%s]\n", s1);//beijing
    printf("后 s2 = [%s]\n", s2);//beijing
    return 0;   
}

1.3 strcat

功能:

​ 字符串追加,将src追加到dest后面,

注意:

1.覆盖dest结尾的’\0’

2.要保证dest足够大

头文件:

​ #include

函数原型:

​ char *strcat(char *dest, const char *src);

参数:

​ dest:目的字符串

src:源字符串

返回值:

​ 目标字符串

例:

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

int main(int argc, const char *argv[])
{
	char s1[32] = "hello world";
	char s2[32] = "beijing";
	printf("前 s1 = [%s]\n", s1);//hello world
	printf("前 s2 = [%s]\n", s2);//beijing

	//追加的操作 注意  s1 要足够大
	strcat(s1, s2);

	printf("后 s1 = [%s]\n", s1);//hello worldbeijing
	printf("后 s2 = [%s]\n", s2);//beijing

	return 0;
}

练习:

自己实现 strcat 函数的功能

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

int main(int argc, const char *argv[])
{
    char s1[32] = "hello world";
    char s2[32] = "beijing";
    printf("前 s1 = [%s]\n", s1);//hello world
    printf("前 s2 = [%s]\n", s2);//beijing
	//先找s1的'\0'的位置
     int i = 0;
     while(s1[i]){
         i++;
     }
     //开始进行追加操作
     int j = 0;
     while(s2[j] != '\0'){
         s1[i++] = s2[j++];
     }
     //别忘了将 s2 的'\0' 也追加给 s1
     s1[i] = s2[j];

	printf("后 s1 = [%s]\n", s1);//hello worldbeijing
	printf("后 s2 = [%s]\n", s2);//beijing

	return 0;
}

1.4 strcmp

功能:

​ 比较两个字符串

比较的是两个字符串中第一个’\0’之前,逐个比较字符的ascii码

直到出现大小关系,函数立即返回

只有两个字符串在第一个’\0’之前的所有字符都相等,才认为是相等

头文件:

​ #include

函数原型:

​ int strcmp(const char *s1, const char *s2);

参数:

​ s1 和 s2 就是参与比较的两个字符串

返回值:

​ >0 s1>s2

<0 s1

0 s1s2

例:

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

int main(int argc, const char *argv[])
{
	char s1[32] = {0};
	char s2[32] = {0};
	gets(s1);
	gets(s2);

	int ret1 = strcmp(s1, s2);
	if(ret1 > 0){
		printf("s1 > s2\n");
	}else if(ret1 < 0){
		printf("s1 < s2\n");
	}else{
		printf("s1 == s2\n");
	}

	//strcmp函数的返回值是两个字符串中第一个不相等的字符的ascci码的差值
	//是 s1 - s2 的差值    或者  返回0
	printf("ret1 = %d\n", ret1);


	//关于  strncpy  strncat  strncmp  这几函数
	//表示只操作前n位
	//以 strncmp 为例 其他同理
	char s3[32] = "abcd1234567";
	char s4[32] = "abcd7654321";
	int ret2 = strcmp(s3, s4);  //  ret2 < 0
	int ret3 = strncmp(s3, s4, 4);  //  ret3 == 0
	printf("ret2 = %d  ret3 = %d\n", ret2, ret3); // -6  3

	return 0;
}

字符串处理函数有很多,常用的就是这四个,其他的,感兴趣的同学可以自己查查看。

字符串处理练习:

在终端输入一个字符串,要求整体翻转后输出

如输入:“ab cd 12” --> 则输出 “21 dc ba”

#include <stdio.h>
int main(){
    char s1[32] = {0};
    gets(s1);
    printf("翻转前:s1 = [%s]\n", s1);
    //先找一下最后一个字符的位置
    int i = 0;
    while(s1[i]!='\0'){
        i++;
    }
    i--;
    //上面循环结束的时候 i 在s1的'\0'的位置 所以 得--
    //开始翻转
    int j = 0;
    char temp = 0;
    while(j < i){
        temp = s1[i];
        s1[i] = s1[j];
        s1[j] = temp;
        i--;
        j++;
    }
    
    printf("翻转后:s1 = [%s]\n", s1);
    return 0;
}

作业:

有一个以空格为分隔符的字符串。

“this is a book”

要求以单词为单位进行翻转。

le(s1[i]!=‘\0’){
i++;
}
i–;
//上面循环结束的时候 i 在s1的’\0’的位置 所以 得–
//开始翻转
int j = 0;
char temp = 0;
while(j < i){
temp = s1[i];
s1[i] = s1[j];
s1[j] = temp;
i–;
j++;
}

printf("翻转后:s1 = [%s]\n", s1);
return 0;

}


作业:

有一个以空格为分隔符的字符串。

"this is a book"

要求以单词为单位进行翻转。

"book a is this"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值