• C

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?
rutledgjAsked:
Who is Participating?
 
cphConnect With a Mentor Commented:
Hi,

What you want to do is insert a QUOTE (") after a BACKSLASH (\)?
If so, it goes like this:

  case '\\':    if(CookieList[++k] == ''\"'){ //if current character is a (\) (that's the case)
 //if the next one is a QUOTE (") then do your stuff

      }
0
 
seedyCommented:
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.

0
 
seedyCommented:
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.
0
The Firewall Audit Checklist

Preparing for a firewall audit today is almost impossible.
AlgoSec, together with some of the largest global organizations and auditors, has created a checklist to follow when preparing for your firewall audit. Simplify risk mitigation while staying compliant all of the time!

 
rutledgjAuthor Commented:
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












0
 
RONSLOWCommented:
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

0
 
RONSLOWCommented:
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
0
 
RONSLOWCommented:
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 :-)

0
 
rutledgjAuthor Commented:
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?
0
 
rutledgjAuthor Commented:
Adjusted points to 150
0
 
seedyCommented:
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 '\\'.



0
 
seedyCommented:
The case '\\': segment in your code converts
sequence like 'a' 'b' 'c' '\' '"' 'd' 'e' \0
to            'a' 'b' 'c' '\' '\' '\' 'e' \0
0
 
seedyCommented:
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.
0
 
rutledgjAuthor Commented:
i give

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

I am NOT happy :-(

0
 
RONSLOWCommented:
"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.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.