replace space by underscore 
Author Message
 replace space by underscore

Hi,

I would like to replace a space character by an underscore character in
a pointer.
char line[260];
char title[260];
char *ptr;

ptr = line;
for (i=0;i<9;i++)
   {
    *ptr++;
   } // To get rid of the first 9 characters

The pointer ptr at that stage contains line without the 9 first
characters.
I would like to replace any space characters that there could be in ptr
and replace it by an underscore and then copy the new ptr in a string
called title.
strcpy(title, ptr);

How could I do, please?
Thanks.



Tue, 27 Nov 2001 03:00:00 GMT  
 replace space by underscore


Quote:

> I would like to replace a space character by an underscore character in
> a pointer.
> char line[260];
> char title[260];
> char *ptr;

> ptr = line;
> for (i=0;i<9;i++)
>    {
>     *ptr++;
>    } // To get rid of the first 9 characters

> The pointer ptr at that stage contains line without the 9 first
> characters.
> I would like to replace any space characters that there could be in ptr
> and replace it by an underscore and then copy the new ptr in a string
> called title.
> strcpy(title, ptr);

> How could I do, please?

You've already got most of what you need; looping through a
string pointed to by p can be achieved through
    for( ; *p; p++) {
        /* work on the specific character *p here */
    }
and to replace a space with an underscore within the loop,
        if (*p == ' ')
            *p = '_';
The one other step you'll need is to avoid changing the value of ptr
in the loop, which is why I wrote p instead of ptr above; so do
    p = ptr;
before the loop.  If you don't want to change the original string, you
can copy the string first and then apply the above steps to the copy.

--
MJSR

Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.



Tue, 27 Nov 2001 03:00:00 GMT  
 replace space by underscore
On Fri, 11 Jun 1999 11:40:37 +0100, in comp.lang.c, s gontier

Quote:

>Hi,

>I would like to replace a space character by an underscore character in
>a pointer.
>char line[260];
>char title[260];
>char *ptr;

>ptr = line;
>for (i=0;i<9;i++)
>   {
>    *ptr++;
>   } // To get rid of the first 9 characters

First, instead of this elaborate for loop, do:

ptr+=9;

Quote:

>The pointer ptr at that stage contains line without the 9 first
>characters.
>I would like to replace any space characters that there could be in ptr
>and replace it by an underscore and then copy the new ptr in a string
>called title.
>strcpy(title, ptr);

for(i=ptr; *ptr; ptr++)
        if( *ptr == ' ' )
                *ptr = '_';
strcpy(title, ptr);

It's that simple.

Moshe
--

Delete _NO_SPAM_ when replying!



Tue, 27 Nov 2001 03:00:00 GMT  
 replace space by underscore


Quote:
> On Fri, 11 Jun 1999 11:40:37 +0100, in comp.lang.c, s gontier

> >I would like to replace any space characters that there could be in ptr
> >and replace it by an underscore and then copy the new ptr in a string
> >called title.
> >strcpy(title, ptr);

> for(i=ptr; *ptr; ptr++)
>    if( *ptr == ' ' )
>            *ptr = '_';
> strcpy(title, ptr);

> It's that simple.

Not quite that simple, since ptr no longer points to the desired string.

--
MJSR

Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.



Tue, 27 Nov 2001 03:00:00 GMT  
 replace space by underscore


Quote:
> On Fri, 11 Jun 1999 11:40:37 +0100, in comp.lang.c, s gontier

> >Hi,

> >I would like to replace a space character by an underscore character in
> >a pointer.
> >char line[260];
> >char title[260];
> >char *ptr;

> >ptr = line;
> >for (i=0;i<9;i++)
> >   {
> >    *ptr++;
> >   } // To get rid of the first 9 characters

> First, instead of this elaborate for loop, do:

> ptr+=9;

> >The pointer ptr at that stage contains line without the 9 first
> >characters.
> >I would like to replace any space characters that there could be in ptr
> >and replace it by an underscore and then copy the new ptr in a string
> >called title.
> >strcpy(title, ptr);

> for(i=ptr; *ptr; ptr++)
>    if( *ptr == ' ' )
>            *ptr = '_';
> strcpy(title, ptr);

> It's that simple.

Actually, it's not quite that simple. At the end of your loop, ptr will be
pointing at the character after the '\0'.

strcpy(title, i); would be okay though. (Assuming i has been declared as
char *).

--
Richard Heathfield

The bug stops here.



Tue, 27 Nov 2001 03:00:00 GMT  
 replace space by underscore


Quote:
>On Fri, 11 Jun 1999 11:40:37 +0100, in comp.lang.c, s gontier

>>Hi,

>>I would like to replace a space character by an underscore character in
>>a pointer.
>>char line[260];
>>char title[260];
>>char *ptr;

>>ptr = line;
>>for (i=0;i<9;i++)
>>   {
>>    *ptr++;
>>   } // To get rid of the first 9 characters

>First, instead of this elaborate for loop, do:

>ptr+=9;

Or if you go back one line further you could write

 ptr = line + 9;

Quote:
>>The pointer ptr at that stage contains line without the 9 first
>>characters.
>>I would like to replace any space characters that there could be in ptr
>>and replace it by an underscore and then copy the new ptr in a string
>>called title.
>>strcpy(title, ptr);

>for(i=ptr; *ptr; ptr++)

i is used as an integer above so this can't be right.

Quote:
>        if( *ptr == ' ' )
>                *ptr = '_';
>strcpy(title, ptr);

At this point ptr will be pointing at the null character at the end of
the string. Maybe

 strcpy(title, line+9);

On the other hand if you're making a copy anyway there may be no need to
modify the original. In that case something liek this would do the trick:

    i = 0;
    do {
        ch = line[i+9];
        if (ch == ' ')
            ch = '_';
        title[i++] = ch;
    } while (ch != '\0');

--
-----------------------------------------


-----------------------------------------



Tue, 27 Nov 2001 03:00:00 GMT  
 replace space by underscore
Groovy hepcat s gontier was jivin' on Fri, 11 Jun 1999 11:40:37 +0100
in comp.lang.c.
replace space by underscore's a cool scene! Dig it!

Quote:
>I would like to replace a space character by an underscore character in
>a pointer.
>char line[260];
>char title[260];
>char *ptr;

>ptr = line;
>for (i=0;i<9;i++)
>   {
>    *ptr++;
>   } // To get rid of the first 9 characters

  This does not skip 9 characters. It increments the first character
by 9; so if, for example, the first character was 'A', now it is 'J'
(on an ASCII system). The pointer will still point to this character.
What you meant was ptr++, not *ptr++.
  However, this method of incrementing a pointer is rather inefficent.
You could (and should) just add 9 to the pointer, even when assigning
its initial value. That is, just do this:

ptr = line + 9;

  BTW, don't use C++ comments here. They are not supported by the
standard, and are frowned upon by many regulars here (myself
included).

Quote:
>I would like to replace any space characters that there could be in ptr
>and replace it by an underscore and then copy the new ptr in a string

  Now, this is where a loop might come in handy. You could do
something like this:

while(*ptr)
{
  if(*ptr == ' ')
    *ptr = '_';
  ptr++;

Quote:
}

Want another way? OK, here's one:

while((ptr = strchr(ptr, ' ')) != NULL)
  *ptr = '_';
--

----- Dig the EVEN NEWER, MORE IMPROVED news sig!! -----

-------------- Shaggy was here! ---------------
    http://aardvark.apana.org.au/~phaywood/
============= Ain't I'm a dawg!! ==============



Fri, 30 Nov 2001 03:00:00 GMT  
 replace space by underscore


Quote:
> Groovy hepcat s gontier was jivin' on Fri, 11 Jun 1999 11:40:37 +0100
> in comp.lang.c.
> replace space by underscore's a cool scene! Dig it!

> >I would like to replace a space character by an underscore character in
> >a pointer.
> >char line[260];
> >char title[260];
> >char *ptr;

> >ptr = line;
> >for (i=0;i<9;i++)
> >   {
> >    *ptr++;
> >   } // To get rid of the first 9 characters

>   This does not skip 9 characters. It increments the first character
> by 9; so if, for example, the first character was 'A', now it is 'J'
> (on an ASCII system). The pointer will still point to this character.
> What you meant was ptr++, not *ptr++.

Sorry, Shaggy, but it DOES skip 9 characters. The contents of p are
evaluated and, in this case, discarded, and then the ptr variable is
incremented. In this context, the outcome of *ptr++ and ptr++ would be the
same.

<snip>

--
Richard Heathfield

The bug stops here.



Sat, 01 Dec 2001 03:00:00 GMT  
 replace space by underscore


Quote:


> > >ptr = line;
> > >for (i=0;i<9;i++)
> > >   {
> > >    *ptr++;
> > >   } // To get rid of the first 9 characters

> >   This does not skip 9 characters. It increments the first character
> > by 9; so if, for example, the first character was 'A', now it is 'J'
> > (on an ASCII system). The pointer will still point to this character.
> > What you meant was ptr++, not *ptr++.

> Sorry, Shaggy, but it DOES skip 9 characters. The contents of p are
> evaluated and, in this case, discarded, and then the ptr variable is
> incremented. In this context, the outcome of *ptr++ and ptr++ would be the
> same.

But you don't know _when_ the ptr variable is incremented.  Postfix
++ has higher precedence than prefix *, so it applies to ptr and not
to (*ptr), no matter when it happens (before the next sequence point).

--
MJSR

Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.



Sat, 01 Dec 2001 03:00:00 GMT  
 replace space by underscore

Quote:



in

> > > >ptr = line;
> > > >for (i=0;i<9;i++)
> > > >   {
> > > >    *ptr++;
> > > >   } // To get rid of the first 9 characters

> > >   This does not skip 9 characters. It increments the first character
> > > by 9; so if, for example, the first character was 'A', now it is 'J'
> > > (on an ASCII system). The pointer will still point to this character.
> > > What you meant was ptr++, not *ptr++.

> > Sorry, Shaggy, but it DOES skip 9 characters. The contents of p are
> > evaluated and, in this case, discarded, and then the ptr variable is
> > incremented. In this context, the outcome of *ptr++ and ptr++ would be
the
> > same.

> But you don't know _when_ the ptr variable is incremented.  Postfix
> ++ has higher precedence than prefix *, so it applies to ptr and not
> to (*ptr), no matter when it happens (before the next sequence point).

Er, are you {*filter*}ly agreeing with me? <g>

You are certainly correct in stating that we can't tell when ptr is
incremented (and I was wrong to use the word 'then' in my second sentence -
thank you for your correction), but we can be certain that the value of the
expression is (*ptr), and that the _pointer_ is incremented, not the
_contents_ of the pointer.

In any case, that code ain't gonna turn *ptr from A into J or any other
letter.

--
Richard Heathfield

The bug stops here.



Sun, 02 Dec 2001 03:00:00 GMT  
 replace space by underscore
Groovy hepcat Richard Heathfield was jivin' on 15 Jun 1999 18:54:35
+0100 in comp.lang.c.
Re: replace space by underscore's a cool scene! Dig it!

Quote:
>> >for (i=0;i<9;i++)
>> >   {
>> >    *ptr++;
>> >   } // To get rid of the first 9 characters

>>   This does not skip 9 characters. It increments the first character

>Sorry, Shaggy, but it DOES skip 9 characters. The contents of p are

  Oh yeah, that's right. I knew that. My silly mistake! I really aught
to get some sleep. Maybe I will sometime in the next few months. :)
  Sorry if I mislead anyone!
--

----- Dig the EVEN NEWER, MORE IMPROVED news sig!! -----

-------------- Shaggy was here! ---------------
    http://aardvark.apana.org.au/~phaywood/
============= Ain't I'm a dawg!! ==============



Fri, 07 Dec 2001 03:00:00 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. Replacing NULLS with spaces in a string?

2. Replace more then one charaster using String.Replace()

3. regular expressions expression with space and then multiple spaces

4. How To Retrieve Free Disk Space and Total Disk Space

5. How To Retrieve Free Disk Space and Total Disk Space

6. underscore

7. identifiers beginning with underscores?

8. What does the underscore mean/do?

9. underscores in cpp #defines

10. Underscore - should they or should they not?

11. CLS, naming, underscore, and readonly properties

12. Problems with functions beginning with underscore (_)

 

 
Powered by phpBB® Forum Software