字符串、内存函数的介绍

1. strlen(求字符串长度)

size_t strlen(const char *str)//(size_t是一个无符号整数)

需要注意的点:

  1. 参数指向的字符串必须要以‘\0’结尾;
  2. 其返回是值是个无符号整数,且不包含‘\0’
#include <stdio.h>
#include <string.h>
int main()
{
   
const char*str1 = "abcdef";
const char*str2 = "bbb";
if(strlen(str2)-strlen(str1)>0)
{
   
printf("str2>str1\n");
}
else
{
   
printf("srt1>str2\n");
}
return 0;
}

2. strcpy(字符串拷贝)

char* strcpy(char * destination, const char * source );

需要注意的点:

  1. 源字符串必须以‘\0’结尾,在拷贝时‘\0’也会发生拷贝;
  2. 目标空间必须足够大且空间可变。
#include<stdio.h>
#include <string.h>
#include<Windows.h>

void Print(char *arr,int num)
{
   
	int i = 0;
	for (i = 0; i < num; i++){
   
		printf("%c", arr[i]);
	}
	printf("\n");
}
int main()
{
   
	const char* src = "hello world!";
	char dst[16];
	strcpy(dst, src);
	Print(dst,strlen(dst));
	system("pause");
	return 0;
}

3. strcat(字符串拼接)

char * strcat ( char * destination, const char * source );

需要注意的点:

  1. 源字符串必须以 ‘\0’ 结束;
  2. 目标空间必须可修改,且足够大,能容纳下源字符串的内容;
#include<stdio.h>
#include <string.h>
#include<Windows.h>
void Print(char *arr,int num)
{
   
	int i = 0;
	for (i = 0; i < num; i++){
   
		printf("%c", arr[i]);
	}
	printf("\n");
}
int main()
{
   
	const char* src = "hello ";
	char dst[16] = "world!";
	strcat(dst, src
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值