Is there a function to remove spaces from a string? I need a way to have a user input a string with spaces, numbers, and letters and remove all spaces before storing it. Using scanf, cin, gets or fgets, apparently I only retain whatever is before the first space.
veronicak5678
0
Light Poster
Recommended Answers
Jump to Postscanf() and cin tokenize on whitespace, so yes, they stop reading input at any whitespace. Please don't use gets(). fgets() reads an entire line (it even returns the newline character at the end of the line), so if there are spaces in the line you'll get them too. The C++ …
Jump to PostI just couldn't believe there wasn't one already.
I'm curious. Why would you think there's a function to remove spaces from a string? Of what use would such a function be in the real world? :?:
Jump to PostYou can break the logic down into a 3 step process:
Im leaving the coding upto you.
1) Remove leading white spaces using a loop until you hit a non white space
2) Use a loop to remove the middle white spaces and copy characters into a temp array
3) …
Jump to PostOr do it in one step:
For each char in str: If it's a space: * Remember that you saw a space, but don't copy it to the output string. Else (if it's not a space): * If it's not leading space: * Copy a space to …
All 17 Replies
Duoas
1,025
Postaholic
Featured Poster
veronicak5678
0
Light Poster
WaltP
2,905
Posting Sage w/ dash of thyme
Team Colleague
powerof2
0
Newbie Poster
iamthwee
kenji
1
Junior Poster
nucleon
114
Posting Pro in Training
kenji
1
Junior Poster
cheldric
0
Newbie Poster
JameB
66
Junior Poster
Duoas
1,025
Postaholic
Featured Poster
damnpoet
0
Light Poster
pseudorandom21
166
Practically a Posting Shark
Nick Evan
4,005
Industrious Poster
Team Colleague
Featured Poster
Duoas
1,025
Postaholic
Featured Poster
WaltP
2,905
Posting Sage w/ dash of thyme
Team Colleague
bridgett.grace
0
Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.