| | |
Ideas needed for problem with trailing whitespaces
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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:
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.
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)
// 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.
>#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:
You also have issues with short strings and empty strings or strings with nothing but spaces. Perhaps a new algorithm is in order.
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)
#include <iostream> #include <string> using namespace std; int main() { string s; int wordcount = 1; char space = ' '; cout<<"Enter a sentence: "; getline(cin,s); 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; }
New members chased away this month: 5
![]() |
Similar Threads
- Sony Vaio Fn key problem (USB Devices and other Peripherals)
- JavaScript drop-down menu mouseout hide problem (JavaScript / DHTML / AJAX)
- Timer problems! (Visual Basic 4 / 5 / 6)
- Microphone problems (USB Devices and other Peripherals)
- pc problem (Troubleshooting Dead Machines)
- Recursive help needed badly! (Java)
- Accessing a variable of another class (Java)
Other Threads in the C++ Forum
- Previous Thread: Cant execute a c++ program
- Next Thread: I need to get rid of whitespace
Views: 2202 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib library lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct temperature template templates text tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






