Write a program that prompts the user to enter a string and displays the number of characters it contains, fourth character, and last character. Note: The string may contain blanks. For example: “C++ programming is fun”. You should write a complete program.
C++ Code :
#include <iostream>
#include <string>
using namespace std;
int main()
{
//declaring the string
string s;
//reading string from user
getline(cin, s);
//calculating length of string
int length= s.length();
//printing number of characters in string
cout<<"The number of characters in string
is : "<<length<<endl;
//printing 4th character in string
cout<<"The fourth character in string is :
"<<s[3]<<endl;
//printing last character in string
cout<<"The last character in string is :
"<<s[length-1]<<endl;
return 0;
}
Output :
Get Answers For Free
Most questions answered within 1 hours.