Link to home
Start Free TrialLog in
Avatar of rutledgj
rutledgj

asked on

handling escape character in a string?

I want to do something like this:

#include <stdio.h>
#include <string.h>

struct Ent{
      
      char * name;
      char *value;
      }entries[5];

      void main(void)
      {
 
   int k;
   int Index = 0;
   int j;
int i;
char * CookieList = "Name1=value\" ";
entries[Index].name = CookieList;

i = strlen(CookieList);
k = 0;
while(k<i)
      {
      char testChar = CookieList[k];

      switch(testChar)
            {
            case '\\':    if(CookieList[++k] == '"'){

                          }
                       break;

            case '=':     CookieList[k] = '\0';
                        entries[Index].value = &(CookieList[++k]);
                        Index++;
                        if(CookieList[k] == ';')
                        entries[--Index].name = &CookieList[++k];
                        else if(CookieList[k] == '"')   {
                        char *temp = &CookieList[k];
                          CookieList[k] = '\\';
                          CookieList[++k] = *temp;
                          }

                        break;

            case ';':     CookieList[k] = '\0';
                        entries[Index].name = &CookieList[++k];
                        break;

             default:   k++;

            }



      }

      for(j = 0; j < 2; j++){
      printf("entry1 %i name = %s\n",j, entries[j].name);
      printf("entry %i value = %s\n",j, entries[j].value);
      }
      }


I want entries[0].name to have "Name1"
and entries[0].value to have "value\" "
C sees the "\"" as " " ".
Is there a way to make this work?
Avatar of seedy
seedy

Yes,  When you have "value\"" this means to <escape> only the
double quote.  What you actually want is the back-slash character
AND  the double-quote character in the string.  So do,
    char * CookieList = "Name1=value\\\" ";

Here \\ means escaping a back-slash and \" means escaping a
double quote character.

BTW, my compiler (gcc 2.7.2.2 in HP-UX 10.10) did not
like modifying the string CookieList like,
    CookieList[k] = '\0';

'Cause you have defined CookieList as
     char *CookieList = "somestring";
This makes the compiler place the string "somestring" in a
constant string table and place only the pointer in a variable
CookieList.  So when the string is modified, it generates a
'Bus error(coredump)'

This may not happen with your compiler/OS, but you might just
want to change the code.
Avatar of rutledgj

ASKER

I realize that I need to insert another slash "\". I think I included the wrong code in the question. Please look at this:

#include <stdio.h>
#include <string.h>

struct Ent{
      
      char * name;
      char *value;
      }entries[5];

      void main(void)
      {
 
   int k;
   int Index = 0;
   int j;
int i;
char * CookieList = "Name1=\" ";
entries[Index].name = CookieList;

i = strlen(CookieList);
k = 0;
while(k<i)
      {
      char testChar = CookieList[k];

      switch(testChar)
            {

       //if CookieList = '\"' insert another '\\'

            case '\\':    if(CookieList[++k] == '"'){
                                   char *temp = &CookieList[k];
                                CookieList[k] = '\\';
                                CookieList[++k] = *temp;
                               }
                       break;

            case '=':   CookieList[k] = '\0';
                        entries[Index].value = &(CookieList[++k]);
                        Index++;
                        if(CookieList[k] == ';')
                        entries[--Index].name = &CookieList[++k];
                        }
                        break;

            case ';':     CookieList[k] = '\0';
                        entries[Index].name = &CookieList[++k];
                        break;

             default:   k++;

            }



      }

      for(j = 0; j < 2; j++){
      printf("entry1 %i name = %s\n",j, entries[j].name);
      printf("entry %i value = %s\n",j, entries[j].value);
      }
      }



When visual C++ sees the '\"' it really only reads the ' " '. I can't change the input CookieList. That is why I am trying to read first the '\' then check if the next character is a ' " '. if so I will insert another '\' in front of it.

Sorry for the confusion












char * CookieList = "Name1=\" ";

will set Cookielist to the characters...

'N' 'a' 'm' 'e' '1' '=' '"' ' ' 0

There is no slash character in the string.

If you want one, then you need

char * CookieList = "Name1=\\\" ";

this will set Cookielist to the characters...

'N' 'a' 'm' 'e' '1' '=' '\' '"' ' ' 0

Also, if you are going to modify CookieList, then don't declare it as a char* pointer to some fixed string.

Instead

char CookieList[] = "Name1=\" ";

This will allocate an array (buffer) of exactly the required number of characters.  You can change values in such a buffer.

Alternatively, if the string can grow, explicitly give it a bigger size.  eg.

char CookieList[100] = "Name1=\" ";

This allocates a 100 characters for CookieList
Another problem is that your code..
   //if CookieList = '\"' insert another '\\'
  case '\\':
    if(CookieList[++k] == '"'){
      char *temp = &CookieList[k];
      CookieList[k] = '\\';
      CookieList[++k] = *temp;
    }
    break;

doesn't actually insert anything.  You're just rearranging the characters that are already there, ther eis no code here that would insert a character.

Assuming that CookieList[k] is a '\' and CookieList[k+1] is a '"'
then the code would do the following

    char *temp = &CookieList[k];
temp now points to a (sub)string starting from the '\' character
    CookieList[k] = '\\';
this does nothinh, as CookieList[k] was laready a slosh
    CookieList[++k] = *temp;
this replaces the '"' with a '\'.

This doesn't sound like what you want to do :-)

Soory guys, the whole point is that this is an input and CookieList cannot be modified except in the switch statement.
if case'\\' could see the slash in ' \" ' I would insert another slash in front of the quote. this is what temp section does. However, the compiler sees ' \" ' as ' " '.   THIS IS WHAT I NEED TO GET AROUND! HOW DO I MAKE THE COMPILER SEE '\' INSTEAD OF A QUOTE?
Adjusted points to 150
Rutledgj,
Sorry, I still do not understand you.  May be you can help
us by providing sample input(CookieList) and how you want
that to be translated?  Or explain in simple English what
need to be done.
Example:  Change every occurance of <BACK-SLASH><DOUBLE-QUOTE>
          sequence to <BACK-SLASH><BACK-SLASH><DOUBLE-QUOTE>
          in the string.

PS:  You make the compiler see '\' by escaping it - as in '\\'.



The case '\\': segment in your code converts
sequence like 'a' 'b' 'c' '\' '"' 'd' 'e' \0
to            'a' 'b' 'c' '\' '\' '\' 'e' \0
Rutledgj,
May be this is what you want to do in you code
-- segment start --
case '\\':       if (CookieList[++k] == '"') {
                  int kk;
                  kk = strlen(&CookieList[k]);
                  for ( ; kk >= 0 ; kk--) {
                        CookieList[k+kk+1] = CookieList[k+kk];
                  }
                  CookieList[k] = '\\';
            }
            k += 2; i++;
            break;
-- segment end --
Cheers.
ASKER CERTIFIED SOLUTION
Avatar of cph
cph

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
i give

What?  Did that work for you?!
I don't see why you accepted that.

I am NOT happy :-(

"the whole point is that this is an input and CookieList cannot be modified except in the switch statement."

That is not what you put in your question.  If it is input then there is no problem.  But that is NOT what your question stated

"if case'\\' could see the slash in ' \" '"

It DOES see the slash

"I would insert another slash in front of the quote. this is what temp section does."

Read my comments .. it DOES NOT DO THIS

":However, the compiler sees ' \" ' as ' " '.   THIS IS WHAT I NEED TO GET AROUND! HOW DO I MAKE THE COMPILER SEE '\' INSTEAD OF A QUOTE?"

You don't seem to be making any sense here .. Read my comments (and those of the others).

And why did you accept an answer that was no where near correct or what you wanted and rejeted mine ???????

Perhaps if you post your question agian and give us CORRECT details for your question and what you want to achieve, then you may get an answer that will work.  In fact, I'd be more that glad to answer it.