Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > how to remove a character from a string

Reply
Thread Tools

how to remove a character from a string

 
 
Toto
Guest
Posts: n/a
 
      04-07-2004
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.


 
Reply With Quote
 
 
 
 
Arthur J. O'Dwyer
Guest
Posts: n/a
 
      04-07-2004

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
 
Reply With Quote
 
 
 
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      04-07-2004
Toto <> 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.
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      04-07-2004
"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.

--

 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      04-07-2004
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.

--

 
Reply With Quote
 
Martin Dickopp
Guest
Posts: n/a
 
      04-07-2004
"Arthur J. O'Dwyer" <> 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. \_/
 
Reply With Quote
 
code_wrong
Guest
Posts: n/a
 
      04-07-2004

"Toto" <> wrote in message
news:c51olk$3fb$...
> 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++;
}
}


 
Reply With Quote
 
Joe Wright
Guest
Posts: n/a
 
      04-07-2004
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 private.php?do=newpm&u=
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      04-08-2004
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
 
Reply With Quote
 
Mac
Guest
Posts: n/a
 
      04-08-2004
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Convert string with control character in caret notation to realcontrol character string. Bart Vandewoestyne C Programming 8 09-25-2012 12:41 PM
remove the last character or the newline character? Daniel Mark Python 6 09-28-2006 02:40 PM
8 bit character string to 16 bit character string Brand Bogard C Programming 8 05-28-2006 05:05 PM
getting the character code of a character in a string Velvet ASP .Net 9 01-19-2006 09:27 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57