【发布时间】:2015-05-11 00:08:10
【问题描述】:
我正在寻找一种简单而基本的方法(非常适合初学者学习最简单的方法)用 C++ 编写一个程序,该程序从用户那里获取一个字符串并输出大写字母、小写字母和整数的数量(数字)。我在使用 C++ 语法方面非常业余,所以请帮助我使用易于理解的语法。谢谢!
编辑: 这是我在 Google 中找到的一个非常简单的代码,并做了一些更改和更正:
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
char array1[50];
int i = 0, lowercase = 0, uppercase = 0, numbers = 0, total;
cout << "Enter a string: "<<endl;
cin >> array1;
cout <<endl;
while (array1[i] != 0){
if(array1[i] >= 'a' && array1[i] <= 'z'){
lowercase++;
i++;
}
else if (array1[i] >= 'A' && array1[i] <= 'Z'){
uppercase++;
i++;
}
else if (array1[i] >= '0' && array1[i] <= '9'){
numbers++;
i++;
}
else
i++;
}
total = lowercase + uppercase + numbers;
cout << "Your string has " << lowercase << " lowercase letters." << endl;
cout << "Your string has " << uppercase << " uppercase letters." <<endl;
cout << "Your string has " << numbers << " numbers." <<endl;
cout << "Your string has " << total << " total characters." <<endl;
getch();
return 0;
}
所以在这段代码中;我们假设字符串的末尾有整数 0,对吗?我们如何更改它以便我们可以在字符串中包含空格?
【问题讨论】:
-
我们不会为您编写代码。我们解决现有代码的问题。
-
发布您已经尝试编写的代码怎么样?
-
请在tutorialspoint.com/cplusplus之类的网站上尝试学习c++,如果有困难,可以在这里发帖。
-
对不起各位,我很抱歉。
-
使用小写字母数组和大写字母数组。搜索数组。如果字母存在于数组中,则增加相应的计数器。或使用
std::tolower和std::toupper。
标签: c++ string uppercase counting lowercase