我编写了一个代码,可以打印出用户输入字符串中有多少个 a。
我希望程序打印出“总共有 1 个 a”而不是“总共有 1 个 a”。
我尝试编写一个 if 语句,但它会在每个循环中打印出字符串的长度。我该如何调整?我已经尝试了一些东西,但我没有达到那个程度。
#include <stdio.h>
#include <cs50.h>
#include <string.h>
int main(void){
int count = 0;
string input = get_string("Write something and I'll tell you how many a's there are: ");
for (int i = 0, n = strlen(input); i < n ; i++){
if (input[i] == 'a' || input[i] == 'A'){
count++;
}
if (count == 1){
printf("\nThere is a total of %i a", count);
}
}
printf("\nThere are a total of %i a's", count);
}
答案 0 :(得分:1)
#include <stdio.h>
#include <cs50.h>
#include <string.h>
int main(void){
int count = 0;
string input = get_string("Write something and I'll tell you how many a's there are: ");
for (int i = 0, n = strlen(input); i < n ; i++)
if ((input[i] | 0x20) == 'a')
count++;
(count == 1) ? printf("\nThere is a total of 1 a") : printf("\nThere are a total of %i a's", count);
}
使用 ternary operator。
注意:('a' 或 0x20) 以及 ('A' 或 0x20) 等于 'a'
答案 1 :(得分:1)
您可以使用 if 语句来编写。
#include <stdio.h>
#include <cs50.h>
#include <string.h>
int main(void){
int count = 0;
string input = get_string("Write something and I'll tell you how many a's there are: ");
for ( size_t i = 0, n = strlen(input); i < n ; i++){
if (input[i] == 'a' || input[i] == 'A'){
count++;
}
}
if (count == 1){
printf("\nThere is a total of %i a", count);
}
else {
printf("\nThere are a total of %i a's", count);
}
}
或者你可以使用条件运算符
#include <stdio.h>
#include <cs50.h>
#include <string.h>
int main(void){
int count = 0;
string input = get_string("Write something and I'll tell you how many a's there are: ");
for ( size_t i = 0, n = strlen(input); i < n ; i++){
if (input[i] == 'a' || input[i] == 'A'){
count++;
}
}
printf("\nThere %s a total of %i a%s",
count == 1 ? "is" : "are",
count,
count == 1 ? "" : "'s" );
}
第三种方法可以使用指定格式字符串的字符串文字。例如
#include <stdio.h>
#include <cs50.h>
#include <string.h>
int main(void){
int count = 0;
string input = get_string("Write something and I'll tell you how many a's there are: ");
for ( size_t i = 0, n = strlen(input); i < n ; i++){
if (input[i] == 'a' || input[i] == 'A'){
count++;
}
}
string format;
if (count == 1){
format = "\nThere is a total of %i a";
}
else {
format = "\nThere are a total of %i a's";
}
printf( format, count );
}
答案 2 :(得分:1)
只需将这一行从 for 循环中取出
if (count == 1){
printf("\nThere is a total of %i a", count);
}
然后在循环外构造一个这样的条件语句
if (count == 1){
printf("\nThere is a total of %i a", count);
}else if(count > 1){
printf("\nThere are a total of %i a's", count);
}else{
printf("\nThere are no a's");
}
这就是你的最终代码应该是什么样子
int main(void){
int ctr = 0;
string input = get_string("Write something and I'll tell you how many a's there are: ");
for (int i = 0; i < strlen(input) ; i++){
if (input[i] == 'a' || input[i] == 'A'){
count++;
}
//Where you removed the if (count == 1)
}
//Where it should be
if (count == 1){
printf("\nThere is a total of %i a", count);
}else if(count > 1){
printf("\nThere are a total of %i a's", count);
}else{
printf("\nThere are no a's");
}
}
答案 3 :(得分:1)
如果你想在满足条件后退出循环,你可以使用 break
语句,它会让你退出内部循环。
答案 4 :(得分:-1)
int main(void)
{
int count = 0;
string input = get_string("Write something and I'll tell you how many a's there are: ");
for (size_t i = 0, n = strlen(input); i < n ; i++)
count += (input[i] == 'a' || input[i] == 'A');
printf("\nThere is a total of %i a%s", count, count == 1 ? "" :"'s");
}