In: Computer Science
Complete the function, MycountWords in C++, which will count the number of words in a string. We will have different word separators(such as newline(\n), spaces, special symbols(? | \ , . : ;) ) That will separate the words in the giving istringstream. Remember if a words is separated by a hyphen and followed by a new line, they're still one word. The function has an option to be sensitive to case and also avoid duplicates. for duplicates, this is a boolean value that checks whether a word is repeated or not if its set to true and sensitive case is if the word count is sensitive to case and differentiates two words based on their case. even though they have different cases, they are the same word hence duplicates.
complete this function based on the description above.
unsigned int MycountWords(istringstream & iss, bool Duplicate=false, bool SenstiveCase =false) {
}
DESCRIPTION :
The program takes a string and counts the number of words in it.
SOLUTION:
1. The program takes a string.
2. Using a for loop, the number of spaces in the string are counted.
3. The total number of words will be number of spaces plus 1.
4. The result is printed.
5. Exit.
C++ CODE
Here is the source code of C++ Program to Find the Number of Words in a Given Sentence. The program output is shown below.
#include<iostream>
#include<string.h>
using namespace std;
int main ()
{
char str[50];
int count = 0, i;
cout << "Enter a string : ";
gets(str);
for (i = 0; str[i] != '\0';i++)
{
if (str[i] == ' ')
count++;
}
cout << "Number of words in the string are: " << count + 1;
return 0;
}
OUTPUT:
Case 1 :
Enter a string : Mosaic
Number of words in the string are : 1
Case 2 :
Enter a string : 1 2 3 4 5
Number of words in the string are : 5
Case 3 :
Enter string : The sun shines brightly.
Number of words in the string are: 4
Case 4 :
Enter string : One two three\n four\tfive
Number of words in the string are: 5
EXTRA CODE :
There can be many solutions to this problem. Following is a simple and interesting solution.
The idea is to maintain two states: IN and OUT. The state OUT indicates that a separator is seen. State IN indicates that a word character is seen. We increment word count when previous state is OUT and next character is a word character.
Given a string, count number of words in it. The words are separated by following characters: space (‘ ‘) or new line (‘\n’) or tab (‘\t’) or a combination of these.
/* C++ program to count no of words
from given input string. */
#include <bits/stdc++.h>
using namespace std;
#define OUT 0
#define IN 1
// returns number of words in str
unsigned countWords(char *str)
{
int state = OUT;
unsigned wc = 0; // word count
// Scan all characters one by one
while (*str)
{
// If next character is a separator, set the
// state as OUT
if (*str == ' ' || *str == '\n' || *str == '\t')
state = OUT;
// If next character is not a word separator and
// state is OUT, then set the state as IN and
// increment word count
else if (state == OUT)
{
state = IN;
++wc;
}
// Move to next character
++str;
}
return wc;
}
// Driver code
int main(void)
{
char str[] = "One two three\n four\tfive ";
cout<<"No of words : "<<countWords(str);
return 0;
}
OUTPUT :
Number of words : 5
TIME COMPLEXITY :
O(n)