主函数接收从键盘输入的一行不超过80个字符的字符串(包括空格),调用自定义函数,删除字符串中大小写字母之外的其它字符。 函数调用结束后输出删除其它字符后的字符串。
提示:参见教材【例7-3】。
函数接口定义:
int deleChar(char a[]);
a
是char
型数组形式参数,接收实参数组的首元素地址;
函数返回删除字符的个数。
函数调用结束后,a
中仅保留大写和小写字母,其余字符全部删除。
裁判测试程序样例:
#include<iostream>
using namespace std;
int deleChar(char a[]);
const int N=81;
int main()
{
char s[N];
cin.getline(s,N);
int n = deleChar(s);
cout << "delete " << n << " characters.!" << endl;
cout << "After deleted:" << s << endl;
return 0;
}
/* 请在这里填写答案 */
输入样例1:
How are you!
输出样例1:
delete 3 characters.!
After deleted:Howareyou
输入样例2:
hello!
输出样例2:
delete 1 characters.!
After deleted:hello
/* 请在这里填写答案 */
int deleChar(char a[])
{
int i=0,j=0;
for(; a[i]!='\0'; i++)
{
if(a[i]>='A'&&a[i]<='z')
{
a[j] = a[i];
j++;
}
}
a[j]='\0';
return i-j;
}