Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8872205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:10:12+00:00 2026-06-14T18:10:12+00:00

I have an array of words, and I have a text file. What I

  • 0

I have an array of words, and I have a text file. What I want to do is use the array of words and search through the text file, count the number of times each word in the array appears in the text file.

I have thought about using a For Loop but that just gave me the total of the word count not the individual word count for each. I can’t put the text file into an array as there is about 40000 words in the text file.

After the count, I want to divide each count by a integer value known as ‘scale’. And then mulitply a string by the new count number.

So I am currently doing it as shown below. Is there anyway I can make this more efficient?

Any help is greatly appreciated.

Array of words = testwords.

Name of file = testF.

inWord = each word in the file.

while(testF >> inWord)
    {if (inWord == testwords[0]){
            count1++;
            }
        if (inWord == testwords[1]){
            count2++;
            }
        if (inWord == testwords[2]){
            count3++;
            }
        if (inWord == testwords[3]){
            count4++;
            }
        if (inWord == testwords[4]){
            count5++;
            }
        if (inWord == testwords[5]){
            count6++;
            }
        if (inWord == testwords[6]){
            count7++;
            }
        if (inWord == testwords[7]){
            count8++;
            }
}
cout << testwords[0] << " " << count1 << " " << s1.append(count1/scale, '*') << endl;
cout << testwords[1] << " " << count2 << " " << s2.append(count2/scale, '*') << endl;
cout << testwords[2] << " " << count3 << " " << s3.append(count3/scale, '*') << endl;
cout << testwords[3] << " " << count4 << " " << s4.append(count4/scale, '*') << endl;
cout << testwords[4] << " " << count5 << " " << s5.append(count5/scale, '*') << endl;
cout << testwords[5] << " " << count6 << " " << s6.append(count6/scale, '*') << endl;
cout << testwords[6] << " " << count7 << " " << s7.append(count7/scale, '*') << endl;
cout << testwords[7] << " " << count8 << " " << s8.append(count8/scale, '*') << endl;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T18:10:13+00:00Added an answer on June 14, 2026 at 6:10 pm

    Before you worry about efficiency, you should worry about approach. You’re not using logical data structures. Instead of having 8 separate counts, keep an array of counts. Or better yet, keep a map of word -> count.

    Lucky in this situation, cleaner code will correspond to much faster execution.

    In particular, use an std::map<std::string, size_t>.

    Alternatively, if you’re using C++11, you could use a std::unordered_map for likely better performance.

    Assuming you’re reading your words from cin:

    std::map<std::string, size_t> counts;
    
    std::string word;
    
    while (std::cin >> word) {
        ++counts[word];
    }
    
    for (std::map<std::string, size_t::const_iterator it = counts.begin(),
         end = counts.end(); it != end; ++it) {
        std::cout << "The word '" << it->first << " appeared " 
                  << it->second << " times" << std::endl;
    }
    

    Documentation for std::map.

    Documentation for std::unordered_map.

    For what it’s worth, std::unordered_map is (pretty assumably always) implemented as a hash map, and std::map is implemented (pretty assumably always) using a balanced binary tree as the backing structure.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text file with spam words. I want to have an array
I have a text file of 1000 words that I want to format an
I am trying to count the number of times a particular word occurred in
I have to text file in the following format : Word [tab][tab] Word [Carriage
I have a text file that looks like this: STUFF UP HERE APEXED NUMBER
Suppose I have a text file with data like below, I want to read
I have text file. there around 1000 lines in it, each line contains a
My objective is to read an XML text file and split each word and
I have this code to read in a text file, with words separated by
I have a text file and I want to remove some lines that contain

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.