Solved

Easy question - escaping double quotes

Posted on 2000-02-29
16
243 Views
Last Modified: 2008-03-17
I would like to know how to escape a double quote containted in a string.

If the string is "Did you say "Hello"?"

I would like it to read:

"Did you say \"Hello\"?"

Thanks
0
Comment
Question by:mdlittle
16 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 2570629
Yes, that's correct.
0
 
LVL 86

Expert Comment

by:jkr
ID: 2570672
Indeed - this is absolutely correct...
0
 

Author Comment

by:mdlittle
ID: 2570800
I think you both missed my question. How would I do this in code? What functions would I use?
0
 
LVL 1

Expert Comment

by:Michael_OReilly
ID: 2570835
char text[100];

scanf("%s", text);

will read in text and escape whatever is necessary .. it isnt really escaping though ... just saying put the literal char in the array

escaping is only necessary when creating a string in the c code ... as in

char text[] = "Did u say \"hello\"?";

printf("%s", text);

gives

Did u say "hello"?
0
 

Author Comment

by:mdlittle
ID: 2570845
I need to take a string that has double quotes and change each double quote with \".

How would I do that in code?


The string has double quotes already and I want to change the string so that each double quoute is change from " to \".

Mike
0
 
LVL 16

Expert Comment

by:imladris
ID: 2570853
Or are you trying to get a string to contain:  "Did you say \"Hello\"?"

so that you can use that for the format argument for a printf? In that case you could initialize it as:

char *fmts = "Did you say \\\"Hello\\\"?";

0
 

Author Comment

by:mdlittle
ID: 2570899
Adjusted points to 200
0
 

Author Comment

by:mdlittle
ID: 2570900
Yes I want the string to have the \ before the double quote but I don't have control over the original string. I get it from a file.

Assume that I have a string that contains double quotes. The string is in a variable. I want to escape the double quotes.

As I stated above I need to change the double quotes to \".

I did not think this would be that difficult. Maybe I have not done a very good job explaining what I need.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 84

Expert Comment

by:ozo
ID: 2570908
char text[100];
char *p,*q;
for( p="Did you say \"Hello\"?",q=text; *q++=*p++; ){
     if( *p=='"' ){ *q++='\\'; }
}
0
 
LVL 84

Accepted Solution

by:
ozo earned 200 total points
ID: 2570929
/* sorry. that doesn't work if the first character is " */
for( p="\"Did you say \"Hello\"?",q=text; *q=*p; *q++=*p++){
     if( *p=='"' ){ *q++='\\'; }
}
0
 

Author Comment

by:mdlittle
ID: 2570932
I suppose I would just replace the "\"Did you say \"Hello\"?" with a variable. Correct?

Like:
for( p=buffer,q=text; *q=*p; *q++=*p++){ if( *p=='"' ){ *q++='\\'; }

The formatted string is now in q?
0
 

Author Comment

by:mdlittle
ID: 2570937
I suppose I would just replace the "\"Did you say \"Hello\"?" with a variable. Correct?

Like:
for( p=buffer,q=text; *q=*p; *q++=*p++){ if( *p=='"' ){ *q++='\\'; }

The formatted string is now in q?
0
 

Author Comment

by:mdlittle
ID: 2570956
I suppose I would just replace the "\"Did you say \"Hello\"?" with a variable. Correct?

Like:
for( p=buffer,q=text; *q=*p; *q++=*p++){ if( *p=='"' ){ *q++='\\'; }

The formatted string is now in q?
0
 
LVL 32

Expert Comment

by:jhance
ID: 2571105
I'm not sure I understand WHY you are doing this.  It's not necessary to escape quotes except when they appear inside a quoted string in the SOURCE CODE of the program.  If the strings are being read in from a file or are alreay in a variable, then it is not necessary to go through this process...

Of course, your question is very ambiguous....
0
 

Author Comment

by:mdlittle
ID: 2571166
SQL
0
 

Author Comment

by:mdlittle
ID: 2571168
Thanks for not wasting my time and answering my question so quickly. Your code works very nicely.

Mike
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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 structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

758 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now