Solved

Easy question - escaping double quotes

Posted on 2000-02-29
16
249 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
[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
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
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…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

729 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