943,973 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2441
  • C++ RSS
Oct 27th, 2004
0

Ideas needed for problem with trailing whitespaces

Expand Post »
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:
C++ Syntax (Toggle Plain Text)
  1. // Prog to calculate the number of words in a sentence
  2.  
  3. #include <iostream.h>
  4. #include <cstring.h>
  5.  
  6. void main()
  7. {
  8. string s, space;
  9. int wordcount = 1;
  10. space = " ";
  11.  
  12. cout<<"Enter a sentence: ";
  13. getline(cin,s);
  14.  
  15. for (int i=0;i<s.length();i++) //Loop to test chars in string
  16. if ((s[i] == space) && (s[i+1] != space))
  17. wordcount++; //If no double space occurs then increment wordcount
  18.  
  19. cout<<"Number of words equals: "<<wordcount<<endl;
  20. }

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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
blargage is offline Offline
2 posts
since Oct 2004
Oct 28th, 2004
0

Re: Ideas needed for problem with trailing whitespaces

>#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[i] == space) && (s[i+1] != space))
space is a string, yet s[i] is a char. It would be better to make space a char. Something like this:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. string s;
  9. int wordcount = 1;
  10. char space = ' ';
  11.  
  12. cout<<"Enter a sentence: ";
  13. getline(cin,s);
  14.  
  15. for (int i=0;i<s.length()-1;i++) //Loop to test chars in string
  16. if ((s[i] == space) && (s[i+1] != space))
  17. wordcount++; //If no double space occurs then increment wordcount
  18.  
  19. cout<<"Number of words equals: "<<wordcount<<endl;
  20. }
You also have issues with short strings and empty strings or strings with nothing but spaces. Perhaps a new algorithm is in order.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 28th, 2004
0

Re: Ideas needed for problem with trailing whitespaces

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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
blargage is offline Offline
2 posts
since Oct 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Cant execute a c++ program
Next Thread in C++ Forum Timeline: I need to get rid of whitespace





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC