0

For a function I am making, I take a string in as a parameter and do things with it. However I treat characters in the string specially if there is a backslash before it. However I am having problems even seeing the blackslash!

std::string s = "01234\6";
std::cout << s << std::endl;
std::cout << s.at(5) << std::endl;
if(s.at(5)== '\\')
    std::cout << "It's a backslash" << std::endl;
else
    std::cout << "It's not a backslash" << std::endl;

outputs

01234

It's not a backslash

How am I supposed to check if mystring.at(i) == '\\' if it isn't showing up at all?

The input will be coming from another file (which I can't modify) like

myfunc("% \%  %");

If I read the string I count 3 '%' characters (so its not ignored by the backslash), and 0 '\' characters

edit: Code how I count

char percent = '%';
int current_index = 0;
int percent_count = 0;

int ret = str.find(percent, current_index);
while(ret != std::string::npos)
{
  percent_count++;
  current_index = ret +1;
  ret = str.find(percent, current_index);
 }

 return percent_count;
user2770808
  • 347
  • 3
  • 14
  • 2
    Is it not `s = "01234\\6";`? – Ian May 18 '16 at 03:15
  • 1
    `myfunc("% \% %");` has invalid conversion specifiera so the behavior is undefined. You get what you get. – Captain Obvlious May 18 '16 at 03:38
  • When hard-coding a string, the compiler gives special meaning to "\". And the hard-coded string will be translated into something else. Luckily (well by design), one of its special meanings is that "\\" will be treated as "\". So wherever you want a hard-coded string to contain "\", write "\\". **NOTE** That this applies to compilation only. When your program reads text from another source, it will read the text as it receives it. – Disillusioned May 18 '16 at 11:36
  • See https://stackoverflow.com/a/66912435/6327658 – fabda01 Sep 13 '22 at 08:32

4 Answers4

4

C++ supports three kinds of escape sequences:

  1. simple-escape-sequence. It is one of:

    \’ \" \? \\
    \a \b \f \n \r \t \v
    
  2. octal-escape-sequence. It is one of:

    \ octal-digit
    \ octal-digit octal-digit
    \ octal-digit octal-digit octal-digit
    

    \0 is the most well known octal escape sequence that represents the null character.

  3. hexadecimal-escape-sequence. It is one of:

    \x hexadecimal-digit
    hexadecimal-escape-sequence hexadecimal-digit
    

When you use:

std::string s = "01234\6";

the \6 part represents an octal escape sequence. It does not represent two characters.

It is the same as

std::string s = "01234?";

where ? is the character represented by the octal number 6.

In order to have \ as an element of the string, you'll need to use:

std::string s = "01234\\6";
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • 4
    It's worth noting that when the input comes from a file instead of a string literal (as mentioned in the question), no escaping is needed. Backslash will be read as a backslash. – ColBeseder May 18 '16 at 03:56
1

The checking method is right, but \ escape 6, so \6 is counted once, you can check sizeof("12345\6"), which 7, or strlen("12345\6"), which is 6.

Change "12345\6" to "12345\\6".

delta
  • 3,778
  • 15
  • 22
  • @user2770808 don't worry. You don't really *edit* the string in a sense, you let the compiler knows that what you actually mean is to read a backslash. – Ian May 18 '16 at 03:20
  • @user2770808 compiler knows backslash if you read from file, which is not the same as the literal string representation in c/c++. – delta May 18 '16 at 03:25
0

The C++ compiler would have already treated it specially if you have backslash in the string:

std::string s = "01234\6"; //\6 is treated differently already, as unicode character \6, not as backslash + 6

Unless what you mean is you want to have a text with backslash (say, from I/O). In that case, you should put \\ to make your compiler understand that you mean it as real backslash not a unicode character:

std::string s = "01234\\6"; //double backslash here

Then you can test your program.

Ian
  • 30,182
  • 19
  • 69
  • 107
0

No compiler C++ will interpret \ as a backslash, since its the escape character. You will have to use \\ to denote a backslash in a string.

Ani Menon
  • 27,209
  • 16
  • 105
  • 126
  • Not quite true. You might want to broaden your horizons to other languages. ;) Or at least qualify what compilers you're describing. – Disillusioned May 18 '16 at 11:41