Hello. I want to input a sentence (array) and then ask the user to input a letter, then I should count how many words include this letter. I should read the letter once in each word. I can't get this right, my program reads all the letters included. For example, if the sentence was: "The force of energy". My program should report 3 words containing the letter e. But mine reads 4 instead. Can anyone enlighten me with this problem?
Here's my code:
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char text[200];
int length;
char ch;
int counter;
int charcount = 0;
bool done = true;
cout<<"Enter a line of text: ";
cin.get(text, 200);
cout<<endl;
length = strlen(text);
cout<<"Enter a char: ";
cin>>ch;
for (counter = 0; counter < length; counter++)
{
if(ch == text[counter])
{
done = false;
}
if (done == false)
{
charcount++;
}
}
cout<<endl;
cout<<"There are "<<charcount<<" words containing "<<ch<<endl;
Your problem is that you don't see words. You have only characters and you count them all. (That includes whitespace characters.)
You should look one word at a time. If a word contains letter, then increase counter.
Either read words separately or identify distinct words from your text.
While-loops. Nested.
1. Advance until you reach end of text, or whitespace, or a letter to count.
2. If you did stop at letter, then increase counter and advance until you reach end of text or whitespace.
3. Repeat from 1 until you reach the end of the text.
I've tried something like this:
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char text[200];
int length;
char ch;
int counter;
int charcount = 0;
bool done = true;
char test;
cout<<"Enter a line of text: ";
cin.get(text, 200);
cout<<endl;
length = strlen(text);
cout<<"Enter a char: ";
cin>>ch;
for (counter = 0; counter < length; counter++)
{
if(' ' == text[counter])
{
done = true;
}
if (text[counter] >= ch)
{
test = text[counter];
}
if (test == text[counter])
{
charcount++;
}
}
cout<<endl;
cout<<"There are "<<charcount<<" words containing "<<ch<<endl;
return 0;
}
I know you told me to read each word, is that the way to do it? I don't know what you mean by stopping at a letter. My problem is I don't know how to read the sentence word by word. Is there an array for strings?
You are tantalizingly close between the 2 attempts. Each handles a part of the problem right.
From your 1st code:
1 2 3 4 5 6 7 8
if(ch == text[counter])
{
done = false;
}
if (done == false)// check for this at end of word.
{
charcount++;
}
Problem is no check for ' ' encountered
Your 2nd version includes
1 2 3 4
if(' ' == text[counter])
{
done = true;
}
but you don't ++charcount there. That's the right place to do so.
Reworking your code to cover this detail:
1 2 3 4 5 6 7 8 9 10
bool found = true;// your choice seems inverted to me. I would assign false initially and true when ch is found
// for each letter in text[] (ie within for loop):
if( text[i] == ch )// found ch in current word
found = false;
elseif( text[i] == ' ' )// end of current word
{
if( found == false ) ++charcount;// ch was found in last word
found = true;// ready for next word
}
Check once more after the for loop as found = false means 1 more was found in the last word (the 1 followed by'\0' not ' ').