我是 C 编程新手。我的作业问题之一让我很难受。这是:
编写一个 ANSI-C 程序,使用 getchar() 从标准输入读取字符,并使用 putchar() 仅将输入中的字母、空格 (' ') 和换行符输出到标准输出。如果字母是小写字母,您的程序应该将它们转换为大写字母。例如,给定以下输入:
有6个苹果和8个橙子,还有9个香蕉......@Apple Store!在那里见!?
你的程序的输出应该是:
这里有苹果和橙子还有香蕉苹果商店见
我可以正确区分大小写部分,但很难忽略数字和任何其他字符。任何帮助将不胜感激。
#include <stdio.h>
#include <stdlib.h>
int main()
{
int c;
while ((c=getchar())!=EOF) {
if ((c>='a' && c<='z'))
c -= 32;
else
while((c==getchar())<'a' || (c==getchar())>'z' ||(c==getchar())!='\n' ||(c==getchar())!=' '); //This is where I am having trouble.
putchar(c);
}
}
最佳答案
使用isalpha
, isspace
和toupper
在<ctype.h>
像这样
while ((c=getchar())!=EOF) {
if(isalpha(c) || isspace(c))// isspace allow '\t' => c == ' ' || c == '\n'
putchar(toupper(c));
}
关于c - 如何使用 getchar 和 putchar 忽略数字输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32802824/