2014-10-06 6 views
0

백 슬래시 문자 문자열을 백 슬래시 문자 자체로 쉽게 변환 할 수 있습니까? , 내가 알고하지 않는 것이백 슬래시 문자 문자열을 백 슬래시 문자로 변환

char somefunction(std::string code) 
{ 
    if (code.length() != 2) 
     return /* your error code here */ 

    if (code[0] != '\\') 
     return /* your error code here */ 

    switch(code[1]) { 
     case 'n' : return '\n'; 
     case 't' : return '\t'; 
     case 'r' : return '\r'; 
    } 
    return /* your error code here */ 
} 
+1

을 : –

+0

''\\ n "'이외의 다른 문자열을 얻으면'somefunction'은 무엇을 반환해야합니까? –

답변

-1
for (auto it = newline.begin(); it != newline.end(); ++it) { 
    if ((*it) == '\\') 
    newline.erase(it); 
} 
+0

지나치게 단순하지만, 이스케이프 문자 자체에 대한 이스케이프 만 처리하고 있습니다. – Deduplicator

+1

그 코드는 "\ n"을 "\ n"대신 "n"으로 바꿉니다 – user3233447

+2

무효화 된 반복기를 사용하고 있습니다. – chris

1

string newline = "\\n"; 
char n = somefunction(newline); // n == '\n' 
cout << newline << n << "a new line"; 


/*output: 
\n 
a new line 
*/ 
이 문자열은 변환 할 코드가 있다고 가정 : 예를 들어

어떻게 이런 일을 수행 할 것 그러나 직접 작성하는 것은 그리 까다로운 일이 아닙니다.

+0

@Deduplicator thanks .. 편집 됨 – Amadeus