Thread: Replacing character with the string

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    1

    Question Replacing character with the string

    Hello,
    i have a string:
    My *name* is Darryl
    And I want it to look like this:
    My <i>name</i> is Darryl
    I was trying to do in that way, 'line' is the string I am reading from.

    Code:
    char format[250];
    char pom[10];
    bool exist = false;
    int j = 0;
    
    for (i=0; i<=strlen(line); i++)
    {
        if(line[i] != '*')
        {
                      formated[j] = line[i];
                      j = j+1;
        }
        
        if ((line[i] == '*') && (exist == false))
        {
                        char pom[4]="<b>";
                        strcat(format, pom);
                        j=j+4;
                        exist=true;
        }
        if ((line[i] == '*') && (flaga == true))
        {
                        char pom[5]="</b>";
                        strcat(format, pom);
                        j=j+5;
                        exist=true;
        }
    Thank You for advice

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    OK, so what's the question? Does your code not work? not compile?

    It would be better if you published a complete (compilable) code and a specific question.

    (I noticed you try to access an array called formated, yet you've not declared it. Unless you mean format ?)

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
        if ((line[i] == '*') && (exist == false))
        {
                        char pom[4]="<b>";
                        strcat(format, pom);
                        j=j+4;
                        exist=true;
        }
        if ((line[i] == '*') && (flaga == true))
        {
                        char pom[5]="</b>";
                        strcat(format, pom);
                        j=j+5;
                        exist=true;
        }
    You are incrementing too far. You are skipping past the nul you are adding to the end, so your next step through ends up with a string like:

    my <b>\0name</b>\0 is...


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replacing character is character is different
    By s10062971 in forum C# Programming
    Replies: 4
    Last Post: 11-07-2010, 06:45 AM
  2. replacing string elements
    By Dorothy in forum C Programming
    Replies: 4
    Last Post: 06-17-2010, 09:40 AM
  3. Program crashing when replacing newline character
    By mdekom12 in forum C Programming
    Replies: 2
    Last Post: 05-01-2010, 08:49 PM
  4. replacing string in file except the first
    By jetfreggel in forum C Programming
    Replies: 4
    Last Post: 01-12-2003, 03:03 PM
  5. replacing a part of a string
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 05-20-2002, 03:32 AM