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
Medium Priority
173 Views
Hello,
I have to change "home%2fsome+one" for "home/some one" using ansi C on linux.
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
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
• 2

LVL 10

Accepted Solution

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

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

thanks

0

LVL 10

Expert Comment

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

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…
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
###### Suggested Courses
Course of the Month4 days, 15 hours left to enroll