大写字母计数、小写字母计数、空白计数和小写字母转换 - C
我对语言(C)有点陌生,但在我看来,我想做的事情非常简单。我确信我只是忽略了某些事情或遇到了一些逻辑错误。
正如标题所示,我希望我的程序做的就是计算小写字母、大写字母和空格的数量。作为附加功能,它还将输入的小写字母转换为大写字母。
#include <stdio.h>
int main(void){
int iochar, numdigits=0, numlower=0, numupper=0, numwhites=0;
printf("Please enter a phrase:\n\n");
while((iochar=getchar())!=EOF) {
if ((iochar=' ')||(iochar='\t')||(iochar='\n')){
numwhites++;
putchar(iochar);
}
else if((iochar>='0')&&(iochar<='9')) {
numdigits++;
putchar(iochar);
}
else if(('a'<=iochar)&&(iochar<='z')) {
numlower++;
putchar(iochar-32);
}
else if(('A'<=iochar)&&(iochar<='Z')) {
numupper++;
putchar(iochar);
}
else
putchar(iochar);
}
printf("%d white characters, %d digits, ",numwhites,numdigits);
printf("%d lowercase have been converted to ",numlower);
printf("uppercase and %d uppercase.\n",numupper);
printf("\n\n");
return 0;
}
I'm somewhat new to the language (C), but what i am trying to do is pretty simple imo. i'm sure im just neglecting something or running into some logical errors.
As the title suggests, all i want my program to do is count the # of lowercase letters, uppercase letters, and blank spaces. As an additional function, it also converts the lowercase letters entered into uppercase.
#include <stdio.h>
int main(void){
int iochar, numdigits=0, numlower=0, numupper=0, numwhites=0;
printf("Please enter a phrase:\n\n");
while((iochar=getchar())!=EOF) {
if ((iochar=' ')||(iochar='\t')||(iochar='\n')){
numwhites++;
putchar(iochar);
}
else if((iochar>='0')&&(iochar<='9')) {
numdigits++;
putchar(iochar);
}
else if(('a'<=iochar)&&(iochar<='z')) {
numlower++;
putchar(iochar-32);
}
else if(('A'<=iochar)&&(iochar<='Z')) {
numupper++;
putchar(iochar);
}
else
putchar(iochar);
}
printf("%d white characters, %d digits, ",numwhites,numdigits);
printf("%d lowercase have been converted to ",numlower);
printf("uppercase and %d uppercase.\n",numupper);
printf("\n\n");
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在此,您分配给 iochar 并且将始终为真。应该是一个比较:
in this you assign to iochar and will always be true. It should be a comparison:
请查看
ctype 中定义的
。您也许还可以受益于在同一文件中定义的isupper(int c)
、islower(int c)
和isspace(int c)
.htoupper(int c)
和tolower(int c)
。另请注意 Sani 的答案,C 中的比较是
==
,而赋值是=
。此外,可以在if
语句中进行赋值,这可能会导致出现像您这样的常见错误。在我看来,一个好的约定是这样编写比较:这样,如果我犯了一个拼写错误并改为这样写:
我会在编译期间得到语法错误,而在运行时不会出现奇怪的行为。
Please look into
isupper(int c)
,islower(int c)
, andisspace(int c)
which are defined inctype.h
. You could perhaps also benefit fromtoupper(int c)
andtolower(int c)
which are defined in the same file.Also note Sani's answer, comparison in C is
==
while assignment is=
. Additionally, it is possible to assign withinif
statements, which make possible and even common errors such as yours. In my opinion, a good convention is to write comparisons like this:This way, if I were to make a typo and write this instead:
I would get a syntax error during compilation and not weird behavior during runtime.
我认为 Sani Huttenen 很好地解决了你的问题。我还建议您
在结束时只打一次电话......这样会更清楚。
I think Sani Huttenen has solved your question pretty well. I'd also recommend you to call
just once at the end of your bucle... It would be clearer.