-1

My code for finding the word with the fewest letter is this:

cin.get(a, 100);
p = strtok(a," ");
min = p;
int ok;
while (p) {

    if (strlen(min) > strlen(p)) {
        strcpy(min, p);
    }
    p = strtok(NULL," ");
}


 cout << "The word with the fewest lettes is " << min << endl;

My question is how can I find how many times it appears? Sorry if it's a silly question, I'm a beginner in c++.

Roland
  • 885
  • 3
  • 12
  • 16
  • How many times appears word with the same letter number as another e.g. "few", "and", or how many times appears the same word with fewest letter? – BartekPL Mar 24 '17 at 07:59
  • How many times appears a word with the same letter number – Roland Mar 24 '17 at 08:00
  • You're going to need an `else` in there for your goal. At least one, and another `if` or two. – WhozCraig Mar 24 '17 at 08:00
  • just put a counter in, which you increase for `strlen(min)==strlen(p)`, and reset at the point where you do the `strcpy` – Karsten Koop Mar 24 '17 at 08:03
  • ... then buy some temporaries and reduce the `strlen` spam you're going to find yourself writing. That sentence may seem odd at first, but will make more sense once you do what Karsten just described. – WhozCraig Mar 24 '17 at 08:04
  • 2
    It is not about the question but I suggest using std::string – Petar Petrovic Mar 24 '17 at 08:32

2 Answers2

1

Just add a simple counter variable which is 0 in the beginning, changes to 1 when there is a word with fewer characters and increment if there is a word with the same number of characters as the word with minimum characters by now.

I think something like this will work.

enter code herecin.get(a, 100);
p = strtok(a," ");
min = p;
int ok;

int counter = 0;
while (p) {

    if (strlen(min) > strlen(p)) {
        strcpy(min, p);
        counter = 1;
     }
    else if(strlen(min) == strlen(p)){
        counter++;
    }
    p = strtok(NULL," ");
}
Petar Velev
  • 2,305
  • 12
  • 24
0

Simple and fast solution using std::string. You can read about std::string here. It's powerful class that ships with standard library, if you master it, your life will become easier. Advantages of std::size_t are discussed here

#include <iostream>
#include <string>

int main ()
{
    std::string input;
    std::cin >> input;

    std::size_t curPos = 0;
    std::size_t length = input.length();
    std::size_t sw_offset = 0; // sw means 'shortest word'
    std::size_t sw_length = 0;
    std::size_t sw_count = 0;
    while(curPos <= length)
    {
        std::size_t newPos = input.find_first_of(' ', curPos); 
        if(newPos == std::string::npos) // If there is no whitespace it means it's only 1 word
            newPos = length;

        if(newPos != curPos) // If word isn't empty (currentWordLength > 0)
        {
            std::size_t currentWordLength = newPos - curPos;
            if(!sw_length || sw_length > currentWordLength)
            {
                sw_offset = curPos; // Store offset and length instead of copying
                sw_length = currentWordLength;
                sw_count = 1;
            }
            else if(sw_length == currentWordLength &&
                input.substr(sw_offset, sw_length) == input.substr(curPos, currentWordLength))
            {
                ++sw_count;
            }
        }
        curPos = newPos + 1;
    }

    std::cout << "Fewest letter word is " << input.substr(sw_offset, sw_length)
                 << " and it's appeared " << sw_count << " time(s)" << std::endl;
}

Same for c style strings

#include <iostream>
#include <cstring>
#include <algorithm>

int main ()
{
    char input[256];
    std::cin.get(input, sizeof(input));

    std::size_t curPos = 0;
    std::size_t length = strlen(input);
    std::size_t sw_offset = 0;
    std::size_t sw_length = 0;
    std::size_t sw_count = 0;
    while(curPos <= length)
    {
        std::size_t newPos = std::find(input + curPos, &input[sizeof(input) - 1], ' ') - input;

        std::size_t currentWordLength = newPos - curPos;
        if(currentWordLength > 0)
        {
            if(!sw_length || sw_length > currentWordLength)
            {
                sw_offset = curPos;
                sw_length = currentWordLength;
                sw_count = 1;
            }
            else if(sw_length == currentWordLength &&
                      strncmp(input + sw_offset, input + curPos, currentWordLength) == 0)
            {
                ++sw_count;
            }
        }
        curPos = newPos + 1;
    }

    char result[256];
    strncpy(result, input + sw_offset, sw_length);
    std::cout << "Fewest letter word is " << result
                 << " and it's appeared " << sw_count << " time(s)" << std::endl;
}
Inline
  • 2,566
  • 1
  • 16
  • 32