473,320 Members | 2,000 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,320 software developers and data experts.

Delete Characters from char array

What is a good way to delete 1 character (or several) from a
character array that's being processed as a "string"? Specifically, I
have something like:
char szWork[128];
....
strcpy(szWork, "-1234.5"); // populate the data
At this point, I'd like to remove the '-' that's in the 1st character
position (and this example isn't realistic), and I can't see a good way
to do this: strcpy with a char pointer that points to the 2nd character;
memmove; I don't know...
Furthermore, it might be desireable to remove more than 1 character -
and from within the string's contents. I'd want to extend the
particular problem above to do that, as well.
This seems such a common operation (editors, data conversions, etc.),
that I find it odd I can't find examples (on google, etc.) of how to do
this.
I suppose I could use std::string or some advanced "string"
processing (and I don't know what to use there, either), but that seems
pretty extreme for this.
Any thoughts? TIA...
Nov 8 '05 #1
8 32716
Michael R. Copeland wrote:
What is a good way to delete 1 character (or several) from a
character array that's being processed as a "string"? Specifically, I
have something like:
char szWork[128];
...
strcpy(szWork, "-1234.5"); // populate the data
At this point, I'd like to remove the '-' that's in the 1st character
position (and this example isn't realistic), and I can't see a good way
to do this: strcpy with a char pointer that points to the 2nd character;
memmove; I don't know...
Furthermore, it might be desireable to remove more than 1 character -
and from within the string's contents. I'd want to extend the
particular problem above to do that, as well.
This seems such a common operation (editors, data conversions, etc.),
that I find it odd I can't find examples (on google, etc.) of how to do
this.
I suppose I could use std::string or some advanced "string"
processing (and I don't know what to use there, either), but that seems
pretty extreme for this.
Any thoughts? TIA...


I don't think a std::string is too "extreme" for this. I think it's a
perfect demonstration of std::string as a better "C string".

I would use std::string myself.

--John Ratliff
Nov 8 '05 #2
> > What is a good way to delete 1 character (or several) from a
character array that's being processed as a "string"? Specifically, I
have something like:
char szWork[128];
...
strcpy(szWork, "-1234.5"); // populate the data
At this point, I'd like to remove the '-' that's in the 1st character
position (and this example isn't realistic), and I can't see a good way
to do this: strcpy with a char pointer that points to the 2nd character;
memmove; I don't know...
Furthermore, it might be desireable to remove more than 1 character -
and from within the string's contents. I'd want to extend the
particular problem above to do that, as well.
I suppose I could use std::string or some advanced "string"
processing (and I don't know what to use there, either), but that seems
pretty extreme for this.
Any thoughts? TIA...


I don't think a std::string is too "extreme" for this. I think it's a
perfect demonstration of std::string as a better "C string".

I would use std::string myself.


Fair enough. How do I do it to solve the problem(s)? What methods
do I use, and how do I use them?
Nov 8 '05 #3
Ian
Michael R. Copeland wrote:
What is a good way to delete 1 character (or several) from a
character array that's being processed as a "string"? Specifically, I
have something like:
char szWork[128];
...
strcpy(szWork, "-1234.5"); // populate the data
At this point, I'd like to remove the '-' that's in the 1st character
position (and this example isn't realistic), and I can't see a good way
to do this: strcpy with a char pointer that points to the 2nd character;
memmove; I don't know...
Furthermore, it might be desireable to remove more than 1 character -
and from within the string's contents. I'd want to extend the
particular problem above to do that, as well.
This seems such a common operation (editors, data conversions, etc.),
that I find it odd I can't find examples (on google, etc.) of how to do
this.
I suppose I could use std::string or some advanced "string"
processing (and I don't know what to use there, either), but that seems
pretty extreme for this.
Any thoughts? TIA...


Yes, use a std::string, it has all that you require for this.

Ian
Nov 8 '05 #4
In article <MP************************@news.west.cox.net>,
Michael R. Copeland <mr*****@cox.net> wrote:
[somebody else wrote:]
> What is a good way to delete 1 character (or several) from a
> character array that's being processed as a "string"? Specifically, I
> have something like:
> char szWork[128];
> ...
> strcpy(szWork, "-1234.5"); // populate the data
> At this point, I'd like to remove the '-' that's in the 1st character


I would use std::string myself.


Fair enough. How do I do it to solve the problem(s)? What methods
do I use, and how do I use them?


#include <iostream>
#include <string>
using namespace std;

int main ()
{
string szWork = "-1234.5";
szWork.erase(0,1); // starting at position 0, delete 1 character
cout << szWork << endl;
return 0;
}

--
Jon Bell <jt****@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Nov 8 '05 #5
> This seems such a common operation (editors, data conversions, etc.),
that I find it odd I can't find examples (on google, etc.) of how to do
this.


It's just too simple ;-). std::string can do it for you, as well as
many other things. Consult the standard library documentation.

The easy way (easy for small things, hard for more complicated), e.g.
to remove the minus sign (without substituting it by something else):

char szWork[128];
strcpy (szWork, "-1234.5");
//...
szWork++;

This just increases the position of the first character. To substitute
the first character by a '+':

*szWork = '+'; // szWork == "+1234.5"

To substitute the 2nd character by a '2':

*(szWork+1) = '2'; // szWork == "+2234.5"

And so on. But remember - std::string gives more elegant solutions and
more possibilities, especially when things get more complicated.
Eckhard

Nov 8 '05 #6
ec****@web.de wrote:
This seems such a common operation (editors, data conversions, etc.),
that I find it odd I can't find examples (on google, etc.) of how to do
this.


It's just too simple ;-). std::string can do it for you, as well as
many other things. Consult the standard library documentation.

The easy way (easy for small things, hard for more complicated), e.g.
to remove the minus sign (without substituting it by something else):

char szWork[128];
strcpy (szWork, "-1234.5");
//...
szWork++;


Er. szWork is not a modifiable lvalue.

Krishanu
Nov 8 '05 #7
>> char szWork[128];
strcpy (szWork, "-1234.5");
//...
szWork++;


Er. szWork is not a modifiable lvalue.


Ough, sorry - you're right.

It needs to be an dynamically allocated char * if it should be
manipulated:

char *szWork = new char[128];
//...
delete [] szWork;
Eckhard

Nov 8 '05 #8
ec****@web.de wrote:
char szWork[128];
strcpy (szWork, "-1234.5");
//...
szWork++;


Er. szWork is not a modifiable lvalue.

Ough, sorry - you're right.

It needs to be an dynamically allocated char * if it should be
manipulated:

char *szWork = new char[128];
//...
delete [] szWork;
Eckhard


You could also make a char* to szWork and work off that.

char szWork[128];
char* ptr = szWork;
strcpy(szWork, "-1234.5");
// blah blah
++ptr; // *ptr == "1234.5"

--John Ratliff
Nov 8 '05 #9

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

Similar topics

11
by: Ger | last post by:
Hi, I have been trying to return a data packet (as a char array) at the end of a function but that seems to be impossible... I would like to use this char array in a different class..anyone some...
8
by: ali | last post by:
Hi, I'm having a problem understanding the reason for output on the following code: #include <iostream.h> int main() {
6
by: A_StClaire_ | last post by:
hi, wrote the following program to learn a bit about auto pointers. weird thing is, the char array 'name' with size 'max_size' displays a bunch of messed up symbols after the "John Smith" if...
15
by: Kueishiong Tu | last post by:
How do I convert a Byte array (unsigned char managed) to a char array(unmanaged) with wide character taken into account?
2
by: Amrit Kohli | last post by:
Hello. I have the following code, to do a simple operation by copying the elements of a vector of strings into an array of char pointers. However, when I run this code, the first element in the...
9
by: rob.kirkpatrick | last post by:
Hello I need to populate an array of char arrays at run-time. A very simplifed version of the code is below. char ** list should contain cnt char arrays. The values of char ** list are set by...
15
by: thinktwice | last post by:
char a = { 0 } is it ok?
9
by: anon.asdf | last post by:
In terms of efficieny: Is it better to use multiple putchar()'s after one another as one gets to new char's OR is it better to collect the characters to a char-array first, and then use...
2
by: nagesh0280 | last post by:
Hi experts, I'm from a Verilog HDL background and trying to learn C. There are a lot of similarities between Verilog and C but the concept of char arrays and strings has me confused. I'd...
24
by: DomoChan | last post by:
the code below will compile in visual c++ 2003, but im not sure its valid. unsigned char myString = ""; after this line executes, all the bytes within myString are indeed set to '0's' but is...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.