编程测试题1:文件批量改名

输入两个非负整数m,n(位数不确定,n>=m)

给定一个字符串构成的文件名,对文件名进行批量改名。(扩展名须保持不变,且扩展名不仅限于MP3,扩充的位数以n的值为参照)

例如:

输入:

99,103

music.MP3

输出

music099.MP3

music100.MP3

music101.MP3

music102.MP3

music103.MP3

#include <stdio.h>
#include <string.h>
void deal(int x,int y,int count,char arr[100])//x是传过去的数字,y是n的位数
{
	char tmp[100] = {'\0'};
	char X[100] = {'\0'};
	strcpy(tmp,arr);
	int i = 0;
	for(i = 0;i<y;i++)
	{
		X[i] = x%10+'0';
		x = x/10;
	}
	int j = 0;
	for(i = 0;i<y;i++,count--)
	{
		tmp[count+y-1] = X[i];
	}
	printf("%s\n",tmp);
}

int main()
{
	int m = 0;
	int n = 0;
	char arr[100] = {'\0'};
	scanf("%d,%d",&m,&n);
	getchar();
	gets(arr);
	int tmp = 1;
	int a = 1;
	int b = 1;
	while(m%tmp>=10)
	{
		a++;
		tmp = tmp*10;
	}//m的位数
	tmp = 1;
	while(n/tmp>=10)
	{
		b++;
		tmp = tmp*10;
	}//n的位数
	int count = 0;
	for(count = 0;arr[count]!='.';count++);//arr[count]为'.'
	int length = strlen(arr);
	int i = 0;
	for(i = length-1;i>=count;i--)
	{
		arr[i+b] = arr[i];
	}//将'.'后的字符向后移动n的位数,为前面的数字腾出来位置
	//printf("%d\n",b);
	//printf("%s\n",arr);
	int k = m;
	for(k = m;k<=n;k++)
	{
		deal(k,b,count,arr);
	}	
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值