Jump to content

Finding Last character in c++ string

HeaterUp

Hello everyone, I am currently making a program right now in c++ that I need to be able to find the last character in a c++ string and I was looking for if there was any operations in the string library to do that. If I can't do that I would like to search through a user inputted string for a certain letter these are the two options I have thought of to tackle the task. I am limiting myself to where I am not able to use <cstdlib>, any structs, or pointers so keep that in mind.

Thanks for any answers.

Link to comment
Share on other sites

Link to post
Share on other sites

9 minutes ago, HeaterUp said:

Hello everyone, I am currently making a program right now in c++ that I need to be able to find the last character in a c++ string and I was looking for if there was any operations in the string library to do that. If I can't do that I would like to search through a user inputted string for a certain letter these are the two options I have thought of to tackle the task. I am limiting myself to where I am not able to use <cstdlib>, any structs, or pointers so keep that in mind.

Thanks for any answers.

Barring the back function, a well formed string should have a null terminator. Otherwise you could look for a newline character, assuming the user presses enter to input the text.

Link to comment
Share on other sites

Link to post
Share on other sites

33 minutes ago, M.Yurizaki said:

Barring the back function, a well formed string should have a null terminator. Otherwise you could look for a newline character, assuming the user presses enter to input the text.

Follow up question I'm now realizing ill need the first character as well.

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, HeaterUp said:

Follow up question I'm now realizing ill need the first character as well.

string[0] will bring you the first character.

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, NoGravityPanda said:

string[0] will bring you the first character.

Thank you very much 

Link to comment
Share on other sites

Link to post
Share on other sites

8 hours ago, M.Yurizaki said:

Barring the back function, a well formed string should have a null terminator. Otherwise you could look for a newline character, assuming the user presses enter to input the text.

There's nothing in the pre C++ 11 standard that says a std::string has to include a 0-terminator.The C string returned by std::string::c_str() includes the 0-terminator ofc, but that does not mean the actual (implementation defined) representation of the string inside std::string includes it. Thus:

std::string Foo("Bar");

//Foo[3] is not guaranteed to be 0, in fact, it could be undefined behavior (out of bounds access).

C++11 has new requirements for std::string that imply 0-termination, but even then it is a bad idea to look for a 0-terminator in a std::string for the reason that it is perfectly legal for a std::string to contain 0-characters (that are NOT the end of the string).

 

Inputting a string using streams does not store the newline in the string.

 

@HeaterUp

Accessing the last character:

//C++11
char LastChar = Foo.back();

//In C++03, std::string::back is missing, but one can use reverse iterator begin (this works in C++11 too, ofc:
char LastChar = *Foo.rbegin();

Accessing the first character:

//C++11
char FirstChar = Foo.front();

//In C++03, std::string::front is missing, but one can use iterator begin (this works in C++11 too, ofc:
char FirstChar = *Foo.begin();

Take care to guard against doing this on empty strings.

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, Unimportant said:

There's nothing in the pre C++ 11 standard that says a std::string has to include a 0-terminator.The C string returned by std::string::c_str() includes the 0-terminator ofc, but that does not mean the actual (implementation defined) representation of the string inside std::string includes it. Thus:


std::string Foo("Bar");

//Foo[3] is not guaranteed to be 0, in fact, it could be undefined behavior (out of bounds access).

C++11 has new requirements for std::string that imply 0-termination, but even then it is a bad idea to look for a 0-terminator in a std::string for the reason that it is perfectly legal for a std::string to contain 0-characters (that are NOT the end of the string).

So basically C++ added proper string functionality.

Link to comment
Share on other sites

Link to post
Share on other sites

12 minutes ago, M.Yurizaki said:

So basically C++ added proper string functionality.

I don't see how it was not proper before. std::string encapsulates the string as any object should. You can get the string's length trough a member function, you don't and should not manually look for the end. The internal representation of the string is the STL developer's problem, not yours. Once you request a C style string trough std::string::c_str() the 0-terminator is there but that function's sole use is to interface with C functions, such as a library, and should never be used for any other reason imho.

 

I kinda like the fact that std::getline reads an entire line from, for example, a file, even if that line happens to contain a 0-character and can return it in a std::string. That, and similar functionality, would not be possible if we had to sacrifice 0 as a termination character for a self contained object that can keep track of it's own length internally anyway.

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Unimportant said:

I don't see how it was not proper before. std::string encapsulates the string as any object should. You can get the string's length trough a member function, you don't and should not manually look for the end. The internal representation of the string is the STL developer's problem, not yours. Once you request a C style string trough std::string::c_str() the 0-terminator is there but that function's sole use is to interface with C functions, such as a library, and should never be used for any other reason imho.

 

I kinda like the fact that std::getline reads an entire line from, for example, a file, even if that line happens to contain a 0-character and can return it in a std::string. That would not be possible if we had to sacrifice 0 as a termination character for a self contained object that can keep track of it's own length internally anyway.

What I'm saying is C++ treats strings as something more like a data type than borrowing off another one with special rules.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×