首页 > 其他分享 >104.字符串函数:strlen函数,strcpy函数,strcat函数,strcmp函数

104.字符串函数:strlen函数,strcpy函数,strcat函数,strcmp函数

时间:2023-07-15 11:14:46浏览次数:43  
标签:p2 p1 函数 str1 strcat char strcpy printf include

104.字符串函数:strlen函数,strcpy函数,strcat函数,strcmp函数

1.字符串函数strlen

(1)strlen函数

strlen函数返回的是在字符串中’\0’前面出现的字符的个数

(2)strlen的使用

a.代码

#include<stdio.h>
#include<string.h>
int main()
{
	char str1[] = "abcdef";
	printf("%d\n", strlen(str1));
	return 0;
}

b.运行结果

 6

(3)模拟实现strlen函数

size_t strlen(const char *str);

a.代码

#include<stdio.h>
#include<string.h>
size_t MyStrlen(const char* str1)
{
	size_t len = 0;
	while (*str1 != 0)
	{
		++len;
		++str1;
	}
	return len;
}

int main()
{
	char* str1 = "abcdef";
	printf("%d\n", MyStrlen(str1));
	return 0;
}

b.运行结果

 6

(4)注意:

a.指针在传递(赋值,传参数)的过程中,权限不能放大,只能缩小

b.typedef unsigned int size_t
size_t就是无符号的整型

2.字符串函数strcpy

(1)strcpy函数

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

strcpy是覆盖拷贝,将source全覆盖拷贝到destination,会把’\0’也拷过去,且必须考虑destination的空间够不够
(destination的空间必须>=source的空间)

(2)strcpy的使用

a.代码

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
int main()
{
	char p1[] = "abcdef";
	char* p2 = "hello";
	strcpy(p1, p2);
	printf("%s\n", p1);
	printf("%s\n", p2);
	return 0;
}

b.运行结果

hello
hello

c.错误举例

代码:

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
int main()
{
	char p1[] = "abcdef";
	char* p2 = "hello";
	const char* p3 = "world";
	strcpy(p2, p3);
	printf("%s\n", p2);
	printf("%s\n", p3);
	return 0;
}

错误分析:

(3)模拟实现strcpy

a.代码

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
void MyStrcpy(char* dst, const char* src)
{
	while (*src)
	{
		*dst = *src;
		++src;
		++dst;
	}
	*dst = '\0';
}
int main()
{
	char p1[] = "abcdef";
	const char* p2 = "hello";
	MyStrcpy(p1, p2);
	printf("%s\n", p1);
	printf("%s\n", p2);
	return 0;
}

b.运行结果

hello
hello

3.字符串函数strcat

(1)strcat函数

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

strcat追加拷贝,追加到目标空间后面,目标空间必须足够大,能容纳下源字符串的内容

(2)strcat的使用

a.代码

#include<stdio.h>
#include<string.h>
int main()
{
	char p1[20] = "hello";
	const char* p2 = " world";
	strcat(p1, p2);
	printf("%s\n",p1);
	return 0;
}

b.运行结果

hello world

(3)模拟实现strcat

a.代码

#include<stdio.h>
#include<string.h>
void MyStrcat(char* dst, const char * src)
{
	//让dst指向'\0'位置
	while (*dst != '\0')
	{
		++dst;
	}
	//让dst从'\0'开始,将src赋值给dst
	while (*dst = *src)
	{
		++dst;
		++src;
	}
	*dst = '\0';
}
int main()
{
	char p1[20] = "hello";
	const char* p2 = " world";
	MyStrcat(p1, p2);
	printf("%s\n",p1);
	return 0;
}

b.运行结果

hello world

4.字符串函数strcmp

(1)strcmp函数

int strcmp(const char * str1, const char * str2);

strcmp比较两个字符串的大小,一个字符一个字符比较,按ASCLL码比较
标准规定:
第一个字符串大于第二个字符串,则返回大于0的数字
第一个字符串等于第二个字符串,则返回0
第一个字符串小于第二个字符串,则返回小于0的数字

(2)strcmp的使用

a.代码

#include<stdio.h>
#include<string.h>
int main()
{
	char* p1 = "abcdef";
	char* p2 = "abcdef";
	char* p3 = "abcd";
	char* p4 = "bcde";
	printf("%d\n", strcmp(p1,p2 ));
	printf("%d\n", strcmp(p1,p3 ));
	printf("%d\n", strcmp(p3,p4 ));
}

b.运行结果

0
1
-1

(3)模拟实现strcmp

a.代码

#include<stdio.h>
#include<string.h>
int MyStrCmp(const char * str1, const char * str2)
{
	//逐个元素比较
	while (*str1 && *str2)
	{
		if (*str1 > *str2)
		{
			return 1;
		}
		else if (*str1 < *str2)
		{
			return -1;
		}
		else //如果两个元素相等,进入下一个继续比较
		{
			++str1;
			++str2;
		}
	}
	//str2比较完了,str1还有
	if (*str1)
	{
		return 1;
	}
	//str1比较完了,str2还有
	else if (*str2)
	{
		return -1;
	}
	//str1等于str2
	else
	{
		return 0;
	}
}
int main()
{
	char* p1 = "abcd";
	char* p2 = "abcd";
	char* p3 = "abcde";
	char* p4 = "bcd";
	char* p5 = "b";
	printf("%d\n", MyStrCmp(p1,p2 ));
	printf("%d\n", MyStrCmp(p1,p3 ));
	printf("%d\n", MyStrCmp(p1,p4 ));
	printf("%d\n", MyStrCmp(p4,p5 ));
	return 0;
}

b.运行结果

0
-1
-1
1

参考资料来源:

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_50886514/article/details/112606595

标签:p2,p1,函数,str1,strcat,char,strcpy,printf,include
From: https://www.cnblogs.com/codemagiciant/p/17555810.html

相关文章

  • SAP ABAP 函数 TR_REQUEST_CHOICE
    TR_REQUEST_CHOICE是SAPABAP中的一个函数模块,它用于在系统中处理传输请求。传输请求是SAP系统中的一个重要概念,它用于管理和控制系统中对象的传输。这些对象可以是程序、表、视图等。TR_REQUEST_CHOICE函数模块提供了一种界面,允许用户在系统中选择一个传输请求。它有一个......
  • 客户端函数通过网络调用服务器端函数
    客户端函数通过网络调用服务器端函数序列化:序列化是一种用来处理对象流的机制,所谓对象流也就是将对象的内容进行流化。可以对流化后的对象进行读写操作,也可将流化后的对象传输于网络之间。序列化是为了解决在对对象流进行读写操作时所引发的问题。序列化的实现:将需要......
  • 150.vuerouter中的导航钩子函数
    150.vue-router中的导航钩子函数(1)全局的钩子函数beforeEach和afterEachbeforeEach有三个参数,to代表要进入的路由对象,from代表离开的路由对象。next是一个必须要执行的函数,如果不传参数,那就执行下一个钩子函数,如果传入false,则终止跳转,如果传入一个路径,则导航到对应的......
  • 指针数组,数组指针,函数
    指针数组指针数组,首先它是一个数组,数组里面的存储的是一个个指针,例如int*p[5];,指针数组里面的元素大小都是一样的,都是一个指针的大小,也就是8个字节(64位机器),sizeof(p);就为40个字节。下标的本质:下标的本质就是偏移量,[]的含义是解引用#include<stdio.h>intmain(void){ in......
  • SQL注入问题、视图、触发器、事务、存储过程、函数、流程控制、索引、测试索引
    SQL注入问题连接MySQL服务器conn=pymysql.connect(host=‘127.0.0.1’port=3306user=‘root’password='1234'......
  • 聚合函数
    Oracle支持许多内建的聚合函数,可以对数据进行统计汇总。常用的聚合函数如下:COUNT:统计行数SUM:求和AVG:平均值MAX:最大值MIN:最小值STDDEV:标准差VARIANCE:方差例如:--统计employees表的行数SELECTCOUNT(*)FROMemployees;--求employees表的工资总和SELECT......
  • Qt信号槽信号函数重载问题 error: C2664: “QMetaObject::Connection const”
    //connect(spinFontSize,&QSpinBox::valueChanged,this,&MainWindow::spinFontSize_valueChanged);//由于信号函数存在重载,发送者找不到正确信号函数。//改用A.Qt4带形参方式//connect(spinFontSize,SIGNAL(valueChanged(int)),this,SLOT(spinFontSize_valueChang......
  • 【JavaScript】js 处理复制函数实现
    exportconstcopyText=(text:string)=>{constinput=document.createElement('input');input.setAttribute('readonly','readonly');input.setAttribute('value',text);document.body.appendChild(input);......
  • Math函数之Random随机数、Date日期
    publicstaticvoidmain(String[]args)throwsParseException{Datedate1=newDate();//nowDatedate2=newDate(0);//计算机元年Datedate3=newDate(Long.MAX_VALUE);//毫秒数Datedate4=newDate(Long.MIN_VALUE);......
  • Cygwin、Linux Bash计算某个时刻偏移一定时间长度后的时间通用函数:datetimecount
    datetimecount函数代码datetimecount(){ #计算某个日期时间偏移一定时间长度后的时刻(目前主要供录制IPTV直播源时计算视频时长使用) #$1-->偏移量:符合date命令的描述参数即可,也支持传递标准时间格式:eg:+01:23:35(标记符号(加减号)可省略,小时字段可省略) #$2-->要计算偏移......