【问题标题】:How do I print the number of words that begin with a certain character?如何打印以某个字符开头的单词数?
【发布时间】:2017-08-04 10:04:01
【问题描述】:

这是一个介绍性的 c++ 类,提示如下:

打印以某个字符开头的单词数。让用户输入该字符。

虽然,我不知道该怎么做。

我是否使用解析字符串?我尝试了这个,因为他们检查字符串数据类型,但我不断收到错误,所以我把它拿出来并将其更改为字符。我想学习如何做“total_num”(以用户选择的字母开头的单词总数),我还需要一些关于我的 for 循环的帮助。

所需输出示例

用户输入:a
输出:“找到 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
    }
  }
}

【问题讨论】:

  • 我可以建议 load_file 至少返回一个 std::vector<:string>() 吗?由于您没有向用户询问多个 digits,因此我会先向用户询问字符,然后浏览文件并计数。

标签: c++ computer-science


【解决方案1】:

您可以做一些事情来让您的生活更简单,例如按照评论建议使用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;

【讨论】:

    猜你喜欢
    • 2020-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-13
    相关资源
    最近更新 更多