Advertisement

String editing???

Started by May 22, 2000 02:03 PM
2 comments, last by thekid 24 years, 7 months ago
I want to be able to scan a char array for the caracter: \ and change it to the character:/ When I try to do this I scan through the array and try to find the \ like this for(i=0;i==''\'') string = ''/''; } but it tells me i cannot have a newline in a constant. Does anyone know how to solve this </i>
In C/C++, you have to escape a backslash (ie. you have to double it up):
for(n=0; n< strlen(string); n++)  if(string[n] == '\\')      string[n] = '/';  


Edited by - jaxson on May 22, 2000 3:21:17 PM
Advertisement
I believe the problem is that "\" is the control character prefix for C/C++ (for instance, ''\n'' is a newline, ''\g'' is a beep). In order to get the literal character "\" try using ''\\'' instead of ''\''

-fel

~ The opinions stated by this individual are the opinions of this individual and not the opinions of her company, any organization she might be part of, her parrot, or anyone else. ~
Just trying to complete on the previous posts,
I''m not sure about this, but I think that in your case, what the compiler thinks you''re trying to do is to use the '' caracter you open the caracter token identifier '' , then you use the escape caracter \ then the next character is going to be the code or special character you want to use. since you used '' the compiler thinks that''s the character you''re specifiyng and of it goes in search of an enclosing '' wich he doesn''t find , hence the error.
so, as said before, if you want to find the \ character you have to use ''\\'', if you wanted to seach for the '' character you''d do something like this: ''\''''. (Same goes to non printable characters, like new line \n, return \r, tab \t) on a string, for instance, if you want to specify a " inside your string , you have to use the \ before, so the compiler knows you''re not trying to end the string there: " quote: \"I''m F****d\" "

This topic is closed to new replies.

Advertisement