Solved

Easy question - escaping double quotes

Posted on 2000-02-29
16
247 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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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 how to use strings and some functions related to them 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.

820 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