c++面试编程题.docx
- 文档编号:4129744
- 上传时间:2022-11-28
- 格式:DOCX
- 页数:14
- 大小:605.09KB
c++面试编程题.docx
《c++面试编程题.docx》由会员分享,可在线阅读,更多相关《c++面试编程题.docx(14页珍藏版)》请在冰豆网上搜索。
c++面试编程题
c++面试编程题
常用经典编程例子
一个链表的结点结构
structNode
{
intdata;
Node*next;
};
typedefstructNodeNode;
(1)已知链表的头结点head,写一个函数把这个链表逆序(Intel)
Node*ReverseList(Node*head)文件的内容如下:
#include<>
#include<>
char*append(char*s1,char*s2)
{
}
voidmain()
{
char*s,*s1,*s2;
s1="thisis";
s2="astring.";
s=append(s1,s2);
cout<
}
【答案】
char*append(char*s1,char*s2)
{
char*tmp;
intlength;
length=strlen(s1)+strlen(s2);
tmp=newchar[length+1];
strcpy(tmp,s1);
strcat(tmp,s2);
returntmp;
}
字符串中的数字
.简单应用题
请编写一个函数intCalcDigital(char*str),该函数可返回字符串str中数字字符(即'0'-'9'这10个数字)的个数,如字符串"olympic2008"中数字字符的个数为4。
请用if条件判断语句与for循环语句来实现该函数。
注意:
部分源程序已存在文件中。
请勿修改主函数main和其他函数中的任何内容,仅在函数find的花括号中填写若干语句。
文件的内容如下:
#include<>
#include<>
intCalcDigital(char*str);
voidmain()
{
char*str;
str=newchar[255];
cout<<"输入字符串:
";
cin>>str;
intnum=CalcDigital(str);
cout< "< } intCalcDigital(char*str) { } 【答案】intCalcDigital(char*str) { if(str==NULL)return0; intnum_of_digital=0; intlen=strlen(str); for(inti=0;i if(str[i]<='9'&&str[i]>='0') num_of_digital++; returnnum_of_digital; } 字符串中最大的字符 .简单应用题 请编写一个函数charMaxCharacter(char*str),该函数返回参数str所指向的字符串中具有最大ASCII码的那个字符(如字符串"world"中字符'w'具有最大的ASCII码)。 当str所指向的字符串为空时,则返回空字符0x0或'\0'。 输出结果如下: GoodMorning! Maxchar: r 注意: 部分源程序已存在文件中。 请勿修改主函数main和其他函数中的任何内容,仅在函数MaxCharacter的花括号中填写若干语句。 文件的内容如下: #include<> #include<> charMaxCharacter(char*str); voidmain() { charstr[100]; strcpy(str,"GoodMorning! "); charmaxc=MaxCharacter(str); cout< cout<<"Maxchar: "< } charMaxCharacter(char*str) { } 【答案】 charMaxCharacter(char*str) { if(str==NULL) return0x0; charmaxChar=0x0; intlen=strlen(str); for(inti=0;i { if(str[i]>maxChar) maxChar=str[i]; } returnmaxChar; } 字符串删除一 .简单应用题 编写函数fun(),该函数的功能是从字符串中删除指定的字符,同一字母的大、小写按不同字符处理。 例如: 程序执行时输入字符串为turbocandborlandc++,从键盘上输入字符n,则输出后变为turbocadborladc++。 如果输入的字符在字符串中不存在,则字符串照原样输出。 注意: 部分源程序已存在文件中。 请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。 文件的内容如下: #include<> #include<> #include<> voidfun(chars[],intc) { } voidmain() { staticcharstr[]="turbocandborlandc++"; charch; cout<<"原始字符串: \n"< cout<<"输入一个字符: "; cin>>ch; fun(str,ch); cout<<"str="< } 【答案】 voidfun(chars[],intc) { inti=0; char*p; p=s; while(*p) {if(*p! =c) {s[i]=*p; i++; } p++; } s[i]='\0'; } 字符串删除二 .简单应用题 请编写函数fun(),其功能是将s所指字符串中除了下标为奇数、同时ASCII值也为奇数的字符之外,其余的所有字符都删除。 字符串中剩余的字符所形成的一个新的字符串放在t所指的数组中。 例如: s所指字符串中的内容为ABCDEFG12345,其中字符A的ASCII码值虽为奇数,但元素所在的下标为偶数,因此必需删除;字符1的ASCII码值为奇数,所在数组中的下标也为奇数,不删除,最后t所指的数组中的内容应是135。 请勿修改主函数main和其他函数中的任何内容,仅在函数su的花括号中填写若干语句。 文件的内容如下: #include<> #include<> #include<> #include<> voidfun(char*s,chart[]) { } voidmain() { chars[100],t[100]; cout<<"PleaseenterstringS: "< gets(s); fun(s,t); puts(t); } 【答案】 voidfun(char*s,chart[]) { inti,j=0,n; n=strlen(s); for(i=0;i if(i%2! =0&&s[i]%2! =0) {t[j]=s[i];j++;} t[j]='\0'; } 字符串查找 .简单应用题 请编写一个函数intpattern_index(charsubstr[],charstr[]),该函数执行含通配符""的字符串的查找时,该通配符可以与任一个字符匹配成功。 当子串substr在str中匹配查找成功时,返回子串substr在str中的位置,否则返回值为0。 要求使用for循环实现。 输出结果如下: 子串起始位置: 5 注意: 部分源程序已存在文件中。 请勿修改主函数main和其他函数中的任何内容,仅在函数pattern_index的花括号中填写若干语句。 文件的内容如下: #include<> intpattern_index(charsubstr[],charstr[]) { } voidmain() { char*substring,*string; intsame; substring="gram"; string="thisprogramreturnindexofsubstring"; same=pattern_index(substring,string); if(same) cout<<"子串起始位置: "< else cout<<"匹配不成功"< } 【答案】 intpattern_index(charsubstr[],charstr[]) { inti,j,k; for(i=0;str[i];i++) for(j=i,k=0;(str[j]==substr[k])||(substr[k]==' ');j++,k++) if(! substr[k+1]) return(i); return(0); } 字符串排序 .简单应用题 请编写函数fun(),对长度为7个字符的字符串,除首、尾字符外,将其余5个字符按ASCII值码降序排列。 例如: 原来的字符串为CEAedca,则排序后输出为CedcEAa。 注意: 部分源程序已存在文件中。 请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。 文件的内容如下: #include<> #include<> #include<> #include<> voidintfun(char*s,intnum) { } voidmain() { chars[10]; printf("输入7个字符的字符串: "); gets(s); fun(s,7); cout< } 【答案】 intfun(char*s,intnum) {chart; inti,j; for(i=1;i for(j=i+1;j if(s[i] {t=s[i]; s[i]=s[j]; s[j]=t; } } 回文数 .简单应用题 请编写函数fun(),该函数的功能是判断字符串是否为回文,若是则函数返回1,主函数中输出YES;否则返回0,主函数中输出NO。 回文是指顺读和倒读都一样的字符串。 例如: 字符串LEVEL是回文,而字符串123312就不是回文。 注意: 部分源程序已存在文件中。 请勿修改主函数main和其他函数中的任何内容,仅在函数fun的花括号中填写若干语句。 文件的内容如下: #include<> #include<> #defineN80 intfun(char*str) { } voidmain() {chars[N]; cout<<"Enterastring: "< gets(s); cout<<"\n\n"; puts(s); if(fun(s)) cout<<"YES\n"; else cout<<"NO\n"; } 【答案】 intfun(char*str) {inti,n=0,fg=1; char*p=str; while(*p) {n++;p++;} for(i=0;i if(str[i]==str[n-1-i]); else {fg=0;break;} returnfg; } 数组查找一 .简单应用题 请编写一个函数intSeqSearch(intlist[],intstart,intn,intkey),该函数从start开始,在大小为n的数组list中查找key值,返回最先找到的key值的位置,如果没有找到则返回-1。 请使用for循环实现。 注意: 部分源程序已存在文件中。 请勿修改主函数main和其他函数中的任何内容,仅在函数SeqSearch的花括号中填写若干语句。 文件的内容如下: #include<> intSeqSearch(intlist[],intstart,intn,intkey) { } voidmain() { intA[10]; intkey,count=0,pos; cout<<"Enteralistof10integers: "; for(pos=0;pos<10;pos++) { cin>>A[pos]; } cout<<"Enterakey: "; cin>>key; pos=0; while((pos=SeqSearch(A,pos,10,key))! =-1) { count++; pos++; } cout< =1"times": "time")<<"inthelist."< } 【答案】 intSeqSearch(intlist[],intstart,intn,intkey) { for(inti=start;i { if(list[i]==key) { returni; } } return-1; } 数组查找二 .简单应用题 请编写一个函数index(intx,inta[],intn),该函数实现先显示给定长度的一数组中所有元素,然后在其中查找一个数是否存在的功能。 注意: 使用for循环结构实现该函数的基本功能,根据main函数的调用情况给出正确的返回值。 部分源程序已存在文件中。 请勿修改主函数main和其他函数中的任何内容,仅在函数index的花括号中填写若干语句。 源程序文件清单如下: #include<> boolindex(intx,inta[],intn) { } voidmain() { inta[]={1,2,3,4,5,6,7,8}; intnum; num=5; cout<<"num=5\n"; if(index(num,a,8))cout<<"true"< elsecout<<"false"< } 【答案】 boolindex(intx,inta[],intn) { for(inti=0;i
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- c+ 面试 编程