字符串函数---(1)

字符函数


前言

上一篇我们学习了字符函数,下来我们学习常见的字符串函数


1.strlen 的使用和模拟实现

在这里插入图片描述

size_t strlen(const char *str)

  • 字符串以 ‘\0’ 作为结束标志,strlen函数返回的是在字符串中 ‘\0’ 前面出现的字符个数(不包含 ‘\0’ )。
  • 参数指向的字符串必须要以 ‘\0’ 结束。
  • 注意函数的返回值为 size_t,是无符号的( 易错 )
  • strlen的使用需要包含头文件
  • 学会strlen函数的模拟实现
#include<stdio.h>
#include<string.h>
#include<assert.h>
int my_strlen(const char *str)
{
	int cnt = 0;
	assert(str);
	while (*str)
	{
		cnt++;
		str++;
	}
	return cnt;
}
int main()
{
	char str[30] = "abcdefg";
	//printf("%d", strlen(str));
	int ret=my_strlen(str);
	printf("%d", ret);
	return 0;
}

2.strcpy 的使用和模拟实现

在这里插入图片描述

  • Copies the C string pointed by source into the array pointed by destination, including the
    terminating null character (and stopping at that point).
  • 源字符串必须以 ‘\0’ 结束。
  • 会将源字符串中的 ‘\0’ 拷贝到目标空间。
  • 目标空间必须足够大,以确保能存放源字符串。
  • 目标空间必须可修改。
  • 学会模拟实现。
#include<stdio.h>
#include<string.h>
#include<assert.h>
char* my_strcpy(char* dest, const char* src)
{
	char* ret = dest;
	assert(src && dest);
	while (*src)
	{
		*dest = *src;
		dest++;
		src++;
	}
	return ret;
}
int main()
{
	char str1[] = "hello world!";
	char str2[20] = {0};
	//strcpy(str2, str1);
	my_strcpy(str2, str1);
	printf("%s\n", str2);
	return 0;
}

在这里插入图片描述

3. strcat 的使用和模拟实现

  • Appends a copy of the source string to the destination string. The terminating null character
    in destination is overwritten by the first character of source, and a null-character is included
    at the end of the new string formed by the concatenation of both in destination.
  • 源字符串必须以 ‘\0’ 结束。
  • 目标字符串中也得有 \0 ,否则没办法知道追加从哪里开始。
  • 目标空间必须有足够的大,能容纳下源字符串的内容。
  • 目标空间必须可修改。
  • 字符串自己给自己追加,如何?

char* my_strcat(char* dest, char* src)
{
	char* ret = dest;
	while (*dest)
	{
		dest++;
	}
	while (*src)
	{
		*dest = *src;
		dest++;
		src++;
	}return ret;
}
#include<stdio.h>
#include<assert.h>
#include<string.h>
int main()
{	
	char str1[20] = { "hello," };
	char str2[20] = { "world!" };
	strcat(str1,str2);
	printf("%s\n", str1);
	return 0;
}

在这里插入图片描述

4. strcmp 的使用和模拟实现

• This function starts comparing the first character of each string. If they are equal to each
other, it continues with the following pairs until the characters differ or until a terminating
null-character is reached.

  • 标准规定:
  • 第⼀个字符串大于第⼆个字符串,则返回大于0的数字
  • 第⼀个字符串等于第⼆个字符串,则返回0
  • 第⼀个字符串小于第⼆个字符串,则返回小于0的数字
  • 那么如何判断两个字符串? 比较两个字符串中对应位置上字符ASCII码值的大小。
#include<string.h>
#include<stdio.h>
#include<assert.h>
int my_strcmp(const char* str1, const char* str2)
{
	int ret = 0;
	assert(str1 != NULL);
	assert(str2 != NULL);
	while (*str1 == *str2)
	{
		if (*str1 == '\0')
			return 0;
		str1++;
		str2++;
	}
	return *str1 - *str2;

}
int main()
{
	char str1[] = "abcd";
	char str2[] = "abqd";
	//int ret = strcmp(str1, str2);
	int ret = my_strcmp(str1, str2);
	printf("%d", ret);
	return 0;
}

相关推荐

  1. Clickhouse 字符串函数 - 1

    2024-03-25 04:36:01       41 阅读
  2. 模拟实现字符串函数1(详细版本)

    2024-03-25 04:36:01       31 阅读
  3. 字符函数字符串函数

    2024-03-25 04:36:01       53 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-25 04:36:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-25 04:36:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-25 04:36:01       82 阅读
  4. Python语言-面向对象

    2024-03-25 04:36:01       91 阅读

热门阅读

  1. ftp协议的彻底研究

    2024-03-25 04:36:01       35 阅读
  2. c++和c语言的区别实例

    2024-03-25 04:36:01       40 阅读
  3. 再次度过我的创作纪念日

    2024-03-25 04:36:01       34 阅读
  4. MySQL索引介绍

    2024-03-25 04:36:01       35 阅读
  5. Qt笔记 事件分发

    2024-03-25 04:36:01       37 阅读
  6. Qt:使用ctrl+z快捷键取消文本框修改

    2024-03-25 04:36:01       39 阅读
  7. Android Selinux详解[七]--如何给可执行程序bin加标签

    2024-03-25 04:36:01       35 阅读
  8. ES间的导数脚本

    2024-03-25 04:36:01       40 阅读
  9. clickhouse介绍

    2024-03-25 04:36:01       41 阅读
  10. 如何借助API提升产品设计的用户体验

    2024-03-25 04:36:01       38 阅读