views:

547

answers:

3

The function prototype would be:

string f (string s);

or

char* f (char* s);

f would transform a string represented by printable ascii char into a raw string.

and it would behaves as in the following examples:

f("AAA") = "AAA"
f("AA\n") = "AA+line_feed"

i.e the input string is 4 char long (+ NULL), the output is 3 char long(+NULL). The 2 printable characters '\' 'n' (the 2 last characters of the input string) are replaced by one single LF character (ascii 10)

f("\0x007") returns a string of length 1 containing char ascii 7

f("AA\nAA\tAA\07\n") would convert all these escape character stuff to their real (1 char per escape sequence) equivalent.

This must exist as at least the C compiler/C preprocessor does it.

/C

A: 

Well, no. There is no entrypoint of the C standard library or STL that just does this. Yes, there is code in the implementations of cpp. It will take you 20 times as long to find it as to code it yourself.

bmargulies
+2  A: 

I think you have completely misunderstood how escape sequences work. For example "AA\n" is not "4 char long (+ NULL)" as you suggest, it is in fact exactly what you want - a three character string. The compiler provides the translation for you, it does not occur at runtime.

To see that this is true, try the following:

printf( "%d\n", strlen( "AA\n" ) ) ;

and you will observe that the output is 3.

Now if you has a string at runtime containing escape sequences; for example the literal string "AA\n", which is four characters, then there is no reason for the standard library to provide this functionality just because the compiler performs such translation as you seem to think. The compiler functionality is not available at run time.

Implementing such a translation yourself is trivial, for example you might simply scan the string on encountering a '\' you would use the next character as the control variable of a switch construct to select which character to insert into the output string. If you encounter \x or \0 you then need to read subsequent hex or octal digits to determin the character to insert.

Clifford
Thanks for replying...But we agree:I am aware that when you type "AA\n" into a C file, CPP converts it to a 3 char long string. BUTMy problem is that the program I am writting reads these strings from a file, as CPP does. So when I read "AAA\n" (5 chars) I want a function that converts \n into ascii 10 (or any other escape char)...I want to do what CPP does!
Christophe Milard
You should change the question title and content then. Your input string looks like `"AA\\n"`, and you want it to be converted to `"AA\n"`. Of course, it still feels like homework, so show us what you have done so far.
Alok
@Christophe: And my answer addresses that in the last two paragraphs. We may agree, but your question is ambiguous so I answered both interpretations.
Clifford
A: 

This is called "unquoting" or "unescaping". A C compiler will typically do this as part of its scan of the input text, so you are unlikely to find such a function in any C compiler source. Your best bet is to write a simple function to scan through the string, replacing the escapes as it goes, storing the characters in the output string.

ergosys