IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> Day023 C语言的字符串函数2 -> 正文阅读

[C++知识库]Day023 C语言的字符串函数2

一.strlwr与strupr;大小写转换函数
strlwr与strupr函数的格式:
char *strlwr(char *s1);
char *strupr(char *s1);
对strlwr函数进行封装:

char *strlwr_s(char *src)
{
	int i;
	while(src[i]!='\0')
	{
		if(src[i]>='A'&&src[i]<='Z')
		{
			src[i] += 32;
		}
		i++;
	}
	return src;
}


对strupr函数进行封装:

char *strupr_s(char *src)
{
	int i;
	while(src[i]!='\0')
	{
		if(src[i]>='a'&&src[i]<='z')
		{
			src[i]-=32;
		}
		i++;
	}
	return src;
}


代码展示:

#include<stdio.h>
#include<string.h>
char *strlwr_s(char *src);
char *strupr_s(char *src);
int main()
{
	char str[20] = "saacdsCWDFDdsc";
	char *p = strlwr(str);
	printf("%s\n",p);
	char *p1 = strlwr_s(str);
	printf("%s",p1);


	char *p2 = strupr(str);
	printf("%s\n",p2);
	char *p3 = strupr_s(str);
	printf("%s\n",p3);
	return 0;
}

运行结果:

?

二.atoi与atol&atof;字符串转换函数(tips:这三者仅适用纯数字字符串)


atoi与atol&atof函数的格式:
int _cdecl atoi(const char *str);

long _cdecl atol(const char *str);

double_cdecl atof(const char *str);

代码展示:

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

int main()
{
	char str[64] = "dvdsvds";
	char *pch = NULL;
	int ret = atoi(str);
	printf("%d\n",ret);
	
	long ret1 = atol(str);
	printf("%ld\n",ret1);
	
	double ret2 = atof(str);
	printf("%f\n",ret2);

运行结果:

?


三. strtod(转换为实型函数),strtol(转换为long型函数),strtoul(转换为无符号long类型函数)?


strtod的定义格式:double strtod(const char *str, const char **endptr);

strtol的定义格式:long?strtol(const char *str, const char **endptr,int radix);

strtoul的定义格式:unsigned long?strtoul(const char *str, const char **endptr,int radix);

代码展示:

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

int main()
{
    char str[10] = "802345671";
	char *pch  =NULL;

	printf("%p\n",str);
	double ret3 = strtod(str,&pch);
	printf("%p\n",pch);
	printf("%f\n",ret3);
	printf("%p\n",str);
	
	long ret4 = strtol(str,&pch,10);
	printf("%p\n",pch);
	printf("%ld\n",ret4);
	
	unsigned long ret5 = strtoul(str,&pch,10);
	printf("%p\n",pch);
	printf("%lu\n",ret5);
	
	return 0;
}

运行结果:

四:malloc:

用法以及代码展示:

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

int main()
{
	char *pa = NULL;
	char *pb = NULL;
	pa = (char *)malloc(sizeof(char)*20);
	pb = (char *)malloc(sizeof(char)*30);
	memset(pa,0,sizeof(char)*20);
	memset(pb,0,sizeof(char)*30);
	
//	int i;
//	for(i=0;i<10;i++)
//	{
//		pa[i] = 'a'+i; 
//	}
//	
//	for(i=0;i<20;i++)
//	{
//		pb[i] = 'A'+i; 
//	}
	
//	pa = "hello world";
//	pb = "BSP2208class";
	strcpy(pa,"hello world");
	strcpy(pb,"BSP2208class");
	printf("%s\n",pa);
	printf("%s\n",pb);
//	strcpy(pb,pa);
	memcpy(pb,pa,sizeof(char)*10);
	printf("%s\n",pb);
//	for(i=0;i<30;i++)	
//	{
//		printf("%d\n",pb[i]);
//	}
	
	
	free(pa);
	free(pb);
	pa = NULL;
	pb = NULL;
	
	return 0;
}

输出结果:

?

五:memcpy:

格式: void *memcp(void *dest,const void *source,size_t n);

可以除了字符型拷贝其他数据类型 ,strcpy只能copy字符型

代码展示:

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

int main()
{
//	char src[20] = "hello world";
//	char dest[30] = "BSP2208calss";

	int arr[10] = {1,2,3,4,5,6,7,8,9,10};
	int brr[20] = {11,21,31,41,51,61,71,81,91,110,111,112,113};
//	strcpy(dest,src);
//	printf("%s\n",dest);
//	memcpy(dest,src,10);
//	printf("%s\n",dest);

	memcpy(brr,arr,sizeof(int)*10);
	int i=0;
	for(i=0;i<20;i++)
	{
		printf("%d ",brr[i]);
	}
	
	
	return 0;
}

运行结果:

?

六:memmove:

?格式:void *memmove(void *dest,const void *src, size_t n);

代码展示:

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

int main()
{
	char *pa = NULL;
	char *pb = NULL;
	pa = malloc(sizeof(char)*20);
	pb = realloc(pa,sizeof(char)*50);
	printf("%p\n",pa);
	printf("%p\n",pb);
	
	strcpy(pa,"hello world");
	strcpy(pb,"BSP2208class");
	memmove(pb,pa,sizeof(char)*10);
	printf("%s\n",pb);
	
	free(pb);
	pb = NULL;
	
	return 0;
}

运行结果:

七:memcmp;

?

格式:int memcmp(const void *ptrl,const void* ptr2,size_t num);

代码展示:

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

int main() 
{
	int *pa = NULL;
	int *pb = NULL;
	pa = malloc(sizeof(int)*10);
	pb = malloc(sizeof(int)*20);
	memset(pa,0,10);
	memset(pb,0,10);
//	strcpy(pa,"BSP220llo world");
//	strcpy(pb,"BSP22e8");

	int i;
	for(i=0;i<10;i++)
	{
		pa[i] = i+10;
	}
	for(i=0;i<20;i++)
	{
		pb[i] = i+20;
	}
	
	int ret = memcmp(pa,pb,6);
	printf("%d\n",ret);
	
	free(pa);
	free(pb);
	pa = NULL;
	pb = NULL;
	
	
	
	return 0;
}

运行结果:

?


?

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2022-09-13 10:54:41  更:2022-09-13 10:59:18 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/19 14:55:17-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码