• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 264
  • Last Modified:

Replace single doublequotes with double doublequotes in a string

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
manbirpahwa
Asked:
manbirpahwa
1 Solution
 
manbirpahwaAuthor Commented:
once i get the reasonable comment i will increase the points.
0
 
Roshan DavisCommented:
Put '\"' instead of '"'

That is the Valid Double Quote Character

Regards

Roshmon
0
 
Roshan DavisCommented:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
CoolBreezeCommented:
what about when the string already contain "" do we change it to """" or remain as it is?
0
 
jonninCommented:
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
 
manbirpahwaAuthor Commented:
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
 
Roshan DavisCommented:
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
 
Roshan DavisCommented:
Hi manbirpahwa,
    Have you checked my comment ?
regards
Roshmon
0
 
manbirpahwaAuthor Commented:
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
 
Roshan DavisCommented:
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
 
griesshCommented:
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
 
MindphaserCommented:
Force accepted

** Mindphaser - Community Support Moderator **
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now