Sorry about the title of the thread, I was looking at the wrong problem. For our class, we're supposed to get a sentence from a user and output the number of three letter words entered. Here's what I have so far:

#include <iostream>
#include <string>
using namespace std;

void main()
string sent;
int j(0);

cout << "Enter your sentence: ";
getline(cin, sent);

for(int i = 0; i <= sent.length(); i++)
		if(sent[i] == ' ' && sent[i+3] == ' ')

cout << "You have entered " << j << " three letter words." << endl;

I'm not sure what I'm doing wrong.

First, don't use void as return type for main(), even if your compiler allows you to do it. The return type of main() should always be int.

Second, if you're at index i and there are three letters in the word, what would be the next index after the third letter in the word relative to the beginning space?

Once you've answered that you have the general case that you tried to deal with in your code. However, there are two special cases you will need to take of, too. They are:

1) what happens if the the input doesn't start out with a space but the first word is three letters long and

2) what happens if i is within two letters of the end of the input and you want to look at least three letters beyond it?

stringstreams are often useful when you want to parse a sting input, specially when you need to do something with words.
So make a stringstream and use the >> operator to fetch out one word at a time. Check if that has a .length() equal to 3. If yes, increase the counter.

This article has been dead for over six months. Start a new discussion instead.