简答题 C++
题号 #2254596

使用VC++6.0打开考生文件夹下的源程序文件2.cpp。完成函数fun(char*sl,char*s2)的空出部分。函数fun(char*s1,char*s2)的功能是将在字符串s1中下标为偶数的字符,紧随其后重复出现一次,放在一个新串s2中,s2中字符按原字符串中字符的顺序排列。(注意0为偶数)
例如:当s1中的字符串为“abcdef”时,s2中的字符串应为“aaccee”。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include(iostream.h)
voidfun(char*s1,char*s2)
{
}
voidmain()
{
charsi[100】,s2【1OO];
cout<<"Pleaseenterstringstrin9:"<<endl;
cin.getline(s1,100);
fun(s1,s2);
cout<<"Theresultis:"<<s2<<cndl
return;
}

进入在线模考

参考答案

暂无

答案解析

inti;
for(i=0;s1[i]!=0;i++)//循环判断每一个字符
{
if(i%2==0)//判断字符串中下标为偶数位置
{
s2[i]=s1[3];
s2[i+1]=s1[i];
s2[i+2]='\0';//新串添加尾符
}
}
【解析】判断一个字符串中的某一个字符下标i是否为偶数,可用“i%2==0”来判断。对串s1中各个字符串逐个进行考查,满足条件的字符就顺序放在新串s2中,否则就不存放。所有字符处理完成后,记得在新串s2的末尾加上结束符“'\0'。

你可能感兴趣的试题

1 使用VC++6.0打开考生文件夹下的源程序文件3.cpp,其中定义了用于表示日期的类Date,但类Date的定义并不完整,按要求完成下列操作,将类的定义补充完整。
(1)定义私有成员变量year、month、day。分别表示年、月、日,类型为int。请在注释1后添加适当的语句。
(2)完成构造函数,分别给year、month、day赋值,请在注释2后添加适当的语句。
(3)完成重载符号“十=”的定义,请在注释3后添加适当的语句。
(4)完成print打印函数,输出到屏幕和文件的格式相同,请在注释4后添加适当的语句。
注意:仅在函数指定位置添加语句,请勿改动主函数main与其他函数中的任何内容。
程序正常运行,输出的结果为2008年11月813。
试题程序:
#include(iostream.h)
#include(fstream)
#include(iomanip)
#include<cmath)
usingnamespacestd;
voidWriteFile(intc)
{
ofstreamout1;
out1.open("3.txt",ios_base::app);
out1<<C<<'';
out1.close();
}
voidWriteFile(char*str)
{
ofstreamoutl;
out1.open("3.txt",ios_base::app);
out1<<str;
out1.close();
}
voidClearFile()
{
ofstreamout1;
out1.open("3.txt");
out1.close();
}
classDate
{
public:
Date(inty,intm,intd)
{
//********1********
}
voidprint();
//********2********

{
month+=m;
inti=month/12:
intj=month%12;
if(j==0)
{year+=(i-1);
month=12;
}
else
{
year+=i:
month=j;
}
return*this;
}
private:
//********3********
};
voidDate::print()
{
//********4********
WriteFile(year):
WriteFile("年");
WriteFile(month);
WriteFile("月");
WriteFile(day);
WriteFile("日");
}
intmain()
{
ClearFile();
DateOly_day(2008,8,8);
Olyday+=3:
Oly_day.print();
return0;
}
找试题 · 找答案 · 找资料 在线模考