C语言中字符函数和字符串函数介绍

发布时间:2021-07-30 13:43:43 作者:chen
来源:亿速云 阅读:111

本篇内容介绍了“C语言中字符函数和字符串函数介绍”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

目录

前言

C语言中对字符串的处理很是繁琐,但是C语言本身是没有字符串类型的,字符串通常放在常量字符串中或者字符数组中。字符串常量适用于那些对它不做修改的字符串函数。直接使用下面函数需要包含其头文件**<string.h>**

求字符串长度

strlen介绍

语法/原型:

size_t strlen(const char* str); //str 表示要求长度的字符串。

strlen所作的是一个计数器的工作,它从内存的某个位置(可以是字符串开头,中间某个位置,甚至是某个不确定的内存区域)开始扫描,直到碰到第一个字符串结束符'\0'为止,然后返回计数器值(长度不包含'\0')。

strlen直接使用:

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
int main()
{
	char a[] = "abcdefg";
	char b[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g' };
	int len1 = strlen(a);
	int len2 = strlen(b);
	printf("%d\n", len1);
	printf("%d\n", len2);
	return 0;
}

输出结果:

C语言中字符函数和字符串函数介绍

a,b数组对比:

C语言中字符函数和字符串函数介绍

strlen模拟实现:

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <assert.h>
int my_strlen(const char* str)
{
	int count = 0;//计数器
	assert(str != NULL);
	while (*str != '\0')
	{
		count++;
		str++;
	}
	return count;
}
int main()
{
	char a[] = "abcdefg";
	int len  = my_strlen(a);//使用自己创建的strlen函数来计算数组长度!
	printf("%d\n", len);
	return 0;
}

输出结果:

C语言中字符函数和字符串函数介绍

strlen对比sizeof

strlen()的一种实现就是遍历字符串,遇到'\0'就终止,因而返回的结果是第一个'\0'前字符元素的个数 sizeof 常用来求变量占用内存空间的大小,因而它返回的是存储字符串的变量所占用的内存空间大小,用来求字符串的长度,只在特定情况下可行,即字符数组刚好被一个字符串占满。

代码检验:

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
int main()
{
	char a[] = "abcdefg";
	printf("%d\n", strlen(a));
	printf("%d\n", sizeof(a));//一个字符所占空间为一个字节
	printf("%d\n", (sizeof(a) / sizeof(a[0])));
	return 0;
}

运行结果:

C语言中字符函数和字符串函数介绍

长度不受限制的字符串函数

strcpy

直接使用
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>

int main()
{
	char  a[10];
	char  b[] = "hello bit";
	strcpy(a, b);
	printf("%s", a);
	return 0;
}

运行结果:

C语言中字符函数和字符串函数介绍

模拟实现:
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
void my_strcpy(char *a, const char *b)
{
	int i = 0;
	while (*b != '\0')
	{
		*a = *b;
		*a++;
		*b++;
	}
	*a++ = '\0';
}
int main()
{
	char  a[10];
	char  b[] = "hello bit";
	my_strcpy(a, b);
	printf("%s", a);
	return 0;
}

运行结果同上!

strcat

直接使用:
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>

int main()
{
	char  a[20]="hello ";
	char  b[20] = "bit ";
	char c[20] = "hello ";
	strcat(a, b);
	strcat(b, c);
	printf("%s\n",a);
	printf("%s\n", b);
	return 0;
}

C语言中字符函数和字符串函数介绍

模拟实现:
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
#include<assert.h>
void my_strcat(char *x, const char *y)
{
	assert(x != NULL);
	assert(y != NULL);
	while (*x)//找到目标字符串中的'\0'
	{
		x++;
	}
	while ((*x++ = *y++))
	{
		;
	}

}
int main()
{
	char  a[20]="hello ";
	char  b[20] = "bit ";
	my_strcat(a, b);
	printf("%s\n",a);
	return 0;
}

运行结果:

C语言中字符函数和字符串函数介绍

strcmp

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
#include<assert.h>

int main()
{
	char  a[20]=  "ABCD";
	char  b[20] = "ABCG";
	int i=strcmp(a, b);
	if (i > 0)
	{
		printf("a>b\n");
	}
	else if (i==0)
	{
		printf("a=b\n");
	}
	else
	{
		printf("a<b\n");
	}
	return 0;
}
运行结果:

C语言中字符函数和字符串函数介绍

模拟实现:
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
#include<assert.h>
int my_strcmp(const char *x, const char *y)
{
	int ret = 0;
	assert(x != NULL);
	assert(y != NULL);
	while (!(ret = *(unsigned char *)x - *(unsigned char *)y) && *y)
	{
		++x;
		++y;
	}
	if (ret < 0)
		ret = -1;
	else if (ret >0)
		ret = 1;
	return ret;
}
int main()
{
	char  a[20]=  "ABCD";
	char  b[20] = "ABCG";
	int i=my_strcmp(a, b);
	if (i > 0)
	{
		printf("a>b\n");
	}
	else if (i==0)
	{
		printf("a=b\n");
	}
	else
	{
		printf("a<b\n");
	}
	return 0;
}

运行结果同上!

长度受限制的字符串函数

strncpy

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
int main()
{
	char arr1[20]="1234567";
	char arr2[] = "abcdefg";
	strncpy(arr1, arr2, 3);//拷贝3个字符到arr1数组
	printf("%s\n", arr1);
	return 0;
}

运行结果:

C语言中字符函数和字符串函数介绍

strncat

语法/原型:char * strncat(char *dest, const char *src, size_t n);参数说明: dest指向目标字符串,src为指向源字符串。

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h.>
#include<string.h>
int main()
{
	char arr1[20] = "hello ";
	char arr2[] = "csdn";
	strncat(arr1, arr2, 1);
	printf("%s\n", arr1);
	return 0;
}

C语言中字符函数和字符串函数介绍

strncmp

语法/原型:int strncmp(const char *str1, const char *str2, size_t n)参数说明:str1-- 要进行比较的第一个字符串。str2-- 要进行比较的第二个字符串。n-- 要比较的最大字符数。

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
int main()
{
	char* a = "abcdef";
	char* b = "abchij";
	int i = strncmp(a, b, 4);
	if (i > 0)
	{
		printf("a>b\n");
	}
	else if (i == 0)
	{
		printf("a=b\n");
	}
	else
	{
		printf("a<b\n");
	}
	return 0;
}

C语言中字符函数和字符串函数介绍

字符串查找

strstr

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
int main()
{
	char arr1[] = "hello bit";
	char arr2[] = "bit";
	char* ret = strstr(arr1, arr2);
	if (ret == NULL)
		printf("没找到\n");
	else
		printf("找到了:%s\n", ret);
	return 0;
}

C语言中字符函数和字符串函数介绍

strtok

字符分类函数

C语言中字符函数和字符串函数介绍

内存操作函数

该系列函数所需头文件:#include <string.h>

memcpy

注:这个函数遇到'\0‘的时候并不会停下来。

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
int main()
{
	char arr1[] = "abcdef";
	char arr2[20] = { 0 };
	memcpy(arr2, arr1, 3);//数字3的单位是字节,而一个字符占一个字节
	printf("%s\n", arr2);
	return 0;
}

运行结果:

C语言中字符函数和字符串函数介绍

memmove

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
int main()
{
	const char a[] = "abcdefg";
	const char b[] = "1234567";
	printf("a = %s,b = %s\n", a, b);
	memmove(a, b, 7);
	printf("a = %s,b = %s\n", a, b);
	return(0);
}

C语言中字符函数和字符串函数介绍

memcmp

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
int main()
{
	int  a[] = { 1, 2, 3 };
	int  b[] = { 1, 5};
	int ret = memcmp(a,b, 8);//注意第3个参数的单位是字节
	if (ret > 0)
	{
		printf("a>b\n");
	}
	else if (ret < 0)
	{
		printf("a<b\n");
	}
	else
	{
		printf("a==b\n");
	}
	return 0;
}

“C语言中字符函数和字符串函数介绍”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

推荐阅读:
  1. mysql 基本字符函数
  2. oracle字符函数

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

c语言

上一篇:springboot中怎么使用maven构建docker镜像

下一篇:php-fpm超时时间设置request_terminate_timeout资源问题的示例分析

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》