DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 4 of 4
  1. #1
    Sue Mitchell Guest

    String Manipulation in C


    I am stuck with a problem here in trying to manipulate a string in C. What
    I need to do is replace any occurance of "<" or ">" characters with "[["
    and "]]" respectively. This is a piece of cake to do in VB or in Perl but
    I haven't a clue how to do this in C. Please, please can anyone help me
    here?

    An example would be: <b>Welcome to my World.</b><i>Time to start</i>
    would
    end up looking like: []Welcome to my World.[][]Time to start[]

    Sue


  2. #2
    jonnin Guest

    Re: String Manipulation in C


    copy from one string to another in a loop. when you find one of the special
    tokens, copy the replacement over instead. end with 0 to null the c string.
    string.h has some tools to get there (used here), the stl for c++ (you said
    c) has better ones if you can use that.

    heres the nasty c code...

    dx = 0;
    for(i = 0; i < strlen(string1); i++)
    {
    if (string1[i] == '<' )
    {
    string2[dx++] = '[';
    string2[dx++] = '[';
    }
    else
    if (string1[i] == '>' )
    {
    string2[dx++] = '>';
    string2[dx++] = '>';
    }
    else
    string2[dx++] = string1[i];


    }
    dx++;
    string2[dx] = 0;


    "Sue Mitchell" <sue.mitchell@cognos.com> wrote:
    >
    >I am stuck with a problem here in trying to manipulate a string in C. What
    >I need to do is replace any occurance of "<" or ">" characters with "[["
    >and "]]" respectively. This is a piece of cake to do in VB or in Perl but
    >I haven't a clue how to do this in C. Please, please can anyone help me
    >here?
    >
    >An example would be: <b>Welcome to my World.</b><i>Time to start</i>
    >would
    >end up looking like: []Welcome to my World.[][]Time to start[]
    >
    >Sue
    >



  3. #3
    Sue Mitchell Guest

    Re: String Manipulation in C


    Thank you for your help, I had forgotten that strings are meerly an array
    of characters in C. In trying your code however I get an unhandled exception
    at the string2[dx++]=string1[i] line? I will continue to work on it but
    I was wondering whether I was missing something.

    Sue


    "jonnin" <jonnin@vol.com> wrote:
    >
    >copy from one string to another in a loop. when you find one of the special
    >tokens, copy the replacement over instead. end with 0 to null the c string.
    > string.h has some tools to get there (used here), the stl for c++ (you

    said
    >c) has better ones if you can use that.
    >
    >heres the nasty c code...
    >
    >dx = 0;
    >for(i = 0; i < strlen(string1); i++)
    > {
    > if (string1[i] == '<' )
    > {
    > string2[dx++] = '[';
    > string2[dx++] = '[';
    > }
    >else
    > if (string1[i] == '>' )
    > {
    > string2[dx++] = '>';
    > string2[dx++] = '>';
    > }
    >else
    > string2[dx++] = string1[i];
    >
    >
    >}
    >dx++;
    >string2[dx] = 0;
    >
    >
    >"Sue Mitchell" <sue.mitchell@cognos.com> wrote:
    >>
    >>I am stuck with a problem here in trying to manipulate a string in C.

    What
    >>I need to do is replace any occurance of "<" or ">" characters with "[["
    >>and "]]" respectively. This is a piece of cake to do in VB or in Perl

    but
    >>I haven't a clue how to do this in C. Please, please can anyone help me
    >>here?
    >>
    >>An example would be: <b>Welcome to my World.</b><i>Time to start</i>
    >>would
    >>end up looking like: []Welcome to my World.[][]Time to start[]
    >>
    >>Sue
    >>

    >



  4. #4
    jonnin Guest

    Re: String Manipulation in C


    I could have fooed that, but it looks ok to me. The idea was start dx at
    0 and increment each time you add a letter to string2. String2 is bigger
    than string1 (by 1 char every time string1 has a < or >. Did you run off
    the end of a string? and did you set dx = 0? looks like I had > not ] in
    the second loop, duh...

    or did you get it, the email said yes and this said no...


    "Sue Mitchell" <sue.mitchell@cognos.com> wrote:
    >
    >Thank you for your help, I had forgotten that strings are meerly an array
    >of characters in C. In trying your code however I get an unhandled exception
    >at the string2[dx++]=string1[i] line? I will continue to work on it but
    >I was wondering whether I was missing something.
    >
    >Sue
    >
    >
    >"jonnin" <jonnin@vol.com> wrote:
    >>
    >>copy from one string to another in a loop. when you find one of the special
    >>tokens, copy the replacement over instead. end with 0 to null the c string.
    >> string.h has some tools to get there (used here), the stl for c++ (you

    >said
    >>c) has better ones if you can use that.
    >>
    >>heres the nasty c code...
    >>
    >>dx = 0;
    >>for(i = 0; i < strlen(string1); i++)
    >> {
    >> if (string1[i] == '<' )
    >> {
    >> string2[dx++] = '[';
    >> string2[dx++] = '[';
    >> }
    >>else
    >> if (string1[i] == '>' )
    >> {
    >> string2[dx++] = '>';
    >> string2[dx++] = '>';
    >> }
    >>else
    >> string2[dx++] = string1[i];
    >>
    >>
    >>}
    >>dx++;
    >>string2[dx] = 0;
    >>
    >>
    >>"Sue Mitchell" <sue.mitchell@cognos.com> wrote:
    >>>
    >>>I am stuck with a problem here in trying to manipulate a string in C.


    >What
    >>>I need to do is replace any occurance of "<" or ">" characters with "[["
    >>>and "]]" respectively. This is a piece of cake to do in VB or in Perl

    >but
    >>>I haven't a clue how to do this in C. Please, please can anyone help

    me
    >>>here?
    >>>
    >>>An example would be: <b>Welcome to my World.</b><i>Time to start</i>
    >>>would
    >>>end up looking like: []Welcome to my World.[][]Time to start[]
    >>>
    >>>Sue
    >>>

    >>

    >



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links