This question already has answers here:
Get the last element of a std::string
(4 answers)
Closed 7 years ago.
In python you can say print "String"[-1] and it would print be the last character, 'g'. Is there an equivalent for this in c++?
You can use string.back() to get a reference to the last character in the string. The last character of the string is the first character in the reversed string, so string.rbegin() will give you an iterator to the last character.
Use the back() function for std::string:
std::string str ("Some string");
cout << str.back()
Output:
g
For C strings, it is
String[strlen(String) - 1];
For C++ style strings, it is either
String.back();
*String.rbegin();
String[String.length() - 1];
You can use the function:
my_string.back();
If you want to output it, then:
#include <iostream>
std::cout << my_string.back();
Related
This question already has answers here:
What do single quotes do in C++ when used on multiple characters?
(5 answers)
Closed 3 years ago.
#include <iostream>
int main() {
std::cout << 'hello';
return 0;
}
This program output is:
1701604463
I wonder why it actually runs, although the compiler gives a warning message: character constant too long for its type.
What these numbers actually mean, are they garbage digits?
It is multicharacter literal which has type int.
Multicharacter literal, e.g. 'AB', has type int and
implementation-defined value.
This question already has answers here:
How to replace all occurrences of a character in string?
(17 answers)
Closed 4 years ago.
I've tried:
versionString = versionString.replace(versionString.begin(), versionString.end(), '(' , '-' );
The result is: "--------------". Basically replacing all the characters. What is that?
versionString is a basic string.
If you look at e.g. this std::string::replace reference you will see that there's no overload that takes the arguments you pass. Something the compiler really should warn you about.
The closes one is number 6:
basic_string& replace( const_iterator first, const_iterator last,
size_type count2, CharT ch );
which replaces the range with count2 copies of ch.
That is, you replace your string with '(' number of dashes. With ASCII that '(' will be converted to the integer 40 (it's this conversion the compiler should have warned you about).
One solution is to repeatedly find the character you want to replace, and replace only that single character.
A much simpler solution is to use the standard algorithm function std::replace:
std::replace(begin(versionString), end(versionString), '(', '-');
This question already has answers here:
What does while(*pointer) means in C?
(5 answers)
Closed 5 years ago.
This code will print a string of characters:
const char* aString = "This is a string";
const char* ptrString = aString;
while(*ptrString)
{ std::cout << *ptrString;
ptrString++;
}
I'm still a newbie. From what I had learnt so far, while loop always has a condition inside the bracket like while(x<10) or something. I just don't understand why does the while loop in this case has only *ptrString as its condition. What does it actually mean? Can somebody explain to me?
When the pointer reaches the end of the string it finds an end of string character which value is 0, which evaluates to false when casting to bool. Then the loop ends.
while(*ptrString)
It stops when the *ptrString is null character. As ptrString is incremented in ptrString++;, eventually *ptrString will point to null character.
You can simplify the loop body to
std::cout << *ptrString++; // Ideal use of post-fix operator
though..
This question already has answers here:
cout << with char* argument prints string, not pointer value
(6 answers)
Closed 6 years ago.
Even though the name of the string signifies address of the first element in the string,when I perform this:
char str[]="dog";
cout<<str<<endl;
How come the whole string gets printed?
In C++, the "<<" operator is "smart". It knows the types of the thing to its left and to its right, and if the thing to its right is a character pointer and the thing to its left is an output stream, it prints the string rather than the value of the pointer.
In C, you have to tell printf() which one to do with "%p" or "%s", but "<<" makes that choice for you.
Google "C++ operator overloading"
This question already has answers here:
What is the type of a string literal in C++? [duplicate]
(2 answers)
Closed 7 years ago.
When I use :
std::cout << "Hello world ";
Which type is "Hello world" ?
Where does it stored , so I can get it out and work with it ?
For some reasons, I don't want to use something like :
std::string str = "Hello world";
std::cout << str;
Please help me, I searched an hour but still no answer.
The type of a string literal is "constant array of char", with as many elements as characters in the literal, plus one for a final null character. Other versions of string literals (wide, unicode) are arrays of other character types (wchar_t, char16_t etc.) (e.g. see here).