Day 23 C语言 字符串 strlwr,strupr, atoi,atol,atof,strtod,strtol,strtloul,memcpy, memove, memcmp

目录

1.strlwr strupr

2.atoi atol atof

3.strtod

 4.strtol,strtloul

5. memcpy, memove, memcmp


1.strlwr strupr

char *strlwr(char *s1) 大写转小写 
char *strupr(char *s1) 小写转大写 

以下是对strlwr和struper的函数封装

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

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

2.atoi atol atof

int atoi(const char *str)

 把参数 str 所指向的字符串转换为一个整数(类型为 int 型)

long int atol(const char *str)

 把参数 str 所指向的字符串转换为一个长整数(类型为 long int 型)

double atof(const char *str)

 把参数 str 所指向的字符串转换为一个浮点数(类型为 double 型)

3.strtod

double strtod(const char *str, char **endptr)

 把参数 str 所指向的字符串转换为一个浮点数(类型为 double 型)。如果 endptr 不为空,则指向转换中最后一个字符后的字符的指针会存储在 endptr 引用的位置

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

int main()
{
  char str[30] = "20.30300 This is test";
   char *ptr;
   double ret;

   ret = strtod(str, &ptr);
   printf("数字(double)是 %lf\n", ret);
   printf("字符串部分是 |%s|", ptr);

   return(0);
}

 4.strtol,strtloul

将字符型的数,转换成strtol长整型和strtloul不带符号的长整型、

	char str[20] ="82345671";
	char *pch =NULL;
	long ret4 =strtol(str,&pch,10);//有符号long 
	printf("%p\n",pch);
	printf("%ld\n",ret4);
	
	unsigned long ret5 =strtoul(str,&pch,10);//无符号long 
	printf("%p\n",pch);
	printf("%lu\n",ret5);

5. memcpy, memove, memcmp

  • memcpy可以拷贝除了字符类型的其他数据类型的数据,strcpy只能拷贝字符型数据;

他们两个的区别是strcpy只能拷贝字符类型的 ;memcpy()可以拷贝任意类型拷贝 

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

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

	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);//(目的数组)(拷贝数组)从src到dest 
//	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()和memcpy的区别就是memmove函数是处理的源内存块和目标内存块是可以重叠的。
#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,"happy birthday to you");
	strcpy(pb,"BSP2208 class");
	memmove(pb,pa,sizeof(char)*10);
	printf("%s\n",pa);
	
	free(pb);
	pb=NULL;
	return 0;
}
  • memcmp()把存储区str1和存储区str2的前n个字节进行比较。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
//memcmp()把存储区str1和存储区str2的前n个字节进行比较 
int main()
{
	char *pa = NULL;
	char *pb = NULL;
	pa = malloc(sizeof(char)*10);
	pb = malloc(sizeof(char)*20);
	memset(pa,0,10);
	memset(pb,0,10);
	strcpy(pa,"BSP220 world");
	strcpy(pb,"BSP22e8");
	int ret = memcmp(pa,pb,6);
	printf("%d\n",ret);
	free(pa);
	free(pb);
	pa = NULL;
	pb = NULL;
	return 0;
 } 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

慕容离875

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值