#include <iostream>
int main() {
std::string s = "?????";
std::cout << s << std::flush;
}
What should I write in the s variable to output \"\"' (these 5 characters?)
Escape them like this (prepend \ to each of the special characters):
"\\\"\\\"'"
The other option is C++11 raw string literal:
R"(\"\"')"
Since C++11, you can put whatever characters you like in a raw string literal:
R"(\"\"')"
Historically, you would have to escape \ and " with \ in a normal string literal:
"\\\"\\\"'"
You need to escape the \ and " characters with \
std::string s = "\\\"\\\"'";
In C++11, you can also use a raw string literal
R"(\"\"')"
Related
Take a look at the following example:
cout << "option 1:
\n option 2:
\n option 3";
I know,it's not the best way to output a string,but the question is why does this cause an error saying that a " character is missing?There is a single string that must go to stdout but it just consists of a lot of whitespace charcters.
What about this:
string x="
string_test";
One may interpret that string as: "\nxxxxxxxxxxxxstring_test" where x is a whitespace character.
Is it a convention?
That's called multiline string literal.
You need to escape the embedded newline. Otherwise, it will not compile:
std::cout << "Hello world \
and stackoverflow";
Note: Backslashes must be immediately before the line ends as they need to escape the newline in the source.
Also you can use the fun fact "Adjacent string literals are concatenated by the compiler" for your advantage by this:
std::cout << "Hello World"
"Stack overflow";
See this for raw string literals. In C++11, we have raw string literals. They are kind of like here-text.
Syntax:
prefix(optional) R"delimiter( raw_characters )delimiter"
It allows any character sequence, except that it must not contain the
closing sequence )delimiter". It is used to avoid escaping of any
character. Anything between the delimiters becomes part of the string.
const char* s1 = R"foo(
Hello
World
)foo";
Example taken from cppreference.
I have a homework assignment where part of the menu has to have "R\C" printed, but when I run the program the console just prints "RC". Does anyone know why is this happening and how I can fix it?
This is what I have in Visual Studio:
cout << "R\C" << endl;
The \C is being interpreted as an (invalid) escape sequence. You need to escape the \ character as \\ in order to print it as a single \, eg:
cout << "R\\C" << endl;
Alternatively, in C++11 and later, you can use a raw string literal instead, so you do not need to escape the \ character:
cout << R"(R\C)" << endl;
Escape \ with another \:
cout << "R\\C" << endl;
c++ reserve some characters, so you can't directly input them, usually you will have to put \ in front of them to signify that you want to use "\" as a string.
You have to use escape sequences for certain characters. For the character that you specified you would have to output as “\\” and your output would be \. Other escape sequences are:
\’
\t For Tab
\n For newline
\? For question marks
See this for more information.
You can use escape sequences.., like \t, \n, \a...
If you want to print ' \ ', you have to code like this
cout<<"\\";
This question already has answers here:
std::regex escape special characters for use in regex
(3 answers)
Closed 6 years ago.
Say you have a string which is provided by the user. It can contain any kind of character. Examples are:
std::string s1{"hello world");
std::string s1{".*");
std::string s1{"*{}97(}{.}}\\testing___just a --%#$%# literal%$#%^"};
...
Now I want to search in some text for occurrences of >> followed by the input string s1 followed by <<. For this, I have the following code:
std::string input; // the input text
std::regex regex{">> " + s1 + " <<"};
if (std::regex_match(input, regex)) {
// add logic here
}
This works fine if s1 did not contain any special characters. However, if s1 had some special characters, which are recognized by the regex engine, it doesn't work.
How can I escape s1 such that std::regex considers it as a literal, and therefore does not interpret s1? In other words, the regex should be:
std::regex regex{">> " + ESCAPE(s1) + " <<"};
Is there a function like ESCAPE() in std?
important I simplified my question. In my real case, the regex is much more complex. As I am only having troubles with the fact the s1 is interpreted, I left these details out.
You will have to escape all special characters in the string with \. The most straightforward approach would be to use another expression to sanitize the input string before creating the expression regex.
// matches any characters that need to be escaped in RegEx
std::regex specialChars { R"([-[\]{}()*+?.,\^$|#\s])" };
std::string input = ">> "+ s1 +" <<";
std::string sanitized = std::regex_replace( input, specialChars, R"(\$&)" );
// "sanitized" can now safely be used in another expression
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;
C++ supports three kinds of escape sequences:
simple-escape-sequence. It is one of:
\’ \" \? \\
\a \b \f \n \r \t \v
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.
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";
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".
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.
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.
int main()
{
char* a = " 'Fools\' day' ";
char* b[64];
sscanf(a, " '%[^']s ", b);
printf ("%s", b);
}
--> puts "Fools" in b
Obviously, I want to have "Fools' day" in b. Can I tell sscanf() not to consider escaped apostrophes as the end of the character sequence?
Thanks!
No. Those functions just read plain old characters. They don't interpret the contents according to any escaping rules because there's nothing to escape from — quotation marks, apostrophes, and backslashes aren't special in the input string.
You'll have to use something else to parse your string. You can write a little state machine to read the string one character at a time, keeping track of whether the previous character was a backslash. (Don't just scan to the next apostrophe and then look one character backward; if you're allowed to escape backslashes as well as apostrophes, then you could end up re-scanning all the way back to the start of the string to see whether you have an odd or even number of escape characters. Always parse strings forward, not backward.)
Replace
char* a = " 'Fools\' day' ";
with
char* a = " 'Fools' day' ";
The ' character isn't special inside a C string (although it is special within a single char). So there is not need to escape it.
Also, if all you want is "Fools' day", why put the extra 's at the start and end? Maybe you are confusing C strings with those in some other language?
Edit:
As Rob Kennedy's comment says, I was assuming you are supplying the string yourself. Otherwise, see Rob's answer.
Why on earth would you write such a thing, instead of using std::string? Since your question is tagged C++.
int main(int argc, char* argv[])
{
std::string a = " 'Fools' day' ";
std::string b(a.begin() + 2, std::find(a.begin() + 2, a.end(), ' '));
std::cout << b;
std::cin.get();
}
Edit: Oh wait a second, you want to read a string within a string? Just use escaped double quotes, e.g.
int main(int argc, char* argv[]) {
std::string a = " \"Fool's day\" ";
auto it = std::find(a.begin(), a.end(), '"');
std::string b(it, std::find(it, a.end(), '"');
std::cout << b;
}
If the user put the string in, they won't have to escape single quotes, although they would have to escape double quotes, and you'd have to make your own system for that.