Hello everyone, im glad i have come across this forum, im sure its going to be a lot of help for me!
I have been reading through the forum here to try and find some help with a problem i am currently experiencing.
My problem is that i am trying to write a piece of code which asks for a string and then relates back to the user how many words are within that string.

My code so far:

// Prog to calculate the number of words in a sentence

#include <iostream.h>
#include <cstring.h>

void main()
   string s, space;
   int wordcount = 1;
   space = " ";

   cout<<"Enter a sentence: ";

   for (int i=0;i<s.length();i++)   //Loop to test chars in string
   	if ((s[i] == space) && (s[i+1] != space))
      	wordcount++; //If no double space occurs then increment wordcount

   cout<<"Number of words equals: "<<wordcount<<endl;

This is working fine even if double spacing is occuring at the front or middle of any words. However, if i try to input a space at the end of the string, an error box pops up saying "Program Aborted" obviously due to this end character being a spcae but no character after it to check if its a space.

I am using Borland v 5.02 because that is the version that is currently at my college.
I have checked through the forum and have come up with EatTrailingWhitespace which sounds as though it would help me here. Ive looked for it in the Help index and cannot find it. Is this because my program is a lot older and doesnt have this facility?

I have also thought of trying to look at the end char and if it is a space then to delete it from the string. Would this be the easiest way of getting around it? I have also looked at the Trim commands but cannot seem to get them to work.
Any help in pointing me in the right direction would be mostly appreciated. :D

>#include <iostream.h>
If you can use the string class then you can stop using these old headers.

>#include <cstring.h>
Either you're using an old, nonstandard header, or this should give you a compiler error.

>void main()
No matter how old your compiler is, main always returns int. This has always been the case.

>for (int i=0;i<s.length();i++)
Because you check s[i+1], this should loop to s.length()-1 so that you do not overrun the string's memory boundaries.

>if ((s == space) && (s[i+1] != space))
space is a string, yet s is a char. It would be better to make space a char. Something like this:

#include <iostream>
#include <string>

using namespace std;

int main()
  string s;
  int wordcount = 1;
  char space = ' ';

  cout<<"Enter a sentence: ";

  for (int i=0;i<s.length()-1;i++)   //Loop to test chars in string
    if ((s[i] == space) && (s[i+1] != space))
      wordcount++; //If no double space occurs then increment wordcount

  cout<<"Number of words equals: "<<wordcount<<endl;

You also have issues with short strings and empty strings or strings with nothing but spaces. Perhaps a new algorithm is in order.

Thanks Narue! Cant change the libraries for some reason but by changing to char and slipping that -1 in there it works perfectly, many thanks!
Now that it think about it, it makes a lot of sense too.
Thanks again!

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