473,890 Members | 1,971 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

strip commas from string


Hello all! I've been looking for a way to strip characters from strings
such as a comma. This would be great for using a comma
as a delimiter. I show you what I have right now.
#include<iostre am>
#include<string >

int main(int argc, char *argv[])
{
using namespace std ;

char people[14] = "Me,Myself, I" ;
char nocommas[14] ;
int i, j ;
i=j=0 ;

for(i = 0; i < 14; i++){
if(people[i] != ',')
nocommas[i] = people[i] ;

if(people[i] == ',')
nocommas[i] = ' ' ;
}

cout << "With commas : " << people << endl ;
cout << "Without commas : " << nocommas << endl ;

cout << "\n\n\n\n" ;
cout << "\tNow tell me how I can store each name in a separate
variable ?" << endl ;
cout << "\tAnd can someone show me how to do this with string instead
of char[] ?" << endl ;
cout << "\t\t Thanks so much!" << endl ;

return 0 ;
}
so as you can see it strips commas and puts a space in its place.

Thanks for you help :-)

Jul 30 '05 #1
22 13800
in************* @yahoo.com wrote:
Hello all! I've been looking for a way to strip characters from strings
such as a comma. This would be great for using a comma
as a delimiter. I show you what I have right now.
Thank you for posting code. This really helps.


#include<iostre am>
#include<string > You're not using std::string. So you _may_ not need
this header.


int main(int argc, char *argv[])
{
using namespace std ;

char people[14] = "Me,Myself, I" ;
char nocommas[14] ;
int i, j ;
i=j=0 ;

for(i = 0; i < 14; i++){
if(people[i] != ',')
nocommas[i] = people[i] ;

if(people[i] == ',')
nocommas[i] = ' ' ;
}

cout << "With commas : " << people << endl ;
cout << "Without commas : " << nocommas << endl ;

cout << "\n\n\n\n" ;
cout << "\tNow tell me how I can store each name in a separate
variable ?" << endl ;
cout << "\tAnd can someone show me how to do this with string instead
of char[] ?" << endl ;
cout << "\t\t Thanks so much!" << endl ;

return 0 ;
}
so as you can see it strips commas and puts a space in its place.

Thanks for you help :-)


1. See std::string class, especially the constructor:
std::string(cha r *)
So that you can use the find() member of the string class.

2. Also explore the strtok() and strchr() functions in the
"C" standard library.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.l earn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library

Jul 30 '05 #2
> Hello all! I've been looking for a way to strip characters from strings
such as a comma. This would be great for using a comma
as a delimiter. I show you what I have right now. cout << "\n\n\n\n" ;
cout << "\tNow tell me how I can store each name in a separate
variable ?" << endl ;
cout << "\tAnd can someone show me how to do this with string instead
of char[] ?" << endl ;
cout << "\t\t Thanks so much!" << endl ;

return 0 ;
}


to store "each name in a separate variable" you'll want a std::vector,
another container, or perhaps an array.

to split the string based upon commas, you can put the string in a
std::stringstre am and then use std::getline:

std::istringstr eam ss("I,Myself,Me ");
std::string name;
while(std::getl ine(ss, name, ',') {
/* push_back name onto the end of a std::vector or another container
*/
}

Jul 30 '05 #3
in************* @yahoo.com wrote:
Hello all! I've been looking for a way to strip characters from strings
such as a comma. This would be great for using a comma
as a delimiter. I show you what I have right now.
#include<iostre am>
#include<string >

int main(int argc, char *argv[])
{
using namespace std ;

char people[14] = "Me,Myself, I" ;
char nocommas[14] ;
int i, j ;
i=j=0 ;

for(i = 0; i < 14; i++){
if(people[i] != ',')
nocommas[i] = people[i] ;

if(people[i] == ',')
nocommas[i] = ' ' ;
}

cout << "With commas : " << people << endl ;
cout << "Without commas : " << nocommas << endl ;

cout << "\n\n\n\n" ;
cout << "\tNow tell me how I can store each name in a separate
variable ?" << endl ;
cout << "\tAnd can someone show me how to do this with string instead
of char[] ?" << endl ;
cout << "\t\t Thanks so much!" << endl ;

return 0 ;
}
so as you can see it strips commas and puts a space in its place.

Thanks for you help :-)


Here's one possibility:
#include <iostream>
#include <sstream>
#include <string>
#include <list>

int main(int argc, char *argv[])
{
// the test string of comma-seperated words
char * people = "Me,Myself, I";

// make an input stream named 'in' from 'people'
std::istringstr eam in(people);

// 'words' will hold the list of words extracted from 'in'
std::list< std::string > words;

// while no error on 'in'
while(in)
{
// 'aWord' will hold the next word read from 'in'
std::string aWord;

// get all text from 'in' up to the next comma, or end of data,
// into 'aWord'
getline(in, aWord, ',');

// if no error on 'in', i.e. we read text into 'aWord',
// then put a copy of 'aWord' in to the 'words' list
if (in)
words.push_back (aWord);
}

std::cout << "Comma seperated words read from 'people' into the
'words' list"
<< std::endl;

// 'it' is an iterator for iterating over the content of the
// 'words' list
std::list< std::string >::iterator it;

// print all of the entries in 'words'
for (it = words.begin(); it != words.end(); it++)
std::cout << (*it) << std::endl;

return 0;
}

Regards,
Larry
Jul 30 '05 #4
in************* @yahoo.com wrote:
Hello all! I've been looking for a way to strip characters from strings
such as a comma. This would be great for using a comma
as a delimiter. I show you what I have right now.


I'm pretty new to C++ myself (I started learning two or three weeks
ago), so this is probably not the cleanest (or best) way to do this,
but it works - for me anyway (it compiles with g++ -Wall -ansi -pedantic
without any warnings, so it should probably work everwhere else, too).

I stayed pretty close to the code you already had, so you shouldn't have
any problems understanding it.
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main(int argc, char **argv) {
string commas="Me,Myse lf,I";
if(argv[1]) commas=argv[1]; // Use commandline parameter as string
// if specified.
vector<string> nocommas(1);
int j=0;
for(unsigned int i=0; i<commas.length (); i++) {
if(commas[i]==',') { // If the current character is a comma,
nocommas.push_b ack(""); // add a new (empty) string to the vector
j++; // and increase the counter so that all
// future operations are performed on
the
// new string
}
else nocommas.at(j)+ =commas[i]; // If it's not add the current char
// to the string with the number j
// in the vector
}
cout<<"With commas: "<<commas<<endl <<endl;
cout<<"From the array:"<<endl;
for(int i=0; i<=j; i++) {
cout<<"Word "<<i<<": "<<nocommas.at( i)<<endl;
}
return 0;
}

--
If geiger counter does not click,
the coffee, she is just not thick
Jul 30 '05 #5
Larry, why use std:: all the time? You can just put 'using namespace
std' at the beginning of your code :-)

Jul 30 '05 #6
in************* @yahoo.com wrote:
Larry, why use std:: all the time? You can just put 'using namespace
std' at the beginning of your code :-)


That can get you in trouble.
In a small program like this it's ok, but it can cause
unexpected problems in large apps where name conflicts
might occur with methods from other (3rd party) libs.

I explicitly use 'std::' in example programs so the reader
will be clear on where things come from in the example.

Regards,
Larry

Jul 30 '05 #7
Larry I Smith wrote:
in************* @yahoo.com wrote:
Larry, why use std:: all the time? You can just put 'using namespace
std' at the beginning of your code :-)


That can get you in trouble.
In a small program like this it's ok, but it can cause
unexpected problems in large apps where name conflicts
might occur with methods from other (3rd party) libs.

I explicitly use 'std::' in example programs so the reader
will be clear on where things come from in the example.

Regards,
Larry

For completeness, this line in my example:

getline(in, aWord, ',');

should be:

std::getline(in , aWord, ',');

In the absence of a "using namespace std;" declaration,
some compilers will compile the example ok, but some
will complain about not being able to find the appropriate
getline() function.

Regards,
Larry
Jul 30 '05 #8
>That can get you in trouble.
In a small program like this it's ok, but it can cause
unexpected problems in large apps where name conflicts
might occur with methods from other (3rd party) libs.


Ok Larry, I see now. You know I learn way more in the news groups
than I do from a text book ! By the way as far as text books go,
I'm studying from the Pearson Textbook series , C++ Today and C++
Primer.
They're good books but have me starting my progs with using namespace
and
sticking with char[] instead of string alot. And to sebastian, Alipha,
and Thomas ,
I appreciate your help too ! Larry, I never knew that putting 'Using
namespace whatever'
would get me into trouble until you demonstrated it to me in this
forum. From now
on I'm going to append std:: to all my std library functions and
objects.
I had to say all that to demonstrate my appreciation ... Thanks

Jul 30 '05 #9
in************* @yahoo.com wrote:
That can get you in trouble.
In a small program like this it's ok, but it can cause
unexpected problems in large apps where name conflicts
might occur with methods from other (3rd party) libs.


Ok Larry, I see now. You know I learn way more in the news groups
than I do from a text book ! By the way as far as text books go,
I'm studying from the Pearson Textbook series , C++ Today and C++
Primer.
They're good books but have me starting my progs with using namespace
and
sticking with char[] instead of string alot. And to sebastian, Alipha,
and Thomas ,
I appreciate your help too ! Larry, I never knew that putting 'Using
namespace whatever'
would get me into trouble until you demonstrated it to me in this
forum. From now
on I'm going to append std:: to all my std library functions and
objects.
I had to say all that to demonstrate my appreciation ... Thanks


The use of 'using namespace std;' versus explicit 'std::' is a decision
that needs to be made on a project-by-project basis. If you are not
linking with multiple libs and the project is small, then the 'using'
declaration is fine. If the project is large and you are linking
with multiple libs then explicit use of 'std::' might be a better
option. Most IT departments have some kind of guidelines for their
developers spelling out the corporation's C++ standards. It just
happens that my employer expects the use of 'std::' in all but the
most trivial programs.

Like anything else, do what makes sense for the particular project.

Regards,
Larry
Jul 30 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3335
by: qwweeeit | last post by:
Hi all, I need to limit as much as possible the lenght of a source line, stripping white spaces (except indentation). For example: .. . max_move and AC_RowStack.acceptsCards ( self, from_stack, cards ) must be reduced to: .. . max_move and AC_RowStack.acceptsCards(self,from_stack,cards) My solution has been (wrogly): ''.join(source_line.split())
6
2433
by: Markus Rosenstihl | last post by:
Hi, I wonder if i can make this nicer: In my case a list is created from a bill which is a tab seperated file and each line looks like this: "Verbindungen Deutsche Telekom vom 03.08.2005 bis 10.08.2005" "ISDN" "Deutsche Telekom AG" "Rufnummer" "123456789" "3" "Verbindungen zu T-Mobile AktivPlus xxl sunday" "19483" "19" "0,1719" "3,27" "16"
6
2175
by: Mark Miller | last post by:
I have a scheduled job that uses different XSL templates to transform XML and save it to disk. I am having problems with the code below. The problem shows up on both my development machine (Windows XP Pro SP 1, .Net Framework 1.1) and on our production server (Windows 2K SP 4, .Net Framework 1.1). I have simplified the code and data to isolate the problem. When I use the xsl:strip-space (Line 12) declaration in conjunction with the xsl:sort...
6
7481
by: dixie | last post by:
I have a text field on a form which has names with a comma between them like this: 'J. Smith, A. Jones, A. Man, J. Johns'. I am trying to find a procedure that will count the number of people in this text box and put the number into another text box (4 in the case above). If I could count the number of commas + 1, this would do it. Can anyone please help me with a way to achieve this? dixie
5
8727
by: dan.j.weber | last post by:
I'm using Python 2.3.5 and when I type the following in the interactive prompt I see that strip() is not working as advertised: >>>s = 'p p:p' >>>s.strip(' :') 'p p:p' Is this just me or does it not work? I want to get rid of all ' ' and ':' in the string. I've checked the doc and from what I can tell this is what strip() is supposed to do. Thanks for any help.
0
2088
by: Brian Henry | last post by:
I thought this was useful, its a extender i just wrote for the new .NET menu strip and status strip to extend the menu items to add a status message so when a mouse rolls over the item it displays the text on the status strip lable specified for the extender.. all you have to do is copy this to a code file, and add it as a control to your form. set the status lable property of the extender control then set the status mesages of the menu...
6
2609
by: rtilley | last post by:
s = ' qazwsx ' # How are these different? print s.strip() print str.strip(s) Do string objects all have the attribute strip()? If so, why is str.strip() needed? Really, I'm just curious... there's a lot don't fully understand :)
6
2364
by: eight02645999 | last post by:
hi can someone explain strip() for these : 'example' when i did this: 'abcd,words.words'
3
2377
by: Colin J. Williams | last post by:
The Library Reference has strip( ) Return a copy of the string with the leading and trailing characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a
0
9975
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9812
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11212
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9614
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8004
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6032
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4255
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3263
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.