Skip to content

Instantly share code, notes, and snippets.

@Gaurav-Singh-97
Created December 2, 2016 17:55
Show Gist options
  • Save Gaurav-Singh-97/aca77d86bff7d74ea72c308a19c56651 to your computer and use it in GitHub Desktop.
Save Gaurav-Singh-97/aca77d86bff7d74ea72c308a19c56651 to your computer and use it in GitHub Desktop.
Convert string to corresponding escape seq character (Eg "\n" to '\n')
#include<iostream>
using namespace std;
#include<stdexcept>
#include<string.h>
char strToEscSeq(const char str[])
{
//short int ctr = 0;
if (str[0]=='\\') //it itself is an escape sequence, which means single backslash'\'
switch(str[1])
{
case 'a': return '\a';
case 'b': return '\b';
case 'n': return '\n';
case 'r': return '\r';
case 't': return '\t';
case 'v': return '\v';
default : throw runtime_error("Invalid string arguement");
}
else
throw runtime_error("Invalid string arguement");
}
int main(int argc, char * argv[])
{
char res;
char inputString[3] = "\\n";
/*
char inputString[2];
inputString[0] = '\\';
inputString[1] = 'n';
*/
//Both ways work
try
{
res = strToEscSeq(inputString);
}
catch(runtime_error err)
{
cout<<err.what();
return 1;
}
cout<<res<<' '<<(int)res<<"!!";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment