Friday, 8 November 2013


This C Program will count the number of words in a given sentence.  The program uses pointers' approach. 

Logic
To calculate the number of words in a string(sentence), count the number of spaces in the string and display it. But, in this approach, a few cases have to dealt with. Read the Test Cases section for more detail.

Test Case 1 - Ignore the leading and trailing whitespaces in the string.
Example: Let the string be " Hello, how are you ". This sentence contains 4 words but 5 whitespaces.

Test Case 2 - Ignore multiple whitespaces
Example:  Let the string be "Hello    how   are you". This string contains 4 words but 8 whitespaces. 

So, the leading and white spaces can be removed (trim) from the original string. Or, simply check for leading and trailing white spaces and ignore them if found. Simple, isn't it. This program ignores the leading and trailing whitespaces with the following piece of code.

Code Snippet: Ignore Leading Whitespace
Here p is an char type pointer. and is initially pointing to the starting position of the string (character array in C). So, if a whitespace is detected, the count variable is decremented.

// ignoring leading whitespace
if(*p == ' ')
{
count--;
}

Code Snippet: Ignore Multiple Whitespace
If the string contains multiple white spaces, then the count is much higher than the number of words in the string. So, before incrementing count, if we check for the previous element's value in array, we can solve this problem. Now, check if the previous element is a whitespace or not. In case it is, do not increment. Simple, but remember you have to store the value of previous element in a variable in order to access it. See Code.
In the code, a variable named previous of type integer is used to store the precious value. Note: The  value 32 is the ASCII code for space.

while(*p != '\0')
{
if(*p == ' ' && previous!= 32)
count++;
previous = *p;
p++;
}
Code Snippet: Ignore Trailing Whitespace
After we have traversed to the last element of the array (to end of string), now check if the last element in array is a white space or not. In case it is, then just decrement the value of count variable to make up for the extra increment.

// ignoring trailing white space 
p--;
if(*p == ' ')
count--;
Full Code
File: CountWords.c

#include <stdio.h>
#define MAX 200
int countWords(char* sentence)
{
char* p;
int count = 1;
int previous = 0;
p = sentence;
// ignoring leading whitespace
if(*p == ' ')
{
count--;
}
while(*p != '\0')
{
if(*p == ' ' && previous!= 32)
count++;
previous = *p;
p++;
}
// ignoring trailing white space 
p--;
if(*p == ' ')
count--;
return count;
}
int main()
{
char sentence[MAX] = {'\0'};
int count;
printf("Enter a sentence : ");
gets(sentence);
count = countWords(&sentence);
printf("\nNumber of words : %d", count);
return 0;
}


Get Ebooks delivered to your email id

Comments

Subscribe to our channel

Facebook

Powered by Blogger.

Home | Contact Us | DMCA | Terms of Service | Privacy | Advertise

Maven Scientists