1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
// This program will have user input names and have them stored as strings. Computer will then output them.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string full_name; // creating a string variable named 'name'
string first, last; // creating 2 different string varaibles, 'first' and 'last'
string combine;
// This will have the user enter both their first and last name using 1 variable
cout << "What is your full name? ";
getline(cin,full_name); /* A user enters their name. This is different from how you enter numbers, just using the 'cin' command
The value they entered (command 'cin'), is assigned to string varaible 'my_name'
Note: You can enter a first and last name, with a space between them here, and it will store it with the space.
You do not need to use an underscore '_'
*/
cout << "My name is: " << full_name << endl;
// This will have the user enter both their first and last name, seperatly, using 2 variables
cout << "\n" << endl;
cout << "Enter your first name: ";
getline(cin,first);
cout << "Enter your last name: ";
getline(cin,last);
cout << "Your name is: " << first << " " << last << endl;
// data output
cout << "\nLength of your first name: " << first.length() << " characters" << endl;
cout << "Length of your last name: " << last.length() << " characters" << endl;
cout << "Length of your whole name: " << full_name.length() - 1 << " characters" << endl; // this outputs as a number, so the "-1" removes the 1 space character
// this combines 2 strings into 1
combine = first;
combine.insert(combine.length(), " " + last); /* - This will join the first ane last name, and assign it to the string 'combine, e.g. Bob Smith
- If you wanted to, you could put a '-' mark, like "-" instead, and the first and last names would be hyphenated
instead of spaced apart, e.g. e.g. Bob-Smith
- if you just did combine.insert(combine.length(), last); then the first and last names would be joined together, e.g. BobSmith
*/
cout << "\nYour combined first and last name is: " << combine << endl;
// find position of a letter in the first name
cout << "\nThe letter 'd' was found at: " << first.find("d".3) << endl;
/*
for (int i = 0; i < first.length(); i++)
{
cout << "\nThe letter 'g' was found at: " << first.find("g") << endl;
}
// cout << "\nThe letter 'g' was found at: " << first.find("g") << endl; // by default, you start searching at character position 0. It is case-sensitive
// do a for loop to end the search after all character spaces are checked so that no duplicates would be missed. Therefore, probably have to feed in string length into for loop?
*/
cin.ignore();
cin.get();
return 0;
}
|