473,837 Members | 1,648 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

replace string

hi,
I have a url which is a char *

char * url = "/url=%2Fhome%2Fs umit%2Fpackages %2Fmyxml.xml";

when i send this URL to http server it was like -

"/url=/home/sumit/pacakges/myxml.xml"

but when it get by http server it convert '/' to '%2F' i have to
reconvert it to '/'
i did so many string operation but i did not get real string ,
still i am trying. can anyone please give me idea how to replace '%2F' to
'/'
Thanks


Nov 4 '08 #1
17 2332
Sumit wrote:
hi,
I have a url which is a char *

char * url = "/url=%2Fhome%2Fs umit%2Fpackages %2Fmyxml.xml";

when i send this URL to http server it was like -

"/url=/home/sumit/pacakges/myxml.xml"

but when it get by http server it convert '/' to '%2F' i have to
reconvert it to '/'
i did so many string operation but i did not get real string ,
still i am trying. can anyone please give me idea how to replace '%2F' to
'/'
char url[] = "/url=%2Fhome%2Fs umit%2Fpackages %2Fmyxml.xml";
char *p;
while ((p = strstr(url, "%2F")) != NULL) {
*p = '/';
memmove (p+1, p+3, strlen(p+2));
}

Note the change in the first line. Also note that this is not
especially efficient, nor tremendously flexible -- but it's what you
asked for, so ...

--
Er*********@sun .com

Nov 4 '08 #2
On 4 Nov 2008 at 17:02, Eric Sosman wrote:
char url[] = "/url=%2Fhome%2Fs umit%2Fpackages %2Fmyxml.xml";
char *p;
while ((p = strstr(url, "%2F")) != NULL) {
*p = '/';
memmove (p+1, p+3, strlen(p+2));
}

Also note that this is not especially efficient, nor tremendously
flexible
Talk about the understatement of the century - that's right out of the
Richard Heath Field school of efficiency.

Do you get a kick out of mocking new posters by trying to lead them down
the garden path with a deliberately silly solution Eric?

Nov 4 '08 #3
Sumit wrote, On 04/11/08 16:53:
hi,
I have a url which is a char *

char * url = "/url=%2Fhome%2Fs umit%2Fpackages %2Fmyxml.xml";

when i send this URL to http server it was like -

"/url=/home/sumit/pacakges/myxml.xml"
It may look like that, but if the string is defined using "%2F" then I
would expect that to be what is actually transmitted.
but when it get by http server it convert '/' to '%2F' i have to
reconvert it to '/'
i did so many string operation but i did not get real string ,
still i am trying. can anyone please give me idea how to replace '%2F' to
'/'
Show us your attempt. Doing this processing is not difficult, just scan
the string for a % and treat the following two characters as a hex
number representing the required character then copy the rest of the
string (whilst still looking for % characters). Of course, you also have
to decide what to do with invalid input.
--
Flash Gordon
If spamming me sent it to sm**@spam.cause way.com
If emailing me use my reply-to address
See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/
Nov 4 '08 #4

"Sumit" <su********@gma il.comwrote in message
news:49******** *************** @news.sunsite.d k...
hi,
I have a url which is a char *

char * url = "/url=%2Fhome%2Fs umit%2Fpackages %2Fmyxml.xml";

when i send this URL to http server it was like -

"/url=/home/sumit/pacakges/myxml.xml"

but when it get by http server it convert '/' to '%2F' i have to
reconvert it to '/'
i did so many string operation but i did not get real string ,
still i am trying. can anyone please give me idea how to replace '%2F' to
'/'
The following might work. But you probably need a more general solution.

#include <string.h>

void fixstring(char *s) {
char *t,*u,*v;

t=u=s;
v=s+strlen(s)-3; /* Last possible %2F position */

/* copy chars from *u to *t, replacing %2F sequences with / */

while (*u) {
if (u<=v && *u=='%' && *(u+1)=='2' && *(u+2)=='F') {
*t++='/';
u+=3;
}
else
*t++=*u++;
}
*t=0;
}
Nov 4 '08 #5
"Bartc" <bc@freeuk.comw rites:
"Sumit" <su********@gma il.comwrote in message
news:49******** *************** @news.sunsite.d k...
>hi,
I have a url which is a char *

char * url = "/url=%2Fhome%2Fs umit%2Fpackages %2Fmyxml.xml";

when i send this URL to http server it was like -

"/url=/home/sumit/pacakges/myxml.xml"

but when it get by http server it convert '/' to '%2F' i have to
reconvert it to '/'
i did so many string operation but i did not get real string ,
still i am trying. can anyone please give me idea how to replace '%2F' to
'/'

The following might work. But you probably need a more general solution.

#include <string.h>

void fixstring(char *s) {
char *t,*u,*v;

t=u=s;
v=s+strlen(s)-3; /* Last possible %2F position */
This can make v invalid. You can get undefined behaviour just by
computing it. Having computed it you can't ask if u <= v (and get a
defined result) although it often works.
/* copy chars from *u to *t, replacing %2F sequences with / */

while (*u) {
if (u<=v && *u=='%' && *(u+1)=='2' && *(u+2)=='F') {
I like to write this sort of test like this:

if (u <= v && strncmp(u, "%2F", 3) == 0)

since it makes what is sought so very clear.
*t++='/';
u+=3;
}
else
*t++=*u++;
}
*t=0;
}
I now feel I must show an tell so I am not just being negative. I'd
write it like this:

char *decode_hex(cha r *dest, const char *src)
{
char c, *dp = dest;
while ((c = *src++) != '\0') {
int nc;
unsigned int uc;
if (c == '%' && sscanf(src, "%2x%n", &uc, &nc) == 1 && nc == 2) {
src += nc;
*dp++ = uc;
}
else *dp++ = c;
}
*dp = '\0';
return dest;
}

The return of the string is just for convenience in larger expressions
and separating the source from the destination (which can, of course,
be the same) allows the function to be used on unmodifiable strings.

--
Ben.
Nov 4 '08 #6

"Ben Bacarisse" <be********@bsb .me.ukwrote in message
news:87******** ****@bsb.me.uk. ..
"Bartc" <bc@freeuk.comw rites:
>"Sumit" <su********@gma il.comwrote in message
news:49******* *************** *@news.sunsite. dk...
>>hi,
I have a url which is a char *

char * url = "/url=%2Fhome%2Fs umit%2Fpackages %2Fmyxml.xml";

when i send this URL to http server it was like -

"/url=/home/sumit/pacakges/myxml.xml"

but when it get by http server it convert '/' to '%2F' i have to
reconvert it to '/'
i did so many string operation but i did not get real string ,
still i am trying. can anyone please give me idea how to replace '%2F'
to
'/'

The following might work. But you probably need a more general solution.

#include <string.h>

void fixstring(char *s) {
char *t,*u,*v;

t=u=s;
v=s+strlen(s )-3; /* Last possible %2F position */

This can make v invalid. You can get undefined behaviour just by
computing it. Having computed it you can't ask if u <= v (and get a
defined result) although it often works.
Yes I was wondering if there was a problem in the rare case where v becomes
(or would become) negative.

--
Bartc

Nov 5 '08 #7

Sumit <su********@gma il.comwrote in message
news:49******** *************** @news.sunsite.d k...
hi,
I have a url which is a char *

char * url = "/url=%2Fhome%2Fs umit%2Fpackages %2Fmyxml.xml";

when i send this URL to http server it was like -

"/url=/home/sumit/pacakges/myxml.xml"

but when it get by http server it convert '/' to '%2F' i have to
reconvert it to '/'
i did so many string operation but i did not get real string ,
still i am trying. can anyone please give me idea how to replace '%2F' to
'/'
Well, I could tell you exactly how to do it, but it might be easier
to use an existing library that can encode/decode URLs, which
appears to be what you want to do (the big advantage is of
course that you don't have to re-invent not just one wheel, but
dozens of them).

I hate to mention a specific one, but Microsoft (shudder!)
has something called "wininet" which is an API that works
with the DLLs that provide client/server Internet functions
to Windows Internet applications; there might be others available
for other systems if you look for them.

It really does look like you have a very specific system issue
that requires that URLs be encoded (because of the "reserved"
nature of certain URL characters), and if you find a suitable API,
you can just call a function to encode/decode the URLs as needed.

For some specific background info on the topic, you can
go to the original RFC 2396 by Berners-Lee...

---
William Ernest Reid

Nov 5 '08 #8
On Tue, 04 Nov 2008 18:31:35 +0000, Antoninus Twink wrote:
>On 4 Nov 2008 at 17:02, Eric Sosman wrote:
char url[] = "/url=%2Fhome%2Fs umit%2Fpackages %2Fmyxml.xml";
char *p;
while ((p = strstr(url, "%2F")) != NULL) {
*p = '/';
memmove (p+1, p+3, strlen(p+2));
}
...SNIP...
Do you get a kick out of mocking new posters by trying to lead them down
the garden path with a deliberately silly solution Eric?

I don't see anything silly in it, except if:

1) it is totally inefficient.
2) it does not handle the last %2F. I mean if %2F is at end of the url,
then will memove go beyond the array ?

--
www.lispmachine.wordpress.com
my email is @ the above blog.
Google Groups is UnBlocked now :)
Nov 5 '08 #9
On Tue, 04 Nov 2008 23:50:56 +0000, Ben Bacarisse wrote:
....SNIP...
I now feel I must show an tell so I am not just being negative. I'd
write it like this:

char *decode_hex(cha r *dest, const char *src)
{
char c, *dp = dest;
while ((c = *src++) != '\0') {
int nc;
unsigned int uc;
if (c == '%' && sscanf(src, "%2x%n", &uc, &nc) == 1 && nc == 2) {
src += nc;
*dp++ = uc;
}
else *dp++ = c;
}
*dp = '\0';
return dest;
}
The return of the string is just for convenience in larger expressions
and separating the source from the destination (which can, of course,
be the same) allows the function to be used on unmodifiable strings.


How about this ?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int slash_converter ( const char* );
int main( void )
{
char url[] = "%2F%2Fhttp::%2 F%2F%2F%2Fwww.g oogle.com%2F";

if( slash_converter (url) )
{
fprintf(stderr, "Can not allocate memory\n");
exit(EXIT_FAILU RE);
}

return 0;
}
int slash_converter ( const char* pc )
{
const int SUCCESS = 0;
const int FAILURE = 1;

char REPLACE_WORD[] = "%2F";
const int REPLACE_WORD_LE NGTH = strlen(REPLACE_ WORD);
int INSIDE_MOD = 0;

char* new_arr = NULL;
const long arr_size = strlen(pc) + 1;

new_arr = malloc( arr_size + sizeof(*new_arr ));
if( !new_arr )
{
return FAILURE;
}
while( *pc )
{
if( strncmp(pc, REPLACE_WORD, REPLACE_WORD_LE NGTH) == 0 )
{
INSIDE_MOD = 1;
}

if( INSIDE_MOD )
{
*new_arr++ = '/';
pc += REPLACE_WORD_LE NGTH ;
INSIDE_MOD = 0;
}
else
{
*new_arr++ = *pc++;
}
}
return SUCCESS;
}

--
www.lispmachine.wordpress.com
my email is @ the above blog.
Google Groups is UnBlocked now :)
Nov 5 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
62125
by: higabe | last post by:
Three questions 1) I have a string function that works perfectly but according to W3C.org web site is syntactically flawed because it contains the characters </ in sequence. So how am I supposed to write this function? String.replace(/</g,'&lt;');
13
11033
by: M | last post by:
Hi, I've searched through the previous posts and there seems to be a few examples of search and replacing all occurrances of a string with another string. I would have thought that the code below would work... string gsub(const string & sData, const string & sFrom,
22
11132
by: Phlip | last post by:
C++ers: Here's an open ended STL question. What's the smarmiest most templated way to use <string>, <algorithms> etc. to turn this: " able search baker search charlie " into this: " able replace baker replace charlie "
9
2160
by: Crirus | last post by:
dim pp as string pp="{X=356, Y=256}{X=356, Y=311.2285}{X=311.2285, Y=356}{X=256, Y=356}{X=200.7715, Y=356}{X=156, Y=311.2285}{X=156, Y=256}{X=156, Y=200.7715}{X=200.7715, Y=156}{X=256, Y=156}{X=311.2285, Y=156}{X=356, Y=200.7715}{X=356, Y=256}{X=200, Y=150}{X=200, Y=177.6142}{X=177.6142, Y=200}{X=150, Y=200}{X=122.3858, Y=200}{X=100, Y=177.6142}{X=100, Y=150}{X=100, Y=122.3858}{X=122.3858, Y=100}{X=150, Y=100}{X=177.6142, Y=100}{X=200,...
9
9163
by: Peter Row | last post by:
Hi, I know this has been asked before, but reading the threads it is still not entirely clear. Deciding which .Replace( ) to use when. Typically if I create a string in a loop I always use a StringBuilder. At present I am porting a VB6 webclass app to VB.NET and therefore I am trying to make it as efficent as possible via the new functionality of VB.NET.
4
3859
by: Cor | last post by:
Hi Newsgroup, I have given an answer in this newsgroup about a "Replace". There came an answer on that I did not understand, so I have done some tests. I got the idea that someone said, that the split method and the regex.replace method was better than the string.replace method and replace function. I did not believe that.
5
2447
by: djc | last post by:
I need to prepare a large text database field to display in an asp.net repeater control. Currently I am replacing all chr(13)'s with a "<br/>" and it works fine. However, now I also want to be able to replace TAB's with "&nbsp;"'s to preserve the user's indentation. My questions are: 1) even though I'll probably look it up before I get a reply.... whats the vb code for TAB character? 2) more importantly; do I have to run this large field...
10
9614
by: pamelafluente | last post by:
I need to replace all the occurences of a string within another string (or stringbuilder): Function ReplaceInsensitive(ByVal InputString As String, _ ByVal SubstringReplaced As String, _ ByVal Replacement As String) As String '... End Function
5
9317
by: V S Rawat | last post by:
I was trying to use back-to-back replace functions to convert a url: str1 = str.replace("%2F","/").replace("%3F","?").replace("%3D","=").replace("%2 6","&"); It didn't replace all 4 types of strings. Then, I googled and found this suggestion of some JavaScript Tutorials, so I used replace with a regex with a global switch:
1
3414
by: NvrBst | last post by:
I want to use the .replace() method with the regular expression /^ %VAR % =,($|&)/. The following DOESN'T replace the "^default.aspx=,($|&)" regular expression with "": --------------------------------- myStringVar = myStringVar.replace("^" + iName + "=,($|&)", ""); --------------------------------- The following DOES replace it though: --------------------------------- var match = myStringVar.match("^" + iName + "=,($|&)");
0
9852
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10902
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10583
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10288
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9420
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7824
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7014
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
4062
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3128
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.