Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

replacing chars within a string

Posted on 1999-07-12
3
Medium Priority
?
173 Views
Last Modified: 2010-04-15
Hello,
I have to change "home%2fsome+one" for "home/some one" using ansi C on linux.
I had something like
    char *chptr;
    chptr = "marcelo-iturbe";
    while (*chptr++) if (*chptr == '-') *chptr = '_';
but that core dumps and it doesn't cover replcing %2f for a /
Any help is apreciated.
thanks
Marcelo
0
Comment
Question by:sinner052397
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 10

Accepted Solution

by:
rbr earned 200 total points
ID: 1263511
Core dumped since chptr is read only.
use chptr[40];
For decoding this use

char x2c (char *what)
{
    char digit;
    digit =(what[0] >= ´A´ ? ((what[0] & 0xdf) - ´A´)+10: (what[0] - ´0´));
    digit *=16;
    digit +=(what[1] >= ´A´ ? ((what[1] & 0xdf) - ´A´)+10: (what[1] - ´0´));
     return (digit);
}

void unescape_url (char *url)
{
      int x,y;

      for (x=0,y=0;url[y];++x,++y) {
           if ((url[x]=url[y]) == ´%´ ) {
             url[x] =x2c (&url[y+1]);
             y+=2;
          }
      }
      url [x]=0;
}
0
 

Author Comment

by:sinner052397
ID: 1263512
Is there any way that this can work with an assigment like
    char *chptr2 = "marcelo%2fiturbe+vera";
instead of
    char chptr[40] = "marcelo%2fiturbe+vera";

thanks

0
 
LVL 10

Expert Comment

by:rbr
ID: 1263513
No you can^t store the result at char *chptr2. The memory at this location is set read only. The only way is to use a second buffer.


char x2c (char *what)
        {
            char digit;
            digit =(what[0] >= ´A´ ? ((what[0] & 0xdf) - ´A´)+10: (what[0] - ´0´));
            digit *=16;
            digit +=(what[1] >= ´A´ ? ((what[1] & 0xdf) - ´A´)+10: (what[1] - ´0´));
             return (digit);
        }

char *unescape_url (char *url)
        {
              int x,y;
              char *result;
               result =malloc (strlen(url)+1);
              strcpy (result,url);
              for (x=0,y=0;results[y];++x,++y) {
             if ((result[x]=result[y]) == ´%´ ) {
                     result[x] =x2c (&result[y+1]);
                     y+=2;
                  }
              }
              result [x]=0;
               return(result);
        }





0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question