How to return the last character of a String in C++?

Asked

Viewed 84 times

3

I want to make a program that shows on the screen the last character of a String. In Python it would be something like this:

nome = "Teste"
print(nome[-1])

Upshot:

and

So I tried to adapt it to C++:

#include <iostream>
using namespace std;
    
int main(){
    string nome="teste";
    cout<<nome[-1]<<endl;
       
    return 0;
}

And in the end he showed nothing.

So how could I display the last character of a C string++?

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site.

2 answers

5

C++ does not have this syntax. Use back(). Always look at the documentation of what you are using to find out what is available to that object. And note that this method gives you the character by reference and not by copy as Python does, so it may not be what you want in some situations, but it may be the best option. Don’t even think about converting code from one language to another, it’s not just syntax that changes, it’s not whimsy that there are several languages, there’s an important change.

#include <iostream>
using namespace std;

int main() {
   string nome = "teste";
   cout << nome.back() << endl;
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Another option is to use the rbegin() which is the iterator, it alone delivers just the last element of the collection, which is the case of a string. With it you can make a copy of the character easily. Again you need to see the documentation for correct use since it returns a pointer. In a way it is more interesting because it does not accept only the equivalent of -1, it accepts other characters looking the other way.

#include <iostream>
using namespace std;

int main() {
   string nome = "teste";
   cout << *nome.rbegin() << endl;
}

Finally you can catch doing what Python does which is to use mathematics and additional information that does not appear in the syntax, IE, takes the size and adds the value that used as index (yes, adds, but as it is negative, ends up subtracting):

#include <iostream>
using namespace std;

int main() {
   string nome = "teste";
   cout << nome[nome.length() - 1] << endl;
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

3

You can use the method std::string::back(). Read more about it in the documentation link.

Note that C++ does not have the same Python syntax, so its code does not work.

#include <iostream>
using namespace std;

int main() {
    string nome = "teste";
    cout << nome.back() << endl;   
    return 0;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.