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

how to remove a character from a string

Hello,
my problem is quite simple to explain.
I have the following string:

"table+camera"

and I want to remove the + sign:
"tablecamera".

How do i do that ?
Thanks for your help.
Nov 14 '05 #1
18 55696

On Wed, 7 Apr 2004, Toto wrote:

I have the following string:
"table+camera"
and I want to remove the + sign:
"tablecamera".

How do i do that ?

#include <string.h>

char s[] = "table+camera";
char *p;

while ((p = strchr(s,'+')) != NULL)
strcpy(p, p+1);

Make sure to read the FAQ on string manipulation, too.

HTH,
-Arthur
Nov 14 '05 #2
Toto <ma*@nospam.com> spoke thus:
"table+camera"
and I want to remove the + sign:
"tablecamera".


Well, I hope this isn't a homework question, because I'll post an
answer:

void rem_char( char *s, char t )
{
if( !s || !*s ) return;

while( *s++ != t ) if( !*s ) return;
while( *(s-1)=*s++ );
}

I don't guarantee that it's conforming, but it produced correct
results for me.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #3
"Arthur J. O'Dwyer" wrote:

On Wed, 7 Apr 2004, Toto wrote:

I have the following string:
"table+camera"
and I want to remove the + sign:
"tablecamera".

How do i do that ?


#include <string.h>

char s[] = "table+camera";
char *p;

while ((p = strchr(s,'+')) != NULL)
strcpy(p, p+1);


This is only guaranteed to work if the '+' is the
last character in the string. Otherwise the source and
destination overlap, which is a no-no for strcpy(). A
safe alternative is

memmove(p, p+1, strlen(p+1)+1);

.... which can be simplified to

memmove(p, p+1, strlen(p));

Also, the Little Tin God might be better propitiated
by replacing the `while' with

p = s;
while ((p = strchr(p, '+')) != NULL)

.... thus not searching and re-searching and re-re-searching
parts of `s' already known to be free of '+' characters.

--
Er*********@sun.com
Nov 14 '05 #4
"Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> writes:
On Wed, 7 Apr 2004, Toto wrote:

I have the following string:
"table+camera"
and I want to remove the + sign:
"tablecamera".

How do i do that ?

#include <string.h>

char s[] = "table+camera";
char *p;

while ((p = strchr(s,'+')) != NULL)
strcpy(p, p+1);


7.21.2.3#2 (strcpy): "[...] If copying takes place between objects that
overlap, the behavior is undefined."

(Your algorithm is also inefficient, because it scans `s' from the
beginning after replacing each '+' character.)

I suggest the following:

char s [] = "table+camera";
const char *src = s;
char *dst = s;

do
{
while (*src == '+')
++src;
}
while ((*dst++ = *src++) != '\0');

Martin
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
Nov 14 '05 #5
Eric Sosman wrote:

"Arthur J. O'Dwyer" wrote:

while ((p = strchr(s,'+')) != NULL)
strcpy(p, p+1);


This is only guaranteed to work if the '+' is the
last character in the string. [...]


And now that I think about it, not even then.

--
Er*********@sun.com
Nov 14 '05 #6

"Toto" <ma*@nospam.com> wrote in message
news:c5**********@news-reader4.wanadoo.fr...
Hello,
my problem is quite simple to explain.
I have the following string:

"table+camera"

and I want to remove the + sign:
"tablecamera".

How do i do that ?
Thanks for your help.


#define MAX_LEN 1000

char input_string[MAX_LEN];
char output_string[MAX_LEN];

strcpy(input_string, "table+camera");
int len = strlen(input_string);
int j =0;
for(int i =0; i<=len; i++)/*we wanna copy the null terminator*/
{
if(input_string[i] != '+')
{
output_string[j] = input_string[i];
j++;
}
}
Nov 14 '05 #7
Toto wrote:
Hello,
my problem is quite simple to explain.
I have the following string:

"table+camera"

and I want to remove the + sign:
"tablecamera".

How do i do that ?
Thanks for your help.

#include <stdio.h>
#include <string.h>

int main(void) {
char string[] = "table+camera";
char *s;
int i;
puts(string);
s = strchr(string, '+');
i = s - string + 1;
while ((*s++ = string[i++])) ;
puts(string);
return 0;
}

Try this.
--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #8
Mac
On Wed, 07 Apr 2004 22:34:59 +0200, Toto wrote:
Hello,
my problem is quite simple to explain.
I have the following string:

"table+camera"

and I want to remove the + sign:
"tablecamera".

How do i do that ?
Thanks for your help.


Use strchr() to find the character. Use memmove() to copy over it. If you
want to replace more than one instance of the character, do this
repeatedly until strchr() returns NULL.

If there is any chance that the string is long, be sure to avoid
re-searching the whole string for the character.

HTH

--Mac

Nov 14 '05 #9
Toto wrote:

Hello,
my problem is quite simple to explain.
I have the following string:

"table+camera"

and I want to remove the + sign:
"tablecamera".

How do i do that ?
Thanks for your help.


/* BEGIN new.c */

#include <stdio.h>

#define STRING "table+camera"
#define WHITE "+"

char *str_squeeze(char *, const char *);
char *str_tok_r(char *, const char *, char **);
size_t str_spn(const char *, const char *);
size_t str_cspn(const char *, const char *);
char *str_chr(const char *, int);
char *str_cpy(char *, const char *);

int main(void)
{
const char *const original = STRING;
char s1[sizeof STRING];

puts(str_squeeze(str_cpy(s1, original), WHITE));
return 0;
}

char *str_squeeze(char *s1, const char *s2)
{
char *p3;
char const *const p2 = s2;
char *const p1 = s1;

s2 = str_tok_r(p1, p2, &p3);
while (s2 != NULL) {
while (*s2 != '\0') {
*s1++ = *s2++;
}
s2 = str_tok_r(NULL, p2, &p3);
}
*s1 = '\0';
return p1;
}

char *str_tok_r(char *s1, const char *s2, char **p1)
{
if (s1 != NULL) {
*p1 = s1;
}
s1 = *p1 + str_spn(*p1, s2);
if (*s1 == '\0') {
return NULL;
}
*p1 = s1 + str_cspn(s1, s2);
if (**p1 != '\0') {
*(*p1)++ = '\0';
}
return s1;
}

size_t str_spn(const char *s1, const char *s2)
{
size_t n;

for (n = 0; *s1 != '\0' && str_chr(s2, *s1) != NULL; ++s1) {
++n;
}
return n;
}

size_t str_cspn(const char *s1, const char *s2)
{
size_t n;

for (n = 0; str_chr(s2, *s1) == NULL; ++s1) {
++n;
}
return n;
}

char *str_chr(const char *s, int c)
{
while (*s != (char)c) {
if (*s == '\0') {
return NULL;
}
++s;
}
return (char *)s;
}

char *str_cpy(char *s1, const char *s2)
{
char *const p1 = s1;

do {
*s1 = *s2++;
} while (*s1++ != '\0');
return p1;
}

/* END new.c */

--
pete
Nov 14 '05 #10
"Arthur J. O'Dwyer" wrote:
On Wed, 7 Apr 2004, Toto wrote:

I have the following string:
"table+camera"
and I want to remove the + sign:
"tablecamera".

How do i do that ?
#include <string.h>

char s[] = "table+camera";
char *p;

while ((p = strchr(s,'+')) != NULL)
strcpy(p, p+1);


from N869:

7.21.2.3 The strcpy function

Synopsis
[#1]
#include <string.h>
char *strcpy(char * restrict s1,
const char * restrict s2);
Description

[#2] The strcpy function copies the string pointed to by s2
(including the terminating null character) into the array
pointed to by s1. If copying takes place between objects
that overlap, the behavior is undefined.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
try:
#include <string.h>

char s[] = "table+camera";
char *p, *pp;

while ((p = strchr(s, '+'))) {
pp = p + 1;
while (*p++ = *pp++) continue;
}

Make sure to read the FAQ on string manipulation, too.


Here we agree :-)

--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 14 '05 #11
Toto wrote:
Hello,
my problem is quite simple to explain.
I have the following string:

"table+camera"

and I want to remove the + sign:
"tablecamera".

How do i do that ?
Thanks for your help.


#include <stdio.h>
int
main(void)
{
char *s="table+camera+chair";
char *p,*temp,c;
for(p=s,temp=s;c=*temp;temp++)
{
if(c != '+') *p++=c;
}
*p=0;
printf("%s",s);
return 0;
}

Nov 14 '05 #12
In article <news:40***************@sun.com>
Eric Sosman <Er*********@Sun.COM> wrote:
Also, the Little Tin God might be better propitiated
by replacing the `while' with

p = s;
while ((p = strchr(p, '+')) != NULL)

... thus not searching and re-searching and re-re-searching
parts of `s' already known to be free of '+' characters.


Of course, if one is bowing to this Little Tin God, one might want
to consider removing "+" characters from the back end first, so
that in:

this+that++some+other+stuff

you never copy any of the "+" characters. But then it might be
important to move every character at most once, which requires
counting up all the "+" characters up front, and ... :-)

Seriously though, one "backwards loop" version:

size_t len = strlen(s);
size_t i, j, n;

for (i = len; i > 0;) {
for (j = i; i > 0 && s[i - 1] == '+'; i--)
continue;
n = j - i;
if (n) {
/*
* There were some '+' characters (n in a row) that we
* would like to remove. Once they are removed, the
* string will be n bytes shorter. The last such '+'
* character is at s[i], i.e., we wish to keep s[i-1]
* unchanged (if it even exists) but clobber s[i].
*
* s[j] is not a '+' -- either it is a wanted character
* or it is the '\0' byte (j==len). Copying (len-j)+1
* bytes copies the '\0' too; we could copy just len-j
* but then we would have to re-terminate the string
* eventually (see below).
*/
memmove(&s[i], &s[j], len - j + 1);
len -= n;
}
}
/* s[len] = 0; -- do this only if we memmove() len-j without the +1 */

The code above is quite untested. :-)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #13
Martin Dickopp <ex****************@zero-based.org> wrote:
"Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> writes:
#include <string.h>

char s[] = "table+camera";
char *p;

while ((p = strchr(s,'+')) != NULL)
strcpy(p, p+1);
7.21.2.3#2 (strcpy): "[...] If copying takes place between objects that
overlap, the behavior is undefined."


I don't like this (although I'm sure it is correct). I have always
been in the habit of feeling safe about strcpy, strncpy, *printf,
memcpy, etc. as long as dst < src , and using memmove or some other
technique if dst > src.
I suggest the following: [snip] while ((*dst++ = *src++) != '\0');


Some C-teaching books have used exactly that as the
implementation of strcpy (with some extra code to return the
correct return value, of course). It is unfortunate that it
is OK to write this out, but not OK to call strcpy !
Nov 14 '05 #14
ol*****@inspire.net.nz (Old Wolf) wrote:
Martin Dickopp <ex****************@zero-based.org> wrote:
"Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> writes: <snip>
> while ((p = strchr(s,'+')) != NULL)
> strcpy(p, p+1);


7.21.2.3#2 (strcpy): "[...] If copying takes place between objects that
overlap, the behavior is undefined."


I don't like this (although I'm sure it is correct). I have always
been in the habit of feeling safe about strcpy, strncpy, *printf,
memcpy, etc. as long as dst < src , and using memmove or some other
technique if dst > src.


Bad habit. Bad, bad habit. ;-)
I suggest the following:

[snip]
while ((*dst++ = *src++) != '\0');


Some C-teaching books have used exactly that as the
implementation of strcpy (with some extra code to return the
correct return value, of course). It is unfortunate that it
is OK to write this out, but not OK to call strcpy !


The rationale however is clear: the standard imposes no detailed
requirements on the internals of strcpy to allow RealWorld[tm]
implementations to use the underlying platform's most efficient way
to copy the data. To stay on the safe side use memmove, which
is explicitly required to copy data between overlapping objects.

Just to add that Martin's solution avoids multiple data movement in
case the string contains more than one instance of the character to
delete.

That said, the following version _might_ perform _slightly_ better,
if the passed string has a long initial (or consists entirely of a)
sequence not containing the character to delete:

char *igStrsqz( char *str, int ch )
{
char *dst = strchr( str, ch );

if ( dst != NULL )
{
const char *src = dst;
while ( *dst != '\0' )
{
while ( *src == ch )
src++;
*dst++ = *src++;
}
}
return str;
}

Regards
--
Irrwahn Grausewitz (ir*******@freenet.de)
welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc OT guide : http://benpfaff.org/writings/clc/off-topic.html
Nov 14 '05 #15
"(2B) || !(2B)" <sp**@spam.net> wrote in message news:<40**************@spam.net>...
Toto wrote:
Hello,
my problem is quite simple to explain.
I have the following string:

"table+camera"

and I want to remove the + sign:
"tablecamera".

How do i do that ?
Thanks for your help.
#include <stdio.h>
int
main(void)
{
char *s="table+camera+chair";


ITYM:
char s[]="table+camera+chair";
char *p,*temp,c;
for(p=s,temp=s;c=*temp;temp++)
{
if(c != '+') *p++=c;
}
*p=0;
printf("%s",s);
return 0;
}


regards,
goose
Nov 14 '05 #16
goose wrote:
ITYM:
char s[]="table+camera+chair";
regards,
goose

Thanks for correcting me.

Nov 14 '05 #17
Chris Torek wrote:

In article <news:40***************@sun.com>
Eric Sosman <Er*********@Sun.COM> wrote:
Also, the Little Tin God might be better propitiated
by replacing the `while' with

p = s;
while ((p = strchr(p, '+')) != NULL)

... thus not searching and re-searching and re-re-searching
parts of `s' already known to be free of '+' characters.


Of course, if one is bowing to this Little Tin God, one might want
to consider removing "+" characters from the back end first, so
that in:

this+that++some+other+stuff

you never copy any of the "+" characters. But then it might be
important to move every character at most once, which requires
counting up all the "+" characters up front, and ... :-)

.... snip ...

An untested nocount version, returning revised strlen, might be:

size_t str_strip(char *s, char x)
{
char *si, *d;

si = d = s;
while ((*d = *s++))
if (*d != x) d++;
return (d - si);
} /* str_strip */

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #18
may be use sed....

sed 's/+//g' file

where file is the file containing your input strings like table+camera etc
Nov 14 '05 #19

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

Similar topics

1
by: Ren? M?hle | last post by:
I have a psp script with a procedure just to run an update on one table. The Problem occurs when I try to compile this script with pspload: ORA-20006: "frsys_updatereport.psp": compilation...
1
by: rusttree | last post by:
I'm working on a program that manipulates bmp files. I know the offset location of each piece of relevent data within the bmp file. For example, I know the 18th through 21st byte is an integer...
2
by: jt | last post by:
Looking for an example how to convert and CString to an ASCII character string. Any examples on how to do this? Thank you, jt
0
by: MLH | last post by:
Is an apostrophe a character of special significance to MySQL in a way that would cause "Bob's dog" to become translated into a 12-character string when typed into a MySQL memo field? If I type...
2
by: Roy Rodsson via .NET 247 | last post by:
Hi all! I am using a stored procedure in SQL2000 for retrieving fileinformations from a db. the table as an uniqueidentifier for the file information. The stored procedure has a variable...
7
by: Justin | last post by:
i need to build the unsigned character string: "PR0N\0Spam\0G1RLS\0Other\0Items\0\0\0" from the signed character string: "PR0N Spam G1RLS Other Items" Tokeninzing the character string is not...
5
by: Karthik | last post by:
Hello! I am not a wizard in this area! Just need some help out in this. I am trying to convert bstr string to new character string. Here is the snippet of my code. **** Code Start**** ...
25
by: lovecreatesbeauty | last post by:
Hello experts, I write a function named palindrome to determine if a character string is palindromic, and test it with some example strings. Is it suitable to add it to a company/project library...
8
by: Brand Bogard | last post by:
Does the C standard include a library function to convert an 8 bit character string to a 16 bit character string?
3
by: Gmuchan | last post by:
Hi, Help needed please. Basically I am wanting to update a 16 digit character string which is in a table called say accounts. If the account no. field is say 0502562389568745 I want to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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: 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...
1
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.