I would like to capture special characters such as \n from the command line into a C program.
For example, for the following program, if I run ./a.out “\nfoo\n” , I’d like to to print (newline) foo (newline) instead of “\nfoo\n”. How can I capture that in to a string?
#include <stdio.h>
#include <string.h>
int main(int argc, char ** argv){
if(argc >1){
char * s = strdup(argv[1]);
printf("%s\n", s);
free(s);
}
return 0;
}
Edit: sorry, by (newline) foo (newline), I mean the acutal output is
foo
Currently,the output is literally “\nabc\n”.(newlines are not printed because s captures “\n” 2 characters instead of the ‘\n’ character). Sorry about the confusion.
make a new string, iterate through the old string adding characters to the new string. if you ever see the
'\'character, add a special character to the new string based on the next character in the old string.