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
Solved

Replace single doublequotes with double doublequotes in a string

Posted on 2002-06-27
12
233 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
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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

861 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