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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
#include <iostream>
#include <string>
using namespace std;
int findCharInString(const string, char );
int main()
{
string name1 = "Hello from CIS!";
string name2, name3, name4, name5, s;
int where;
name2 = "Mobile";
cout << name1 << endl
<< name2 << " welcomes you!" << endl;
cout << "From that first line, characters 2 to 7 (positions 1 to 6) are: ";
for (int i = 1; i <= 6; i++)
{
cout << name1[i] << endl;
}
name3 = "Hi there!";
cout << name3 << endl;
name4 = name3;
cout << "Once again... " << name4 << endl
<< "name3 = " << name3 << endl << "name4 = " << name4 << endl;
if (name3 == name4)
{
cout << "So... it looks like name3 and name4 are equal!\n";
}
else
{
cout << "name3 and name4 are not equal.\n";
}
cout << "name1 =" << name1 << endl << "name4 = "<< name4 << endl;
if(name1 == name4)
{
cout << "So... it looks like name1 and name4 are equal!\n";
}
else if(name1 < name4)
{
cout << "name1 is less than name4.\n";
}
else
{
cout << "name1 is greater than name4.\n";
}
name5 = name3 + name4;
cout << "name3 and name4 back to back: "<< name5 << endl;
// For fun let us call upon one of our own functions.
where = findCharInString(name3, 'X');
if (where != -1)
{
cout << "'" << name3 << "' has an 'X' in position " << where << endl;
}
else
{
cout << "'" << name3 << "' does not contain an 'X'.\n";
}
if (findCharInString(name3, 't') != -1)
{
cout<< "'" << name3 << "' has a 't' in position "<< where << endl;
}
else
{
cout << "'" << name3 << "' does not contain a 't'.\n";
}
cout << "name1 = '" << name1 << "' and length = " << name1.length() << endl
<< "name2 = '" << name2 << "' and length = " << name2.1ength() << endl
<< "name3 = '" << name3 << "' and length = " << name3.1ength() << endl
<< "name4 = '" << name4 << "' and length = " << name4.1ength() << endl
<< "name5 = '" << name5 << "' and length = " << name5.1ength() << endl;
cout << "Enter your name full name: " ;
getline(cin,name5);
cout << "Hi there " << name5 << endl;
cout << "Note, using getline version reading name gets full string!" << endl;
cout<<"Enter full name again: " ;
cin >> name5;
cout << "Hi there " << name5 << endl;
cout << "Note:reading into a string variable only goes up to blank." << endl;
cout << "name1.substr(6,4) yields -> \'" << name1.substr(6,4) << "\'" << endl;
getline(cin,s);
return (0);
}
int findCharInString(const string source, char seeking)
{
int answer = -1;
for (int i=0; (i < source.length()) && (answer == -1); i++)
if (source[i] == seeking)
{
answer = i;
}
return(answer);
}
|