问答题
1. 请编写函数proc(),其功能是:将str所指字符串中下标为偶数的字符删除,串中剩余字符形成的新串放在t所指数组中。
例如,当str所指字符串中的内容为“ABCDEFGHIJK”(输入完成以空格、Tab键或者Enter键加任意非空格、Tab键或者Enter键的一个字符作为输入结束标志),则在t所指数组中的内容应是“BDFHJ”。
注意:部分源程序如下。
请勿改动main()函数和其他函数中的任何内容,仅在函数proc()的花括号中填入所编写的若干语句。
试题程序:
#include<stdlib.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void proc(char*str,char t[])
{
}
void main()
{
int i=1;
char str[100],t[100];
system("CLS");
printf("\nPlease enter string Str:");
scanf("%s\n",str);
proc(str,t);
printf("\nThe result is:%s\n",t);
}
【正确答案】void proc(char *str,char t[])
{
int i, j=0, k=strlen(str); //s所指字符串中下标为偶数的字符删除即跳过去,对其什么也不做
for(i=1; i<k; i=i+2)
t[j++]=str[i]; //把下标为奇数的数,放到t数组中
t[j]='\0'; //最后用'\0'作为字符串结束标志
}
【答案解析】 按照题目的要求,将str所指字符串中下标为偶数的字符删除,串中剩余字符形成的新串放在t所指数组中。将字符串str中下标为奇数的字符放到字符串t中,对字符串str中的其余字符不予处理。最后,为新的字符串数组添加结束符。