BSP Day23

关于字符串的函数的补充:

1.memmove

由src所指内存区域复制count个字节到dest所指内存区域。

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

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

2.memcmp

memcmp是比较内存区域buf1和buf2的前count个字节。该函数是按字节比较的

nt memcmp(const void *buf1, const void *buf2, unsigned int count);

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

int main()
{
	char *pa = NULL;
	char *pb = NULL;
	pa = malloc(sizeof(char)*20);
	pb = malloc(sizeof(char)*50);
	memset(pa,0,sizeof(char)*20); 
	memset(pb,0,sizeof(char)*50);
	strcpy(pa,"hello world");
	strcpy(pb,"BSP2208class");
//	int ret = memcmp(pa,pb,5);
	int ret = strncmp(pa,pb,5);
	printf("%d\n",ret);
	free(pa);
	free(pb);
	pb = NULL;
	pa = NULL;
	
	return 0;
}

3.类型转换函数

 atio转换为整型

atol转换为长整型

atof转换为浮点型

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
//把字符串转换成整型的函数
 
int main()
{
	char str[10] = "-82345678";
	char *pch = NULL;
//	int ret = atoi(str);
//	printf("%d\n",ret);

//	long ret1 = atol(str);
//	printf("%ld\n",ret1);
	
//	double ret2 = atof(str);
//	printf("%10f\n",ret2);

//	printf("%p\n",str);
//	double ret3 = strtod(str,&pch);
//	printf("%p\n",pch);
//	printf("%10f\n",ret3);
	
//	printf("%p\n",str);
//	long ret4 = strtol(str,&pch,10);
//	printf("%p\n",pch);
//	printf("%d\n",ret4);

	printf("%p\n",str);
	unsigned long ret5 = strtoul(str,&pch,10);
	printf("%p\n",pch);
	printf("%lu\n",ret5);
	return 0;
}

 下面来附上一些关于字符串的练习

1.实现删除-一个字符串中的指定字母,如:字符串“abcd”,删除其中的”a”字母,剩余”bcd”,也可
以传递多个需要删除的字符,传递”ab”也可以做到删除”ab”,剩余”cd”。

我先写了一个删除字符的然后写了一个删除字符串的,没看清题意。代码如下

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

int main()
{
	char str1[100];
	char str2[100];
	char ch;
	int i = 0;
	int j = 0;
	printf("请输入原字符串\n");
	scanf("%s",str1);
	getchar();
	printf("请输入要删除的字符\n");
	scanf("%c",&ch);
	for(i=0;i<strlen(str1);i++)
	{
		if(str1[i]!=ch)
		{
			str2[j] = str1[i];
			j++;
		 } 
	
	 } 
	 str2[j] = '\0';
	 printf("删除后的字符串是%s\n",str2);
	return 0;
 } 
#include<stdio.h>
#include<string.h>
int main()
{
	char arr[50]={0};
	printf("请输入一串字符:");
	fgets(arr,50,stdin); 
	char num[20]={0};
	printf("请输入要删除的字符:");
	fgets(num,20,stdin);
	int i,j;
	for(i=0;*(num+i)!='\n';i++)
	{
	j=0;
	for(j=0;*(arr+j)!='\n';j++)
	{
	if(*(arr+j)==*(num+i))
	*(arr+j)=0;
	}
	}
	for(j=0;*(arr+j)!='\n';j++)
	{
	if(*(arr+j)!=0)
	printf("%c",*(arr+j));
	}
	return 0;
}

2. 若s所指向的字符串中找到与参数c相同的字符,在其后插入一个与之相同的字符,若找不到,不做处理

#include<stdio.h>
#include<string.h>
void fun(char *p,char ch)
{
	int i,j,n;
	for(i=0;p[i]!='\0';i++)
	
		if(p[i] == ch)
		{
		n = 0;
		while(p[i+1+n]!='\0')
		n++;
		for(j=i+1+n;j>i;j--)
		p[j+1] = p[j];
		p[j+1] = ch ;
		i = i+1;
		}
}
int main()
{
	char pstr[50] = "iphone";
	char ch;
	printf("原字符串为:%s\n",pstr);
	printf("\n输入与原字符串相同的字符\n");
	scanf("%c",&ch);
	fun(pstr,ch);
	printf("\n字符插入后为:%s\n",pstr);
	return 0;
	}	

 3.  从键盘输入一个字符串s和一个整数n,在字符串中查找字符串后n位的字符串的位置,并返回该字符串

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void fun(char *str,int n)
{
//	char str;
//	int n;
	printf("请输入一个字符串\n");
	gets(str);
	printf("请输入截取位置\n");
	scanf("%d",&n);
	if(n>strlen(str))
	{
		printf("位置过大,输入错误"); 
	}
	else
	printf("截取后字符串是%s\n",str+n-1); 
	
}
int main()
{
	char str[100];
	int n;
	fun(str,n);
	return 0;
 } 

今天的练习题让我意识到了自己还是有些地方不太理解的,还需要努力啊。

等明天再来分享收获。 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

weixiaxiao

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

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

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

打赏作者

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

抵扣说明:

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

余额充值