Re: Escape character treatment in string library functions



rejithomas.d@xxxxxxxxx wrote:
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.

It isn't accurate to say that when you are doing things character
by character that C is treating "\r" as a single character. There
are two reasons why this isn't accurate:

(1) When you are doing the character by character processing,
there is no C. There is only a binary. The binary was
produced by a C compiler, but that is immaterial at that
point.
(2) When you are doing the character by character processing,
there is no sequence of characters "\" followed by "r".
There is only a carriage return, which is a single character.
Nothing is treating anything as a single character. There
is no special processing going on. There just is 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 you don't want C to produce a string constant with a
carriage return in it, then don't tell the C compiler to
produce a string constant with a carriage return in it.
If you want a string constant with a "\" character followed
by a "r", then write this: "\\r". Then the "\\" will be
translated into a "\" when the source code is turned into
a string constant.

- Logan
.



Relevant Pages

  • Re: finding strings in a text file help
    ... digits and reserved words and then prints them out in order ... > it gets the whole string matches it against the reserved words array ... one character of a potential word in your "s" string. ... a char[] array would do. ...
    (comp.lang.java.help)
  • Re: detecting characters on RS232-Interface
    ... read data into string variable ... > splitted at the end of the receive buffer). ... examine the next char in turn. ... When a character ...
    (microsoft.public.vb.general.discussion)
  • Re: remove spaces from a string and Complexity
    ... string character by character and copying onto another output string. ... void delchar(char *s, char c) ... I've seen functions written as above, however I'm still a little confused about one point - C passes by value therefore with your above function wouldn't the following behave incorrectly (incorrectly as in not modify the contents referenced by the first parameter but instead modify a copy of it): ...
    (comp.lang.c)
  • Re: "Read stuff from a file and chop it up to do stuff" code advice wanted.
    ... ;; This function returns TRUE if any character ... (if (char< char #\!) ... a stream and an array to hold characters in temp memory. ... ;; resulting string. ...
    (comp.lang.lisp)
  • RfD: Escaped Strings (Version 6)
    ... Escape character is case sensitive, ... the S" string can only contain printable characters, ... \f FF (form feed, ASCII 12) ... impact of char in the file word sets. ...
    (comp.lang.forth)