C++
How do you get the first character of a string? The last character? How do you remove the first character? The last character?
#include <iostream> #include <string> using namespace std; int main() { string s = "hello how are you?"; // How do you get the first character of a string? cout << s[0] << endl; // The last character? cout << s[s.length()-1] << endl; // How do you remove the first character? s.erase(s.begin()); cout << "String after removing first character is " << s << endl; // The last character? s.erase(s.begin() + s.length() - 1); cout << "String after removing last character is " << s << endl; return 0; }
Get Answers For Free
Most questions answered within 1 hours.