Solved

handling escape character in a string?

Posted on 1998-04-27
16
190 Views
Last Modified: 2010-04-15
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?
0
Comment
Question by:rutledgj
  • 6
  • 5
  • 4
  • +1
16 Comments
 
LVL 2

Expert Comment

by:seedy
ID: 1256241
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
 
LVL 2

Expert Comment

by:seedy
ID: 1256242
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
 

Author Comment

by:rutledgj
ID: 1256243
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
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1256244
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
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1256245
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
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1256246
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
 

Author Comment

by:rutledgj
ID: 1256247
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
 

Author Comment

by:rutledgj
ID: 1256248
Adjusted points to 150
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 2

Expert Comment

by:seedy
ID: 1256249
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
 
LVL 2

Expert Comment

by:seedy
ID: 1256250
The case '\\': segment in your code converts
sequence like 'a' 'b' 'c' '\' '"' 'd' 'e' \0
to            'a' 'b' 'c' '\' '\' '\' 'e' \0
0
 
LVL 2

Expert Comment

by:seedy
ID: 1256251
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
 
LVL 1

Accepted Solution

by:
cph earned 150 total points
ID: 1256252
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
 

Author Comment

by:rutledgj
ID: 1256253
i give

0
 
LVL 2

Expert Comment

by:seedy
ID: 1256254
What?  Did that work for you?!
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1256255
I don't see why you accepted that.

I am NOT happy :-(

0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1256256
"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

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

757 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

20 Experts available now in Live!

Get 1:1 Help Now