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: ";
getline(cin,s);
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