473,326 Members | 2,337 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

replace function

I'm trying to replace the characters in a pointer from an url string.

Here is my code.

// string has embedded '+', this code will not work.
VOID PatchQuery(char *szQuery, char **ppszPatchedQuery)
{
char *p = szQuery;

while (*p++)
{
if (*p == '+') //replace '+' by ' '
*p = ' ';

}
strcpy(begin,p);
*ppszPatchedQuery =begin;
return;
}

I get an access violation when I try to replace the + with a space.
Can anyone tell me how to do this?

TIA
Nov 13 '05 #1
5 19108
Ema
"Hilary Cotter" <hi*****@att.net> ha scritto nel messaggio
news:3b**************************@posting.google.c om...
I'm trying to replace the characters in a pointer from an url string.

Here is my code.

// string has embedded '+', this code will not work.
VOID PatchQuery(char *szQuery, char **ppszPatchedQuery)
{
char *p = szQuery;

while (*p++)
{
if (*p == '+') //replace '+' by ' '
*p = ' ';

}
strcpy(begin,p);
*ppszPatchedQuery =begin;
return;
}

I get an access violation when I try to replace the + with a space.
Can anyone tell me how to do this?

where is declared "begin"?

Bye,
Ema
Nov 13 '05 #2
On 3 Sep 2003 05:15:11 -0700, hi*****@att.net (Hilary Cotter) wrote:
I'm trying to replace the characters in a pointer from an url string.

Here is my code.

// string has embedded '+', this code will not work.
VOID PatchQuery(char *szQuery, char **ppszPatchedQuery)
{
char *p = szQuery;

while (*p++)
{
if (*p == '+') //replace '+' by ' '
*p = ' ';

}
strcpy(begin,p);
p is no longer pointing at a valid object, ITYW:

strcpy(begin,szQuery);
*ppszPatchedQuery =begin;
return;
}
Also why modify the original string then make a copy of it? It
would be more logical to swap the order so that the original is
not destroyed.
I get an access violation when I try to replace the + with a space.
Can anyone tell me how to do this?


Nick.

Nov 13 '05 #3
On 3 Sep 2003 05:15:11 -0700
hi*****@att.net (Hilary Cotter) wrote:
I'm trying to replace the characters in a pointer from an url string.

Here is my code.

// string has embedded '+', this code will not work.
Invalid syntax in C89 (hint: C++ style comments)
VOID PatchQuery(char *szQuery, char **ppszPatchedQuery)
What's VOID?
{
char *p = szQuery;

while (*p++)
{
if (*p == '+') //replace '+' by ' '
*p = ' ';
might attempt to overwrite read-only strings!

}
strcpy(begin,p);
*ppszPatchedQuery =begin;
You make a copy too late... copy the string BEFORE modifying it.
And what's 'begin'?
return;
}

I get an access violation when I try to replace the + with a space.
Can anyone tell me how to do this?


You're probably trying to write to something you're not supposed to write to
(read-only string or string literal). Are you calling it like this:

PatchQuery ("stringwith+es and +es", foo);

If so, you're trying to overwrite a string literal, which is not allowed.
This may be a better function for you:

#include <string.h>
int patchquery (char *string, char **retstring)
{
char *sptr;
/* make a copy of the original string and modify it instead of the original */
*retstring = sptr = strdup (string);
if (!sptr)
return 0;
for (;*sptr;sptr++)
if (*sptr == '+')
*sptr = ' ';
/* retstring already holds the pointer to our (modified) copy */
return 1;
}

Do remember to free() the string returned in retstring.

--
char*x(c,k,s)char*k,*s;{if(!k)return*s-36?x(0,0,s+1):s;if(s)if(*s)c=10+(c?(x(
c,k,0),x(c,k+=*s-c,s+1),*k):(x(*s,k,s+1),0));else c=10;printf(&x(~0,0,k)[c-~-
c+"1"[~c<-c]],c);}main(){x(0,"^[kXc6]dn_eaoh$%c","-34*1'.+(,03#;+,)/'///*");}
Nov 13 '05 #4
Hi,

"Hilary Cotter" <hi*****@att.net> wrote in message
news:3b**************************@posting.google.c om...
| // string has embedded '+', this code will not work.
| VOID PatchQuery(char *szQuery, char **ppszPatchedQuery)

As Ema pointed out, the code you posed does not compile...
But from the function's signature and code snippet, I see
two things that can go wrong:

- The type of the char **ppszPatchedQuery suggests that
the PatchQuery function will use it to return a pointer
to a newly allocated memory buffer. Is this intended ?
This is not what the function currently does...
Suggestion: consider letting the caller allocate
the destination buffer...

- If your function is called by passing a string literal
as the first parameter, the function may trigger an
access violation, as it modifies the contents
of szQuery.
PatchQuery("a+b",&pbuf); // will crash
Suggestion: do not modify the source string. Only
modify the copy you create.

| I get an access violation when I try to replace the + with a space.
| Can anyone tell me how to do this?

I'm afraid this could be homework... but here's a way
to implement equivalent functionality:
void patch_query(char const* src, char* dst)
{
char c;
do {
c = *src++;
*dst++ = (c=='+') ? ' ' : c;
} while(c);
}
Cheers,
--
http://www.post1.com/~ivec
Nov 13 '05 #5
Hilary Cotter wrote:

I'm trying to replace the characters in a pointer from an url string.

Here is my code.

// string has embedded '+', this code will not work.
VOID PatchQuery(char *szQuery, char **ppszPatchedQuery)
{
char *p = szQuery;

while (*p++)
{
if (*p == '+') //replace '+' by ' '
*p = ' ';

}
strcpy(begin,p);
*ppszPatchedQuery =begin;
return;
}


Others have diagnosed various problems here, but one I
haven't seen mentioned yet is the fact that a '+' at the
very beginning of the string will not be replaced: you're
incrementing `p' too early.

--
Er*********@sun.com
Nov 13 '05 #6

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

Similar topics

12
by: Barnes | last post by:
Does anyone know of a good way to use the JavaScript string.replace() method in an ASP form? Here is the scenario: I have a form that cannot accept apostrophes. I want to use the replace() so...
6
by: Danny | last post by:
I need an asp command to strip out from a string all extra punctuation such as apostrophe, comma, period, spaces dashes, etc etc and just leave the letters. Can anybody give me some ideas? ...
4
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...
12
by: Brian | last post by:
I want to use regxp to check that a form input contains at least 1 non-space charcter. I'd like to only run this if the browser supports it. For DOM stuff, I'd use if (documentGetElementById) {}...
3
by: Dave | last post by:
I have an Access 2K application that is distributed to about a dozen users (all with identical NT environments and identical Access versions, object libraries and service packs). I am using the VBA...
5
by: pembed2003 | last post by:
Hi all, I need to write a function to search and replace part of a char* passed in to the function. I came up with the following: char* search_and_replace(char* source,char search,char*...
19
by: Paul | last post by:
hi, there, for example, char *mystr="##this is##a examp#le"; I want to replace all the "##" in mystr with "****". How can I do this? I checked all the string functions in C, but did not...
5
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...
4
by: Lauren Wilson | last post by:
Hi folks, We have a need to replace sub strings in certain message text. We use the Office Assistant to display help and often use the imbedded formatting commands. Those of you who have used...
10
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.