Question

C++ How do you get the first character of a string? The last character? How do...

C++

How do you get the first character of a string? The last character? How do you remove the first character? The last character?

Homework Answers

Answer #1
#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;
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Complete the method so that it returns a string with the first and last character removed...
Complete the method so that it returns a string with the first and last character removed from the input string public class Class1 { public static String clipEnds(String str) { } }
ow do you check if a string starts with a certain character in c programing language?...
ow do you check if a string starts with a certain character in c programing language? For example if a user puts in a word like cake how od i check if that word starts with c?
write a program (in Java/Eclipse to loop through a string character by character. If the character...
write a program (in Java/Eclipse to loop through a string character by character. If the character is a letter, print a question mark, otherwise print the character. Use the code below for the message string. This will be the first string that you will decode. Use the String class method .charAt(index) and the Character class method .isLetter(char). (You can cut and paste this line into your program.) String msg = "FIG PKWC OIE GJJCDVKLC MCVDFJEHIY BIDRHYO.\n";
3. Removing a character from a string Write a function remove() that asks the user to...
3. Removing a character from a string Write a function remove() that asks the user to enter a string and an index, and displays the string with the character at that index removed. You may assume the given index is within the string. For example, removing the character at index 2 in "hello" should produce "helo". Hint: Use slicing.
Specify and implement an ADT character string by using a linked chain of characters. Include typical...
Specify and implement an ADT character string by using a linked chain of characters. Include typical operations such as fi nding its length, appending one string to another, fi nding the index of the leftmost occurrence of a character in a string, and testing whether one string is a substring of another. Do not #include<string>...you are making something like a STL string. Also do not use an array or vector...the point is to practice linked lists. Remember the last character...
C++: Write a function that receives a pointer to a character string consisting of only alphabets...
C++: Write a function that receives a pointer to a character string consisting of only alphabets a-z and returns the character with the maximum number of repetitions. If more than one-character repeats same number of times, return the character with smallest alphabetical order. For example, the string “Mississippi” has three repeated characters (i-4, s-4, p-2). Both ‘i’ and ‘s’ repeated 4 times but ‘i’ is alphabetically smaller hence the function should return ‘i’. Do not count repeated blanks, special characters,...
Implement Python logic to determine the “last’ letter of the alphabet in following string.       my_str...
Implement Python logic to determine the “last’ letter of the alphabet in following string.       my_str = "I went fishing, crabbing, and swimming yesterday. It was a great day, and I hope to do it again next weekend." The ‘last’ character of the provided string (my_str) is ‘y’ Feel free to convert the string to all upper or lower case letters first.
In C++ , given, string words = "OO/CC II/ZZ"; How do you make it so if...
In C++ , given, string words = "OO/CC II/ZZ"; How do you make it so if user inputs Z, you replace of the Z with ' '  and return words = "OO/CC II/Z "; and if the user inputs another Z then it returns words = "OO/CC II/ "; but if the user inputs Z for the third time, it makes the user re-enter another character and shows a retry message.
Get taylor's string from ln (1 + x) and do the coding in C ++ OOP.
Get taylor's string from ln (1 + x) and do the coding in C ++ OOP.
You may notice that the function strchr in the <cstring> library searches for a character within...
You may notice that the function strchr in the <cstring> library searches for a character within a string, and returns the memory address (pointer) to where that character first occurs. 1.) Write your own version of the strchr function, int findchr( char text[ ], char target) , which returns the index of the first occurrance of the character target if it occurs within the string text. If target does not occur within the string, a -1 is to be returned....