c语言字符串运算

来源:百度知道 编辑:UC知道 时间:2024/05/15 12:54:21
已知年月 char month[7],char n[3];
求month后n个月的年月为多少,用C语言怎么编
比如 month=200907 n=12 ,如何得出新的年月201007
能不能提供给我具体的解决办法

将整个串截成月串和年串,再用atoi函数将串转为整型进行你要求的操作,再用sprintf将整型转为字符串,将两个串拼接就行了

定义成字符型应该简单点吧……

#include <stdio.h>
#include <stdlib.h>
void main()
{
int m;
int n;
int l;
int s;
int z;
if (scanf("%d",&m)==0)
{
printf("输入年月错误!");
return;
}
if(scanf("%d",&n)==0)
{
printf("输入错误!");
return;
}
l=m/100;
s=m%100;
if (s>12)
{
printf("输入的年月有错误!");
return;
}
s=s+n;
while(s>12)
{
s=s-12;
l++;
}
z=l*100+s;
printf("%d后的%d个月是%d",m,n,z);
}

不用用char类型的
用int比较简单

#include<stdio.h>
#include <conio.h>

int main()
{
int year,month;
int n;
printf("请输入year和month:");
scanf("%4d%d",&year,&month);