LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-05-2004, 12:07 PM   #1
qcoder
Member
 
Registered: Oct 2003
Location: United States
Distribution: Debian
Posts: 65

Rep: Reputation: 15
Removing spaces from string


I am writing a C++ program where the user enters string that could contain spaces. I then need to use the string as a filename (I don't want spaces in a file name). How would I remove spaces and replace with and _ character?
 
Old 07-05-2004, 12:23 PM   #2
joyxf86
LQ Newbie
 
Registered: Aug 2003
Location: India
Distribution: Red Hat 9.0
Posts: 9

Rep: Reputation: 0
simple....

int i=0;
while(string[i]!='\0')
{
if(string[i]==' ')
{
string[i]='_';
}
i++;
}
 
Old 07-05-2004, 12:27 PM   #3
qcoder
Member
 
Registered: Oct 2003
Location: United States
Distribution: Debian
Posts: 65

Original Poster
Rep: Reputation: 15
thanks
 
Old 07-05-2004, 12:35 PM   #4
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
Code:
while((p=strchr(string,' '))!=NULL) *p='_';
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
problems in removing white spaces from string of text monil Programming 7 03-08-2005 11:28 AM
Removing '\r' from a string beep Programming 5 01-27-2005 07:13 AM
Bash - Strip Spaces from string then cat cmfarley19 Programming 8 07-25-2004 12:01 PM
spaces in a string Longinus Programming 18 03-08-2004 06:02 PM
need to innitialize string variable with multiple spaces clsonnt Programming 3 08-11-2003 10:40 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration