如何在单词中打印元音
How to print vowels in a word?
不使用数组、向量或函数!从每个人的教科书c_for解决这个问题。
问题 - 编写一个程序来读取单词并打印单词中的元音数。在本练习中,假设 a e i o u y 是元音。例如,如果用户提供输入"Harry",程序将打印 2 个元音
尝试-
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
cout <<"Please enter a word" ;
char alpha;
cin>> alpha;
int count = 0;
for ( int i=0; i <= alpha.length(); i++)
{
if (alpha == 65 || alpha == 69 || alpha == 73 || alpha == 79 || alpha == 85 || alpha == 89)
count++;
}
cout << count << " vowels." ;
return 0;
显示此错误且无法编译 - p.4.13.cpp:15:27:错误:成员引用基类型"char"不是结构或联合。感谢您的帮助!
尝试使用 std::string(或字符数组,char[])代替 char,"char" 是基元类型,它不是结构体,也没有可以通过 "." 运算符访问的成员。
有更好的方法可以做到这一点,但只是尝试发布一些接近你原来的东西,我可以做到。
#include <iostream>
#include <string>
using namespace std;
const string vowels{ "aeiouy" };
int main()
{
cout << "Please enter a word: ";
std::string alpha;
getline( cin, alpha, 'n' );
int count = 0;
for( const auto& letter : alpha ) {
if( string::npos != vowels.find( letter ) ) ++count;
}
cout << count << " vowels.";
cout << 'n' << 'n';
system( "PAUSE" );
return 0;
}
相关文章:
- 在gem5中打印文件中的所有cache_blocks
- 当您在此单词中搜索单词时调整字符数组的大小?
- 计算单词中的音节数,但考虑到单词中彼此相邻的任何元音都算作一个音节
- 如何打印源文件中与 GDB 中的程序流关联的所有行号
- 如何在 c++ 中打印存档中的对象向量
- 如何使用Qt删除单词中的表行
- 打印类中的变量
- 使用类进行实现时,递归打印C++中的链表
- C++ - 使用结构数据类型将单词中的单个小写字符更改为大写,反之亦然
- 打印C++中字符出现之间/之后的所有子字符串
- 在打印方法中,难以在二进制树中打印数据
- 用最少的元音打印单词
- 延迟逐个字符打印单词
- 如何在C++中转换可打印对象中的对象
- 如何在几个空行后打印文件中的输出
- 计算单词中的字母
- 如何在 STL 中使用reverse_iterator打印单词
- 如何在单词中打印元音
- 计算并打印句子中每个单词的长度
- 为什么当我运行此循环以打印字符串中的每个单词时,我缺少一个值