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 741955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:41:32+00:00 2026-05-14T08:41:32+00:00

Judging from the title, I kinda did my program in a fairly complicated way.

  • 0

Judging from the title, I kinda did my program in a fairly complicated way. BUT! I might as well ask anyway xD

This is a simple program I did in response to question 3-3 of Accelerated C++, which is an awesome book in my opinion.

I created a vector:

vector<string> countEm;

That accepts all valid strings. Therefore, I have a vector that contains elements of strings.

Next, I created a function

int toLowerWords( vector<string> &vec )
{
    for( int loop = 0; loop < vec.size(); loop++ )
        transform( vec[loop].begin(), vec[loop].end(),
            vec[loop].begin(), ::tolower );

that splits the input into all lowercase characters for easier counting. So far, so good.

I created a third and final function to actually count the words, and that’s where I’m stuck.

int counter( vector<string> &vec )
{

for( int loop = 0; loop < vec.size(); loop++ )
    for( int secLoop = 0; secLoop < vec[loop].size(); secLoop++ )
    {
        if( vec[loop][secLoop] == ' ' )

That just looks ridiculous. Using a two-dimensional array to call on the characters of the vector until I find a space. Ridiculous. I don’t believe that this is an elegant or even viable solution. If it was a viable solution, I would then backtrack from the space and copy all characters I’ve found in a separate vector and count those.

My question then is. How can I dissect a vector of strings into separate words so that I can actually count them? I thought about using strchr, but it didn’t give me any epiphanies.


Solution via Neil:

stringstream ss( input );
while( ss >> buffer )
    countEm.push_back( buffer );

From that I could easily count the (recurring) words.

Then I did a solution via Wilhelm that I will post once I re-write it since I accidentally deleted that solution! Stupid of me, but I will post that once I have it written again ^^

I want to thank all of you for your input! The solutions have worked and I became a little better programmer. If I could vote up your stuff, then I would 😛 Once I can, I will! And thanks again!

  • 1 1 Answer
  • 1 View
  • 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-05-14T08:41:32+00:00Added an answer on May 14, 2026 at 8:41 am

    If the words are always space separated, the easiest way to split them is to use a stringstream:

    string words = ....   // populat
    istringstream is( words );
    
    string word;
    while( is >> word ) {
       cout << "word is " << word << endl;
    }
    

    You’d want to write a function to do this, of course, and apply it to your strings. Or it may be better not to store the strings at allm but to split into words on initial input.

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

Sidebar

Related Questions

Is there any way to detect SNI SSL support in rails? Judging from this
Judging from this Wikipedia article on cipher modes and other things I've heard about
The default Cassandra systems keyspace system is present in all Cassandra installations. Judging from
I am building programming contest software. A user's program is received by our judging
Judging from the result of my last inquiry , I need to calculate and
I'm trying to attach multiple Behaviors to an UIElement (which, just judging from the
This seems to be a source of much confusion judging by the amount of
Judging from my knowledge of the history of firmware engineering tools, practices etc. It
This is hopefully a quick/easy one. I know a way to work around this
This question is a spin-off from this one . Some history: when I first

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.