# Count number of words in a string

Question 24. Count the number of words in a string

We will define a word as a collection of one or more characters from a-z and A-Z. The words in a string can be separated by multiple spaces, tabs, punctuation marks etc. To count the number of words, as we traverse the string we will keep track of the previous character and the current character. If the previous character is not an alphabet from a-z and A-Z and the current character is an alphabet from a-z and A-Z, then we have encountered a new word and we increase the word count by 1.

## C/C++

```
/*
str1: string in which the number of words have to be counted
Return value: number of words in the string
*/
int count_words(char *str1)
{
int num_words, pos;
char cur_char, is_prev_char_alphabet;

if (!str1)
return 0;

num_words = 0;
pos = 0;
cur_char = str1[pos];
is_prev_char_alphabet = 0;
while (cur_char != '\0') {
int is_cur_char_alphabet = is_alphabet(cur_char);

/*If previous character is not an alphabet and current character
is an alphabet then we have found a new word*/
if (!is_prev_char_alphabet && is_cur_char_alphabet) {
++num_words;
}

is_prev_char_alphabet = is_cur_char_alphabet;

pos++;
cur_char = str1[pos];
}

return num_words;
}

/*Helper function which checks if a character is an alphabet(a-zA-Z)*/
int is_alphabet(char c)
{
if (c >= 'A' && c <= 'Z')
return 1;

if (c >= 'a' && c <= 'z')
return 1;

return 0;
}

```

## Java

```
/*
str1: string in which the number of words have to be counted
Return value: number of words in the string
*/
public static int countWords(char[] str1) {
if (str1 == null)
return 0;

int numWords = 0;
boolean isPrevCharAlphabet = false;
for (char c : str1) {
boolean isCurCharAlphabet = isAlphabet(c);

/*If previous character is not an alphabet and current character is
an alphabet then we have found a new word*/
if (!isPrevCharAlphabet && isCurCharAlphabet)
++numWords;

isPrevCharAlphabet = isCurCharAlphabet;
}

return numWords;
}

/*Helper function which checks if a character is an alphabet(a-zA-Z)*/
public static boolean isAlphabet(char c)	{
if (c >= 'A' && c <= 'Z')
return true;

if (c >= 'a' && c <= 'z')
return true;

return false;
}

```

## Python

```
#str1: string in which the number of words have to be counted
#Return value: number of words in the string
def count_words(str1) :
if (str1 == None):
return 0

num_words = 0
is_prev_char_alphabet = False
for c in str1 :
is_cur_char_alphabet = is_alphabet(c)

#If previous character is not an alphabet and current character is
#an alphabet then we have found a new word
if (not is_prev_char_alphabet and is_cur_char_alphabet) :
num_words += 1

is_prev_char_alphabet = is_cur_char_alphabet

return num_words

#Helper function which checks if a character is an alphabet(a-zA-Z)
def is_alphabet(c):
if (c >= 'A' and c <= 'Z'):
return True

if (c >= 'a' and c <= 'z'):
return True

return False

```