Solved

Easy question - escaping double quotes

Posted on 2000-02-29
16
246 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

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…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

810 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