Solved

Replace single doublequotes with double doublequotes in a string

Posted on 2002-06-27
12
219 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

707 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

18 Experts available now in Live!

Get 1:1 Help Now