Home > Enterprise >  How can I fix my 'for' loop so that it combines multiple outputs of the same value into on
How can I fix my 'for' loop so that it combines multiple outputs of the same value into on

Time:04-09

I'm trying to separate a string input into individual words, and then determine the length of each word without using find, and while only using the listed libraries below. I've managed to create a 'for' loop that does this, however, it outputs each word individually. I would like to combine words with the same value length into one single output, but haven't been able to figure it out yet.

#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>

using namespace std;

int main()
{
    string a;
    string s;
    
    cout<<"Enter a phrase: "<<endl;
    getline(cin, a);
    int count=0;
    

    for(int i = 0; i <= a.size(); i  ){
        if(!isspace(a[i]) && a[i]){
            s  = a[i];
            count;
        } 
        else if(!a[i] || isspace(static_cast<unsigned char>(a[i]))){
            count  ;
            cout << s.length() << " characters long: " << count<<" words."<< endl;
            s = "";
        }
        else{
        count=0;
        }
    }

    return 0;
}

example:

input: "I wish I could combine this"

output: 1 characters long: 1 words. 4 characters long: 2 words. 1 characters long: 3 words. 5 characters long: 4 words. 7 characters long: 5 words. 4 characters long: 6 words.

CodePudding user response:

A quick and dirty way to do it would be to have an array which is the length of the input string 1 (since no word could be larger than the entire string). Each index would correspond to a word length, and you could increment those accordingly. Then at the end just print the nonzero lengths. Something like this:

#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>

using namespace std;

int main()
{
    string a;
    string s;
    
    cout << "Enter a phrase: "<< endl;
    getline(cin, a);

    int sizes[a.length() 1] = {}; // array for the lengths of words

    for(int i=0; i<a.length() 1; i  ) {

        if(!isspace(a[i]) && a[i]){
            s  = a[i];
        } else if (!a[i] || isspace(static_cast<unsigned char>(a[i]))) {
            int index = s.length();
            sizes[s.length()]  ; // incrementing the array at the given size
            s = "";
        }

    }

    for(int i=0; i<(sizeof(sizes)/sizeof(*sizes)); i  ) {
        if(sizes[i]!=0)
            cout << "There are: " << sizes[i] << " words: " << i << " characters long\n";
    }
    
    return 0;
}

CodePudding user response:

I would use a std::map to map the length of each word to the number of words of that length.

Like this:

#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>
#include <map>

int main()
{
    std::string a = "I wish I could combine this";
    std::string s;
    std::map<size_t, int> m;

    for (size_t i = 0; i < a.size(); i  ) 
    {
        if (!std::isspace(a[i]) && a[i])
        {
            s  = a[i];
        }
        else if (!a[i] || std::isspace(static_cast<unsigned char>(a[i])))
        {
              m[s.size()];
            s.clear();
        }
    }
    if (!s.empty())
    {
          m[s.size()];
    }

    for (std::map<size_t, int>::iterator it = m.begin(); it != m.end();   it)
    {
        std::cout << it->first << " characters long: " << it->second << " words.\n";
    }

    return 0;
}

Online Example

Output:

1 characters long: 2 words.
4 characters long: 2 words.
5 characters long: 1 words.
7 characters long: 1 words.
  • Related