String.Length
//Demonstrates using a string object as if it were an array.
#include <iostream> #include <string> using namespace std; int main( ) { string firstName, lastName; cout << "Enter your first and last name:\n"; cin >> firstName >> lastName; cout << "Your last name is spelled:\n"; int i; for (i = 0; i < lastName.length( ); i++) { cout << lastName[i] << " "; // Printing the last name with a space after that. lastName[i] = '-'; // Replacing the LastName with a "-" character. You not see the result yet. } cout << endl; for (i = 0; i < lastName.length( ); i++) cout << lastName[i] << " "; //Places a "-" under each letter, and prints a space after theme. cout << endl; cout << "Good day " << firstName <<"."<< endl; return 0; } |
No comments:
Post a Comment