Advertisement

05.28.2008 at 10:07AM PDT, ID: 23438803
[x]
Attachment Details

How can I replace certain characters in a string?

Asked by geeta_m9 in C Programming Language

Tags: C

I would like to replace the characters \n, \r and \f whenever I encounter them in a string with <br>. To replace a single character, in the string, I could use the following code:
.
.
.
{
    char oldc, newc, *cp;
    char *s = "Hello World. Hello World";

    oldc = '\n';

    cp = s;

   while ((cp = strchr(sp, oldc)) != NULL)
      *cp = newc;

}

However, in this case, I want to replace 4 characters in place of the \n, \r or \f, i.e. <br>. I wonder if I have to use another string and copy the contents from the first string to the second string and replacing the \ characters with <br>.

If anyone can show me a simple and  effective way to do this, I would really appreciate it.
Start Free Trial
 
 
[+][-]05.28.2008 at 10:17AM PDT, ID: 21662305

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.28.2008 at 10:24AM PDT, ID: 21662375

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: C Programming Language
Tags: C
Sign Up Now!
Solution Provided By: Kdo
Participating Experts: 3
Solution Grade: A
 
 
[+][-]05.28.2008 at 11:55AM PDT, ID: 21663177

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]05.28.2008 at 12:06PM PDT, ID: 21663295

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628