c++ - 在 C++ 中难以掌握简单的 boolean 循环 if/else 程序

标签 c++ if-statement while-loop boolean

我需要一点帮助来找出 C++ 作业中的几个部分。我被要求编写如下程序:

编写一个接受键盘输入的程序(输入 按 Enter 键终止)并计算字母(A-Z 和 a-z)、数字 (0-9) 和其他字符的数量。使用 cin 输入字符串并使用以下循环结构通过“if”语句和多个“else if”语句检查字符串中的每个字符。

char s[50];
int i;

. . .

i = 0;
while (s[i] != 0) { // a string is terminated with a null (0) value

. . .

i++;
}

您的程序应该使用关系运算符(例如,== < > <= >= !=)来确定特定字符是字母、数字还是其他字符。你只能#include 和 不得使用任何其他包含文件。

该程序应具有类似于以下内容的输出:

输入一串没有空格的连续字符(例如:aBc1234!@#$%)

输入您的字符串:aBc1234!@#$%

your string has 12 total characters
3 letters
4 numerical characters
5 other characters

这是一个计算小写字母的示例程序:

// PROG07.CPP example

#include <iostream>
using namespace std;


int main()
{
char s[50];
int i;
int lowercase = 0;

//get string from the user
cout << "Enter a continuous string of characters with no blanspaces\n"
cout << "(example: aBc1234!@#$%)" << endl << endl;
cout << "Enter your string: ";
cin >> s;
cout << endl;

// loop through the string, lower case letters
// note, strings (character arrays) have an invisible
// zero value at their end
i = 0;
while (s[i] != 0) // while the character does not have ASCII code zero
{
if ((s[i] >= 'a' && s[i] <= 'z'))
lowercase++;
i++;
}

cout << "Your string has " << lowercase << " lower case letters" << endl;

// including the next line for Dev-C++:
system("pause"); // not needed for CodeBlocks

return 0;
}

到目前为止,我想出了这个:

#include <iostream>
using namespace std;

int main()
{
char s[50];
int i;
int lowercase, uppercase, numChars, otherChars = 0;

cout << "Enter a continuous string of characters" << endl;
cout << "(example: aBc1234!@#$%)" << endl;
cout << "Enter your string: ";
cin >> s;
cout << endl;

while (s[i] != 0) // while the character does not have ASCII code zero
{
if ((s[i] >= 'a' && s[i] <= 'z'))
    lowercase++;
    i++;
}
while (s[i] != 0)
{
if ((s[i] >= 'A' && s[i] <= 'Z'))
    uppercase++;
    i++;
}
cout << lowercase + uppercase << " letters" << endl;
i = 0;
while (s[i] != 0)
{
if ((s[i] >= '0' && s[i] <= '9'))
    numChars++;
    i++;
}

cout << numChars << " numerical characters" << endl;

return 0;
}

如有任何帮助,我们将不胜感激。

最佳答案

你必须在每次循环之前将 i 重置为 0:

#include <iostream>
using namespace std;

int main()
{
char s[50];
int i;
int lowercase, uppercase, numChars, otherChars = 0;

cout << "Enter a continuous string of characters" << endl;
cout << "(example: aBc1234!@#$%)" << endl;
cout << "Enter your string: ";
cin >> s;
cout << endl;

i = 0; //missing
while (s[i] != 0) // while the character does not have ASCII code zero
{
if ((s[i] >= 'a' && s[i] <= 'z'))
    lowercase++;
    i++;
}
i = 0; // missing
while (s[i] != 0)
{
if ((s[i] >= 'A' && s[i] <= 'Z'))
    uppercase++;
    i++;
}
cout << lowercase + uppercase << " letters" << endl;
i = 0;
while (s[i] != 0)
{
if ((s[i] >= '0' && s[i] <= '9'))
    numChars++;
    i++;
}

cout << numChars << " numerical characters" << endl;

return 0;
}

关于c++ - 在 C++ 中难以掌握简单的 boolean 循环 if/else 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32793967/

相关文章:

for-loop - PL/SQL 中 If 条件中的字符串比较

c# - 对象异常的无限循环stackoverflow列表

php foreach循环用几个词搜索mysql表

c++ - Floyd 循环查找算法中使用的 While 条件

c++ - 为什么禁止将 Derived** 转换为 Base*const*?

C++/是否允许更改静态数组的大小?

c++ - OpenCV绘制轮廓和裁剪

if-statement - 如何检查 Jekyll 中是否存在类别/标签?

java - 一行上有多个 if 语句

ArrayList 的 Java While 循环不会终止