473,386 Members | 1,741 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

question on whitespace in string

I am writing a program that takes input from a user and the program corrects the whitespace and capitalization errors. I have written the part to get the input from the user and stores it in an string with a getline function. My problem is I am tring to figure out a way to check and remove whitespace if anyone can help

thanks

Expand|Select|Wrap|Line Numbers
  1. #include <string>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. int main() {
  6.  
  7.         cout << "enter sentence and hit enter";
  8.         string  s1;
  9.         getline(cin,s1);
this is how i am getting the input in.
Nov 6 '06 #1
3 4625
vninja
40
are you looking to get rid of the whitespace before the string or throughout?
for case one you could just add a cin >> fistword; then after the getline st = firstword + st1;
for the second case i'm not too sure....
Nov 6 '06 #2
horace1
1,510 Expert 1GB
Depends on what you are trying to. You could go thru your string examining each character for correct capitalisation and removing excess whitespace

the C++ <string> class has methods which enable you to iterate thru the contents of a string, repace characters, erase characters, etc. see
http://www.cppreference.com/cppstring/index.html
Nov 6 '06 #3
I have had success deleting all whitespace from a given string using the following:

1 #include <iostream>
2
3 void DeleteWS(std::string& x)
4 {
5 for(unsigned int i = 0; i <= x.length(); i++)
6 {
7 if(x[i] == ' ')
8 {
9 x.erase(i, 1);
10 }
11 }
12 }
13 int main()
14 {
15 std::string myString;
16 std::cout << "String: ";
17 getline(std::cin, myString);
18 DeleteWS(myString);
19
20 std::cout << myString << std::endl;
21 }

If anybody notices anything inherently wrong with this code, please let me know.
Jan 25 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Ajay | last post by:
hi! i am trying to remove all whitespace elements out of a document. my code is below def stripWhiteSpaces(doc): whiteSpaces = xpath.Evaluate("//text()", doc.documentElement) counter=0...
1
by: Fred Smith | last post by:
Any suggestions or tips to the questions below I have been wrestling with would be most welcome: I have an example XSD file I have been experimenting with. Suppose a user can select from 1 to 4...
1
by: Dan W. | last post by:
I've been acting as messenger the past few days between the BOOST and Digital Mars peoples, and they can't seem to come to an agreement about the semantics of using ## vs. juxtaposition. The...
12
by: drM | last post by:
I have looked at the faq and queried the archives, but cannot seem to be able to get this to work. It's the usual factorial recursive function, but that is not the problem. It hangs after the user...
14
by: Peter | last post by:
#include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { char input_string; printf("Please enter conversion: "); scanf("%s", input_string);
13
by: Thomas Liesner | last post by:
Hi all, i am having a textfile which contains a single string with names. I want to split this string into its records an put them into a list. In "normal" cases i would do something like: >...
6
by: George | last post by:
Hi All, I'm trying to learn c++/stl. I'd like a fancy way to read lines of an ascii file into vector of stringbufs. I made a first attempt, but the compiler complains about private...
3
by: pauldepstein | last post by:
The following description of atoi is pasted from cplusplus.com. My question is after the pasting. ***** PASTING BEGINS HERE ****** int atoi ( const char * str ); <cstdlib> Convert string...
13
by: Chaim Krause | last post by:
I am unable to figure out why the first two statements work as I expect them to and the next two do not. Namely, the first two spit the sentence into its component words, while the latter two...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.