CSCI
152 String Comparison -- Traditional
C Strings file: cstrings.cpp
1
#include <iostream>
2
#include <cstring>
3 using
namespace std;
4
typedef char String20 [21];
5 void
main ()
{
6
String20 myName = "
7
char yourName [21]; // or String20 yourName;
8
char actor[21];
// or String20 actor;
9
cout << "My name is "; //
or for (n = 0; myName[n] != '\0';
n++)
10 cout << myName << endl; // cout << myName[n];
11 cout << "What's yours?"
<< endl; //
or n = 0;
12 cin >> yourName; // while ((myName[n] =
cin.get()) != '\n')
// n++;
// myName[n] = '\0';
13 cout << "Hi " <<
yourName << ", it's nice to meet you!" << endl;
14 if ( strcmp(myName, yourName) == 0 )
15 cout << "Wow! We have the
same name!" << endl;
16 else if ( strcmp(myName, yourName) < 0
)
17 cout << "I go first in
alphabetical order\n";
18 else if ( strcmp(myName, yourName) > 0
)
19 cout << "You go first in
alphabetical order\n";
20 if ( strlen(myName) < strlen(yourName)
)
21 cout << "My name is
shorter than yours.\n";
22 else if ( strlen(myName) >
strlen(yourName) )
23 cout << "Your name is
shorter than mine.\n";
24 else cout << "Our names are the
same length\n";
25 cout << "Who's your favorite
actor?" << endl;
26 cin >> actor; //
Assume user types a first-name last-name combination
27 cout << actor << " who? Tell me
again.\n";
28 cin.ignore(80, '\n'); //
Get rid of the extra chars (last name) previously entered.
29 cin.get(actor, 21); //
or cin.getline(actor, 21);
30 cout << "Oh, " <<
actor << " -- good choice!" << endl;
31 cout
<< "Gotta go -- bye!\n";
}
Sample run with user
input in bold:
My name is
What's yours?
Thomas
Hi Thomas, it's nice to
meet you!
I go first in
alphabetical order
My name is shorter than
yours.
Who's your favorite
actor?
Harrison Ford
Harrison Ford
Oh, Harrison Ford --
good choice!
Gotta go -- bye!
CSCI
152 String Comparison -- C++ String
Class file: strings.cpp
1
#include <iostream>
2
#include <string>
3
using namespace std;
5 void
main ()
{
6
string myName = "
7
string yourName;
8
string actor;
9
cout << "My name is ";
10 cout << myName << endl; //
or for (int n = 0; n <
myName.length(); n++)
// cout << myName[n];
// cout << endl;
11 cout << "What's yours?"
<< endl;
12 cin >> yourName;
13 cout << "Hi " <<
yourName << ", it's nice to meet you!" << endl;
14 if ( myName == yourName )
15 cout << "Wow! We have the
same name!" << endl;
16 else if ( myName < yourName )
17 cout << "I go first in
alphabetical order\n";
18 else if ( myName > yourName )
19 cout << "You go first in
alphabetical order\n";
20 if ( myName.length() <
yourName.length() )
21 cout << "My name is
shorter than yours.\n";
22 else if ( myName.length() >
yourName.length() )
23 cout << "Your name is shorter
than mine.\n";
24 else cout << "Our names are
the same length.\n";
25 cout << "Who's your favorite
actor?" << endl;
26 cin >> actor; // Assume user types a first-name
last-name combination
27 cout << actor << "
who? Tell me again.\n";
28 cin.ignore(80, '\n'); // Get rid of the extra chars (last name)
previously entered.
29 getline(cin, actor); // Reads all chars up to a newline and dumps
the newline.
// Visual 6.0 has a
bug which requires pressing Enter twice.
30 cout << "Oh, " <<
actor << " -- good choice!" << endl;
31 cout << "Gotta go --
bye!\n";
}
Sample run with user
input in bold:
My name is
What's yours?
Amy
Hi Amy, it's nice to
meet you!
You go first in
alphabetical order
Your name is shorter
than mine.
Who's your favorite
actor?
Denzel Washington
Denzel who? Tell me again.
Denzel Washington
Oh, Denzel Washington
-- good choice!
Gotta go -- bye!