题目
字符串中提取年月日,year、month、day,格式:year=2012,month=march-April、day=6-8
string date("Jan.30 2012-Feb.2 2012");
string date("Jan.30-Feb.2 2012");
string date("30 March-3 April 2003");
string date("6-8 May 2012");
string date("12 April 2012");
string date("Nov/Dec.12 2012");
string date("April 12 2012");
string date("April 2012");
string date("2012");
string date("Jan.30 2012-Feb.2 2012");
string date("Jan.30-Feb.2 2012");
string date("30 March-3 April 2003");
string date("6-8 May 2012");
string date("12 April 2012");
string date("Nov/Dec.12 2012");
string date("April 12 2012");
string date("April 2012");
string date("2012");
提问时间:2021-03-29
答案
比较繁.
先找有无 - 或 /, 有则分为2 个部分,无, 则是 1个部分.
然后 把两部分 的 字符串 抓出来.
字符串判断,字母开始 是 月,其它,4个数字是年,余下是 日.
用C++找 - 或 / :
size_t pos=0; pos = str.find("-");
if (pos==0) pos = str.find("/");
用C:
string str("Jan. 30 2012-Feb. 2 2012");
int main ()
{
int i;
int found= -1;
char s1[3][10],s2[3][10];
int n1=0,n2=0;
char cstr[40],c1[40],c2[40];
strcpy (cstr, str.c_str());
for (i=0;i < str.length(); i++) if ( cstr[i]=='-' || cstr[i]=='/') {
found = i; cstr[i]=' ';
}
if (found > 0) {
strcpy(c1,cstr); c1[found]='\0';
n1 = sscanf(c1,"%s %s %s",s1[0],s1[1],s1[2]);
strcpy(c2, &cstr[found+1]);
n2 = sscanf(c2,"%s %s %s",s2[0],s2[1],s2[2]);
} else{
n1 = sscanf(cstr,"%s %s %s",s1[0],s1[1],s1[2]);
}
前1 部分 的 字符串 在 s1[0],s1[1],s1[2] 中,年月日个数为 n1
后1 部分 的 字符串 在 s2[0],s2[1],s2[2] 中, 年月日个数为 n2
然后循环判断一下即可
取出的字符串是否正确,可以临时打印出来看:
for (i=0;i
先找有无 - 或 /, 有则分为2 个部分,无, 则是 1个部分.
然后 把两部分 的 字符串 抓出来.
字符串判断,字母开始 是 月,其它,4个数字是年,余下是 日.
用C++找 - 或 / :
size_t pos=0; pos = str.find("-");
if (pos==0) pos = str.find("/");
用C:
string str("Jan. 30 2012-Feb. 2 2012");
int main ()
{
int i;
int found= -1;
char s1[3][10],s2[3][10];
int n1=0,n2=0;
char cstr[40],c1[40],c2[40];
strcpy (cstr, str.c_str());
for (i=0;i < str.length(); i++) if ( cstr[i]=='-' || cstr[i]=='/') {
found = i; cstr[i]=' ';
}
if (found > 0) {
strcpy(c1,cstr); c1[found]='\0';
n1 = sscanf(c1,"%s %s %s",s1[0],s1[1],s1[2]);
strcpy(c2, &cstr[found+1]);
n2 = sscanf(c2,"%s %s %s",s2[0],s2[1],s2[2]);
} else{
n1 = sscanf(cstr,"%s %s %s",s1[0],s1[1],s1[2]);
}
前1 部分 的 字符串 在 s1[0],s1[1],s1[2] 中,年月日个数为 n1
后1 部分 的 字符串 在 s2[0],s2[1],s2[2] 中, 年月日个数为 n2
然后循环判断一下即可
取出的字符串是否正确,可以临时打印出来看:
for (i=0;i
举一反三
已知函数f(x)=x,g(x)=alnx,a∈R.若曲线y=f(x)与曲线y=g(x)相交,且在交点处有相同的切线,求a的值和该切线方程.
我想写一篇关于奥巴马的演讲的文章,写哪一篇好呢?为什么好
奥巴马演讲不用看稿子.为什么中国领导演讲要看?
想找英语初三上学期的首字母填空练习……
英语翻译
最新试题
热门考点
- 1初二英语:将下列两个句子合并成一句
- 2第一个字第三个字是数字第二个第四个是反义词的词语有哪些?(10个)
- 3已知 (tanα+1)/(tanα-1) = - (2+√3),求锐角α的值
- 4朝花夕拾中个文章的主要内容
- 5将一恰能在水中悬浮的小物体投入装满水的烧杯,则烧杯底部受到水的压强怎样变化?用与物块相同的材料制成一个十立方分米的实心物体,则物体质量为多少千克?
- 6将人的胰岛素基因转移到大肠杆菌细胞内,结果在大肠杆菌的后代中合成出人的胰岛素.在这个过程中
- 7英语翻译
- 8____ wash the clothes? Yes,he does.They are dirty.
- 9我英语的词汇量少,一篇阅读就有好多生词,坐起阅读理解和完型填空总是这篇不会那篇也不会,增么办啊
- 10[the lady who is standing behind the counter.]怎么理解这个句子?