Solved

Replace single doublequotes with double doublequotes in a string

Posted on 2002-06-27
12
241 Views
Last Modified: 2010-04-01
This is the input string and the desired output string. The string can change.
Input 1:  This "is" a "quoted" word.
Output1:  This ""is"" a ""quoted"" word.

Input 2:  This is a "quoted" word.
Output2:  This is a ""quoted"" word.
-------------------------------------------------
char *strIn = This "is" a "quoted" word.
char *strOut

I have a string like strIn in memory with 1 double quote across the start and end of the word in the entire string. I want a mechnism to have pointer strOut which will manipulate the string in such a way that it will put(or replace single double quote) with 2 consecutive double quotes as shown in the output 1, 2.

In simple words it will check the sentence and if a single double quote is found across the word it should insert a one more double quote with the previous double quote.

Note: I don't want to use string function, malloc but only pointers and strtok can be used.

-----------------------------------------------
The following is the hint function which i am using to do the opposite of what i want to do now.
i.e. it takes the double double quoted word in the string and convert it to single double doublequoted string and i want the opposite of it now.

void
DoubleQuoteString(char* result, char* src)
{
char *tmp;
char *doubleQuote;
bool doubleQuoteFound;

tmp = src;
for(doubleQuote = ++tmp ; ; ++doubleQuote) {
     if(*doubleQuote == '"') {
          if(*(doubleQuote+1) == '"') {
               doubleQuoteFound = true;
               break;
          }
          else {
               doubleQuoteFound = false;
               break;
          }
     }
}
if(doubleQuoteFound) {
   for(char* psrc = src ; *psrc ; ++psrc) {
     if(*psrc == '"')
        ++psrc;
      *result++ = *psrc;
     }
   }
else {
     strcpy(result, src);
     }
}


0
Comment
Question by:manbirpahwa
[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
12 Comments
 

Author Comment

by:manbirpahwa
ID: 7112932
once i get the reasonable comment i will increase the points.
0
 
LVL 23

Accepted Solution

by:
Roshan Davis earned 100 total points
ID: 7113053
Put '\"' instead of '"'

That is the Valid Double Quote Character

Regards

Roshmon
0
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 7113079
void DoubleQuoteString(char* result, char* src)
{
     char *tmp;
     char *doubleQuote;
     bool doubleQuoteFound;

     bool bContinue = true;
     char *pchSrc = src;
     char *pchDest = result;

     while ( *pchSrc )
     {
          if ( '\"' == *pchSrc )
          {
               *pchDest = '\"';
               pchDest++;
          }

          *pchDest = *pchSrc;
          pchDest++;
          pchSrc++;
     }

     *pchDest = 0;
}
0
Independent Software Vendors: 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 3

Expert Comment

by:CoolBreeze
ID: 7113082
what about when the string already contain "" do we change it to """" or remain as it is?
0
 
LVL 2

Expert Comment

by:jonnin
ID: 7113249
If a string already has it, its the " char (\") so its not a problem. It will display fine...

and """ does not ever work, the compiler matches the first and second and the third is extra, an error.  You must use the \" or the ascii(or unicode) value...

others in this problem area include
\n newline
\t tab
\\  '\'

look up escape charachters or escape sequence in your compilers help or a c++ book.






0
 

Author Comment

by:manbirpahwa
ID: 7113251
what about when the string already contain "" do we change it to """" or remain as it is?

In this case it should make it """".
0
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 7113265
Assigning to a character pointer is like this

char *strIn = "This \"is\" a \"quoted\" word";

Double DoubleQuote look like this in code

char *strIn = "This \"\"is\"\" a \"\"quoted\"\" word";

But u r prinf ing the strings

You can see the first as
This "is" a "quoted" word

and the Second:

This ""is"" a ""quoted"" word

So inputting through a terminal, no need to specify the slash, but assigning u should.....

Try my function it will work...

void DoubleQuoteString(char* result, char* src)
{
    char *pchSrc = src;
    char *pchDest = result;

    while ( *pchSrc )
    {
         if ( '\"' == *pchSrc )
         {
              *pchDest = '\"';
              pchDest++;
         }

         *pchDest = *pchSrc;
         pchDest++;
         pchSrc++;
    }

    *pchDest = 0;
}


GOOD LUCK
0
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 7113335
Hi manbirpahwa,
    Have you checked my comment ?
regards
Roshmon
0
 

Author Comment

by:manbirpahwa
ID: 7118217
Roshmon ur program does do the job but it fails when picking string sentences from multiple lines of a text file. Now the file is a space delimited format like

12 "This ""is"" a ""quoted"" string". 10 "file1"
11 "c:\folder2" 5 "file2"

first field is integer, next is a string sentence of variable length within double quotes(haveing double doublequoted) 3rd is again integer and last is again string.

Now, my code is loading a dynamic string of the format
This "is" a "quoted" string
in the memory my job is to write it back in the format as what appears in the file.

I have recoded the best alternative among the solutions that I got for
my question. I wanted to add it furthur that my program is picking
string sentences from mutiple lines in a text file with other fields.
It is space delimited. So all the other codes fail of doing so. Now this code
is working but fails for allocating the size of result buffer.

The result buffer must be large enough to hold the result string. The result
string will be at least at strlen(src) + 2 (for the start and end double
quotes), and can be larger if there are double quotes which must be converted into 2
double quote escapes.

char *
DoubleQuotedStringWrite(char* result, char* src)
{
   char* dest = result;
   *dest++ = '\"'; // write the starting double quote
 /* strcpy() the src into the dest, expanding any double  
    quotes into 2 double quote escape sequences.*/
   
     for(char* psrc = src ; *psrc ; ++psrc) {
        *dest++ = *psrc;
        if(*psrc == '"') {
          // write another " to make it a 2 double quote
           escape sequence
         *dest++ = *psrc;
        }
     }
     *dest = '\"'; // write the ending double quote
     return(result);
}
0
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 7118223
But my comment satify your first Question.
I thought, u need only one line...and you didn't specified any file there...

regards
Roshmon
0
 
LVL 11

Expert Comment

by:griessh
ID: 7262524
Dear manbirpahwa

I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. You can always request to keep this question open. But remember, experts can only help you if you provide feedback to their questions.
Unless there is objection or further activity,  I will suggest to accept

     "roshmon"

comment(s) as an answer.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0
 
LVL 6

Expert Comment

by:Mindphaser
ID: 7332981
Force accepted

** Mindphaser - Community Support Moderator **
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

710 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