如何打印以特定字符开头的单词数?
How do I print the number of words that begin with a certain character?
这是针对介绍 c++ 类的,提示符如下:
打印以特定字符开头的单词数。让用户输入该字符。
虽然,我不知道该怎么做。
我是否使用解析字符串?我尝试了这个,因为他们检查字符串数据类型,但我不断收到错误,所以我把它拿出来并更改为字符。我想学习如何执行"total_num"(以用户选择的字母开头的单词总数(,并且还需要一些有关 for 循环的帮助。
所需输出的示例
用户输入:输出
:"找到 1270 个以 a 开头的单词">用户输入:E 输出:"找到 16 个以 E
开头的单词">用户输入:#
输出:"找到 0 个以 # 开头的单词">
(我想我把这部分记下来了,不按字母顺序(
数据来自一个名为dict.txt
的文件,它是一个包含许多单词的列表。
以下是它包含内容的一小部分示例:
D
d
D.A.
dab
dabble
dachshund
dad
daddy
daffodil
dagger
daily
daintily
dainty
dairy
dairy cattle
dairy farm
daisy
dally
Dalmatian
dam
damage
damages
damaging
dame
我的程序:
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
const int NUM_WORD = 21880;//amount of words in file
struct dictionary { string word; };
void load_file(dictionary blank_array[])
{
ifstream data_store;
data_store.open("dict.txt");
if (!data_store)
{
cout << "could not open file" << endl;
exit(0);
}
}
int main()
{
dictionary file_array[NUM_WORD];
char user_input;
int total_num = 0;
load_file(file_array);
cout << "Enter a character" << endl;
cin >> user_input;
if (!isalpha(user_input))
{
cout << "Found 0 that begin with " << user_input << endl;
return 0;
}
for (int counter = 0; counter< NUM_WORD; counter++)
{
if (toupper(user_input) == toupper(file_array[counter].word[0]));
//toupper is used to make a case insensitive search
{
cout << "Found " << total_num << " that begin with " << user_input << endl;
//total_num needs to be the total number of words that start with that letter
}
}
}
您可以做一些事情来简化您的生活,例如按照评论的建议使用vector
。
让我们看看你的 for 循环。存在一些明显的语法问题。
int main()
{
dictionary file_array[NUM_WORD];
char user_input;
int total_num = 0;
load_file(file_array);
cout << "Enter a character" << endl;
cin>>user_input;
if(!isalpha(user_input))
{
cout << "Found 0 that begin with " << user_input << endl;
return 0;
}
for(int counter = 0;counter< NUM_WORD; counter++)
{
if (toupper(user_input) == toupper(file_array[counter].word[0]));
// ^no semi-colon here!
//toupper is used to make a case insensitive search
{
cout<< "Found " << total_num << " that begin with "<<
user_input << endl;
//total_num needs to be the total number of words that start with that letter
}
}//<<< needed to end the for loop
}
让我们正确设置for循环。您希望对循环中的匹配项进行计数,然后在完成循环时报告。
int total_num = 0;
//get character and file
for(int counter = 0;counter< NUM_WORD; counter++)
{
if (toupper(user_input) == toupper(file_array[counter].word[0]))
^^^no semi-colon here!
{
++total_num;
}
}
cout<< "Found " << total_num << " that begin with "<< user_input << endl;
相关文章:
- C++字符*缓冲区的大小
- HEX值到wchar_t字符(UTF-8)的转换
- 为什么 Serial.println(<char[]>);返回随机字符?
- C++ 在字符的开头插入字符
- 如何计算QString开头的重复字符?
- 如何打印以特定字符开头的单词数?
- 如何将以"\0"开头的字符 * 转换为无符号整数?
- 如何输入以特殊字符开头的字符串
- 如何使用正则表达式匹配不包含特殊字符(&、\、<、>、|、)的字符串,除非它们以反斜杠开头
- C++:如果我们在字符串中添加一些整数,为什么它会从开头删除该数量的字符?(字符串 + 整数)
- 如何返回 Perl(或C++)中以某些字符开头和结尾的所有字符
- 删除字符C++最后一次出现时字符数组的开头
- 在另一个字符*C++的开头插入一个字符*
- C++ 如何查找下一个字符 std::cout 是否位于控制台行的开头
- 在字符数组的开头添加空格
- C++删除单词开头的非字母字符
- 从从 & 开头然后包含 23 个字符的文件中删除字符串
- C++fread读取文件开头的随机字符
- 文件开头的垃圾字符
- 'BOM'字符在文件开头或文件末尾的位置是什么?