Word Count....Need Help Please

My program is able to count the words in the sentence, however when you space twice or even three four times, it adds another word, how can I fix this? please help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
	int i, numword;
	char aChar;

	numword= 1;

	for (i=0; i<int(number1.length()); i++)
	{
		aChar = number1.at(i);
		if (isspace(number1[i]))
			numword++;
	}
	cout << "Your sentence has " << numword << " words." <<endl;
	cin.ignore();
	}
you add a word every time it is a space, so in your if statement you should also check that the last char wasn't a space
how do you do that??
Here it is :))
1
2
3
4
5
6
7
8
9
10
  int length = Sentence.length();       
   
    int words = 1;                          
  
    
    for (int size = 0; size < length; size++)
    {
                 
		if (Sentence[size] == ' ' && Sentence[size-1] != ' ' ) 
words++; 

wow it worked...thanks a lot!!
#include <stdio.h>

#define OUT 1
#define IN 0

int main()
{
int c, nw, state;
state = OUT;
nw = 0;
while((c = getchar()) !=EOF)
{
if (c == ' ' || c == '\n' || c == '\t')
{
state = OUT;
}
else if (state == OUT)
{
state = IN;
nw++;
}
}
printf("The number of words are: %d\n", nw);
}

This should work
Except that it isn't C++.

@Janlan
That results in an out-of-bound access, so the loop should start at index 1. size is also a misleading name for an index variable, it should be i.
Seriously, what is it with C and its ability to stomp its way onto this forum so much?
be open-minded

C++ is a multi-paradigm language which happens to include C

you can use the paradigm you prefer, or mix paradigms as you choose, at your own risk
C is not a paradigm, it is a language. That said, C++ includes some C features for compatibility reasons, but it is suggested to not use them unless you have to.
What? Why not?

C++ was specifically designed to be as compatible with C as possible. C uses an imperative paradigm, which is what I expect kfmfe04 was saying. C++ includes that paradigm because it includes C (with minimal, unavoidable differences).

There is nothing wrong with using C/imperative programming in/with your C++ code. (Many production environments do just that.)
Duoas is right on the mark

in C++,

C is the imperative part of the language
you have OOP in classes and virtual methods
templates allow for generics which lets you to do something like functional programming

say you know how to the #2 and #3, but not #1 - that would imply that you are not very good with any pointer manipulation, including function pointers - in that case, you are missing out on a lot of power

if you ever work with OPC (Other People's Code), you will have to work with all paradigms
To master C++ you need to know C
That said, C++ includes some C features for compatibility reasons, but it is suggested to not use them unless you have to.


Is int really included for compatibility reasons? :)

As an aside, I code in obj-C at work as well as various others, and it is much, much easier to use the C primitives (int, double and so on) than to use the obj-C number classes. If C++ offered number classes over int, double and so on, they would have to be astonishingly good to warrant using them over the C primitives.
Last edited on
AhmedKamal wrote:
To master C++ you need to know C

I disagree. C++ is not C.
I'm hearing a lot of hooflah, C++ is based on C's style and earlier versions required minimal conversions to compile as C++. But times have changed and modern C and C++ don't get on as well.

I went through C++, and C++ only, and learnt in massive detail about variables, pointers, and all that jazz; the basics, so that I could easily manipulate them and had a solid understanding ready for the hard stuff, a tactic you can apply to most things.

I've not learnt any C (specifically), and even working with other's code still have no trouble.

I wonder where you thought C was a good idea for a C++ forum because they have clear differences that are mentioned all the time.

kfmfe04 wrote:
be open-minded

Even with my opinions, I was asking a question that wasn't rhetorical, so that's not exactly closed-minded.
Veltas wrote:
I wonder where you thought C was a good idea for a C++ forum because they have clear differences that are mentioned all the time.

You're wrong.

C++ and C do have their differences, but C is not separable in a C++ environment. When you talk about C++, you can expect a lot of questions about C as well. This is because...
Duoas wrote:
C++ was specifically designed to be as compatible with C as possible.
Do you need references for that statement?

Duoas wrote:
There is nothing wrong with using C/imperative programming in/with your C++ code. (Many production environments do just that.)
A very useful website when working with mixed C and C++ code is found here:
http://david.tribble.com/text/cdiffs.htm


By the way, who is "you"? (Clarify your antecedents.)

Duoas wrote:
You're wrong.


Wrong?

I haven't said anything other than that C++ and C have differences, which you even mentioned in your last post.

I just don't see how trying to answer a question from someone beginning C++ with C is a good idea, but maybe I'm missing something?
You said, "I wonder where you thought C was a good idea for a C++ forum". That's an exact quote, and it is a clear expression of disdain for stuff about C appearing on this C++ forum.

On review of this thread again, I see you were responding to AhmedKamal's post, which I had ignored (out of habit). I don't think it appropriate to attack others for trying their hand at a helpful response.

It might be more constructive to say something like, "For future reference, as the OP is asking a question about C++ (as evidenced by his use of cout and the like), an answer in C++ is more appropriate than one in C."

Sorry I was grouchy. (I have had a couple of bad days at work.)
Topic archived. No new replies allowed.