Re: Escape character treatment in string library functions



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 .


Thanks
Reji
.



Relevant Pages

  • Re: Gtk-2 File Chooser Modification Time Display
    ... There is a string default ... Well I experimented with setting the format string from %x to %c, ... GtkFileChooserDefault *impl; ... char buf; ...
    (comp.os.linux.misc)
  • String interpolation
    ... I'm trying to do some string interpolation -- that is, ... static char* expand(const char *format) ... strncat(string, format, pos); ...
    (comp.lang.c)
  • Re: How to add thousand separators
    ... First, this code is obsolete as written, because char is a dead data type and should not ... Note that both of these should be stored as string resources since they might need to be ... 18 digits for any reason. ... you have made a VERY SERIOUS DESIGN ERROR. ...
    (microsoft.public.vc.mfc)
  • Re: what is the best way of passing floats into a string
    ... I do not null-terminate as snprintf takes care of this (according to ... But the easiest way to determine the size needed to format a number, ... int length_of_representation(double n,const char* format){ ... I get a nice result of -10.000000 in my char * string. ...
    (comp.unix.programmer)
  • Re: weird problem
    ... I already told you that the comparison between an integer and a float ... to strcmpwhich expects a pointer to a string. ... And now a question about something else: why do you use floating ... int,float, char, etc. ...
    (comp.lang.c)