user2940061
user2940061

Reputation: 3

Counting number of words in char array C++

I am working on an algorithm that will count the number of words in a char array. So far it seems to not work the way it should. When a character is reach and it is not whitespace, it should be considered to be part of a word. Once you reach white space, we are not in a word anymore. For example, "Hello World" is two words because of the space between "hello" and "world".

Code:

for(int l = 0; l < count; l++){
        if(isalpha(letters[l]) && !in_word){
            num_words++;
            in_word = true;     
        }else{
            in_word = false;
        }
    }

sample input: aaaaa bbb aaa lla bub www

sample output: 13 words

desired output: 6 words

Possible answer:

for(int l = 0; l < count; l++){
        if(isalpha(letters[l]) && !in_word){
            num_words++;
            in_word = true;     
        }else if(!isalpha(letters[l])){
            in_word = false;
        }
    }

Upvotes: 0

Views: 3211

Answers (3)

Michael Anderson
Michael Anderson

Reputation: 73470

If you want to get nice handling of newlines, spaces punctuation etc you could use a regex. You may even be able to adapt this to work correctly with utf-8 strings too. However it requires C++11 support.

#include <iostream>
#include <string>
#include <regex>

int main ()
{
  std::string s ("this subject has a submarine as a subsequence");
  std::smatch m;
  std::regex e ("\\b(\w*)\\b")

  int count = 0;
  while (std::regex_search (s,m,e)) {
    ++count;
    s = m.suffix().str();
  }

  std::cout<<"Number of matches = "<<count<<std::endl;

  return 0;
}

Upvotes: 0

basil
basil

Reputation: 720

easy way to do this: trim the string, count the spaces, add 1

Upvotes: 1

Matt
Matt

Reputation: 5567

Step through that code (in a debugger, in your head/on paper).

Given the input "abc def"

Assuming in_word = false initially

  • The first character is 'a', in_word is false, so num_words++, in_word=true
  • The next character is 'b', in_word is true, so in_word=false

Hopefully you will see what is wrong

Upvotes: 2

Related Questions