Re: Escape character treatment in string library functions



rejithomas.d@xxxxxxxxx wrote:
On Jan 16, 8:03 pm, "Malcolm McLean" <regniz...@xxxxxxxxxxxxxx> wrote:
<rejithoma...@xxxxxxxxx> wrote in message

news:83c99c40-9b92-441a-ba95-f6536ed4759c@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>
Can I change the escape character used by string libraries?. My
requirement is to
parse a string in format "domain\username" and change it into
domain\ \username.
The C library functions takes \ as an escape character and also
treat '\r' etc special making it difficult to parse even char by
char.

Can anyone suggest any solution to this issue

char *doublebackslashes(char *in)
{
size_t count = 0;
size_t i = 0;
size_t j = 0;
char *answer = 0;

for(i=0;in[i];i++)
if(in[i] == '\\')
count++;
answer = malloc( strlen(in) + count + 1);
if(!answer)
return 0;
for(i=0;in[i];i++)
{
answer[j++] = in[i];
if(in[i] == '\\')
answer[j++] = '\\';
}
answer[j] = 0;

return answer;

}

int main(int argc, char **argv)
{
int i;

for(i=0;i<32;i++)
printf("%s\n", doublebackslashes("My\\Fred"));
return 0;

}

I've knocked up a little funcion for you.
I suspect that what you really need is a "make C escapes" however,
which is
a little more work.

--
Free games and programming
goodies.http://www.personal.leeds.ac.uk/~bgy1mm



Thanks everyone for the reply. But the issue I am facing is

I get a string literal in the format "domain\username" (with a single
\ and not \\ ,. say char *str= "dom\ret") .If I try to parse the
string char by char C treats '\r' as a single character.

For eg:

char *str="dom\ret";
while( *(str++) !='\0')
if(*str == '\\' )
printf("found");

is not working since it sees '\r' as a single char .

If the string is being read by an external source, and has the characters
'\' and 'r' next to each other, they will be interpreted as two different
characters, '\' and 'r'. If, however, "\r" is typed in the source code, it
will be treated as a single escaped character. The way to prevent this is
in your source code you escape the \ with "\\". I.E.

char* str = "dom\\ret";
will produce '\' and 'r' as 2 seperate characters. Reading the text from a
file with "dom\ret" in it will also produce 2 seperate characters. The only
real concern you should have is always escape your \ in your source code.

Now, there are some exceptions with external libraries. There are, for
example, RE libraries that would treat the string "dom\ret" as the r being
escaped. In which case you need to manually escape the \ in the string.

In your example given, this will work:

char* str="dom\\ret";
while( *(str++) !='\0')
if(*str == '\\' )
printf("found");

should produce the output
found

--
Jim Langston
tazmaster@xxxxxxxxxxxxxx


.



Relevant Pages

  • Re: RegEx and Vb.net /// "Unrecognized escape sequence"
    ... Not that \ is the escape character for regular expressions. ... VB.Net does not use the \ character as an escape character in strings. ... Dim fileName As String ...
    (microsoft.public.dotnet.framework)
  • Re: Trying to remove "" from a string
    ... double up the escape char. ... Bobby Ryzhy ... >I have tried adding 34 to the string instead of the escape character, ... >when i watch the string in the command window or the watch window it has the ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Escape character treatment in string library functions
    ... The C library functions takes \ as an escape character and also treat ... '\r' etc special making it difficult to parse even char by char. ... I get a string literal in the format "domain\username" (with a single ...
    (comp.programming)
  • Re: Trying to remove "" from a string
    ... Escape the escape char ... > I have tried adding 34 to the string instead of the escape ... but you cant replace a escape character, ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Cannot seem to save a string with double quotes
    ... string right? ... What is happening is that \ is the "escape character" meaning that the next ... "Steven Blair" wrote in message ...
    (microsoft.public.dotnet.languages.csharp)