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... 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
> > 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?
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
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
> 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 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
>> 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 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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()
{
|
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...
|
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?
|
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...
|
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...
|
by: thinktwice |
last post by:
char a = { 0 }
is it ok?
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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...
| |