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();
}
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);
}
@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.
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.
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.)
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
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.
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.
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.)
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.)