简答题 C++
题号 #2254747

使用VC++6.0打开考生文件夹下的源程序文件2.epp。完成空出函数fun(char*S,char*t)部分。函数fun(char*s,char*t)的功能是将在字符串S中下标为偶数位置上的字符,紧随其后重复出现一次,放在一个新串t中,t中字符按原字符串中字符的顺序排列。
例如:当s中的字符串为“ABCDEF”时,t中的字符串应为“AACCEE”。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include<iostream.h>
voidfun(char*s,char*t)
{
}
voidmain()
{
chars[100],t[100];
cout<<"Pleaseenterstrings:"<<endl;
tin.getline(s,100);
fun(s,t);
cout<<"Theresultis:"<<t<<endl;
return;
}

进入在线模考

参考答案

暂无

答案解析

voidfun(char*s,char*t)
{
inti;
for(i=0;s[i]!==0;i++)
{
if(i%2==0)
{
t[i]=s[i];
t[i+2]='\0';
}
}
}
【解析】本题要求将在字符串s中下标为偶数位置上的字符,紧随其后重复出现一次,放在一个新串t中,t中字符按原字符串中字符的顺序排列,也就是说重复出现一次偶数位的字符,数组的下标是从0开始的,所以会重复第1、3、5…的字符。

你可能感兴趣的试题

1 使用VC++6.0打开考生文件夹下的源程序文件3.cpp,其中定义了用于表示日期的类Date,但类Date的定义并不完整,按要求完成下列操作,将类的定义补充完整。
(1)定义私有成员变量year、month、day,分别表示年、月、日,类型为int。请在注释1后添加适当的语句。
(2)完成构造函数,分别给year、month、day赋值,请在注释2后添加适当的语句。
(3)完成重载符号“+=”的定义,请在注释3后添加适当的语句。
(4)完成函数print打印函数,如2005年1月5日到屏幕和文件out3.txt格式相同,请在注释4后添加适当的语句。
注意:增加代码,或者修改代码的位置已经用符号表示出来。请不要修改其他的程序代码。
试题程序:
#include<iostream.h>
#include<fstream>
#include<iomanip>
#include<cmath>
usingnamespacestd;
voidWriteFile(intc)
{
ofstreamout1;
out1.open("out3.txt",ios_base::app);out1<<C<<'';
out1.close();
}
voidWriteFile(char*str)
{
ofstreamout1;
out1.open("out3.txt",ios_base::app);out1<<str;
out1.close();
}
voidClearFile()
{
ofstreamout1;
out1.open("out3.txt"):
outl.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();
DateNationa1_day(2004,10,5);
National_day+=3;
National_day.print();
return0;
}
找试题 · 找答案 · 找资料 在线模考