Solved

Easy question - escaping double quotes

Posted on 2000-02-29
16
244 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files 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.

929 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

11 Experts available now in Live!

Get 1:1 Help Now