473,882 Members | 1,639 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 **ppszPatchedQu ery)
{
char *p = szQuery;

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

}
strcpy(begin,p) ;
*ppszPatchedQue ry =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 19131
Ema
"Hilary Cotter" <hi*****@att.ne t> ha scritto nel messaggio
news:3b******** *************** ***@posting.goo gle.com...
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 **ppszPatchedQu ery)
{
char *p = szQuery;

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

}
strcpy(begin,p) ;
*ppszPatchedQue ry =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 **ppszPatchedQu ery)
{
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,sz Query);
*ppszPatchedQue ry =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 **ppszPatchedQu ery)
What's VOID?
{
char *p = szQuery;

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

}
strcpy(begin,p) ;
*ppszPatchedQue ry =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)ch ar*k,*s;{if(!k) return*s-36?x(0,0,s+1):s ;if(s)if(*s)c=1 0+(c?(x(
c,k,0),x(c,k+=* s-c,s+1),*k):(x(* s,k,s+1),0));el se 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.ne t> wrote in message
news:3b******** *************** ***@posting.goo gle.com...
| // string has embedded '+', this code will not work.
| VOID PatchQuery(char *szQuery, char **ppszPatchedQu ery)

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 **ppszPatchedQu ery 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(cha r 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 **ppszPatchedQu ery)
{
char *p = szQuery;

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

}
strcpy(begin,p) ;
*ppszPatchedQue ry =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
8183
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 that the apostrophe is automatically replace with two '' . Reason being--SQL Server does not like apostrophes being sent to database. I've tried to do this on the server side in the SQL area of the ASP page by writing a function (with some great...
6
5682
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? Thanks
4
62126
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;');
12
4059
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) {} Is there an object/feature detection I can use to check for regxp string manipulation support? --
3
3791
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 Replace function in a couple of queries to reformat some data. The application works flawlessly on 8 of the workstations, but on the other 4 throws a "Unrecognized function Replace" error when the queries in question are encountered. I have...
5
2643
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* replace){ char* result; size_t l = strlen(source), r = strlen(replace), i; int number_of_replaces = 0; for(i = 0; i < l; i++){ if(source == search)
19
78864
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 find one.
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...
4
23016
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 them know they look like this: "{cf 5}" or "{cf 0}" or "{ul 1}" or "{ul 0}", etc. The commonality they have is that they are always 6 charters long and always begin and end with curly brackets. This all works great if the user is running the...
10
9615
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
0
9931
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
9777
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10829
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10403
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
9556
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
7955
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
7113
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();...
0
5781
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4599
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.