XDOJ 233/237-字符串复制

问题描述

给定一个的字符串,从第m个字符开始复制成为另一个字符串。编写函数copystr( )完成字符串复制,使用字符类型的指针变量作为形参,主函数完成字符串输入和m值的输入,函数调用,以及最后结果的输出。

输入说明

第一行从键盘输入一个字符串,该字符串中字符可以是字母、数字、空格和其它字符。字符串总长不超过50个字符。

第二行输入整数m的值。

输出说明

若该字符串长度小于m,则输出error,否则输出从该字符串第m个字符复制的字符串。

输入样例1

Abc ba/!.123

3

输入样例2

abced

6

输出样例1

c ba/!.123

输出样例2

error

 1.指针式

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

void copystr(char *str, int m, char *copy);

int main()
{
	char a[51]={NULL}, b[51]={NULL};
	gets(a);
	int m, len=strlen(a);
	scanf("%d",&m);
	if(m>len)	printf("error");
	else	copystr(a,m,b);
	puts(b);
	return 0;
}

void copystr(char *str, int m, char *copy)
{
	int len = strlen(str); 
	char *p = str+m-1;
	char *i = copy;
	while(p<str+len)
	{
		*i = *p;//i对应的是copy,p对应的是str 
		i++,p++;
	}
}

2.一般式 

#include <stdio.h>
#include <math.h> 
#include <string.h>
#include <stdlib.h> 
int main()
{
	char a[50]={NULL}, b[50]={NULL};
	int m;
	gets(a);
	int len = strlen(a);
	scanf("%d", &m);
	int i, j; 
	if(len>=m)
	for(i=m-1,j=0;i<len;i++,j++){
		b[j] = a[i];
	}else{
		printf("error");
	}
	printf("%s", b);
	return 0;
}

  • 5
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值