/***************************************************************
* Name: Prof. Orta
* Course: Computer Science & Programming
* Class: CS04103 Section: 1 & 2
* Assignment Date: 11/19/20
* File Name: Chapter12.cpp
*****************************************************************
* Purpose: Explain the use of C-string and String objects
*****************************************************************/
#include <iostream>
#include <cctype>
#include <iomanip>
#include <string>
#include <cstring>
using namespace std;
int main()
{
char input, initial;
string line;
line.assign(80,'*');
cout << line << endl << setw(45) << right << "Testing Characters" << endl << line << endl;
// Testing Characters
cout << "\nPlease enter a character: ";
cin.get(input);
cout << "The character you entered is: " << input << endl;
if (isalpha(input))
cout << "That's an alphabetic character.\n";
if (isdigit(input))
cout << "That's a numeric digit.\n";
if (islower(input))
cout << "The letter you entered is lowercase.\n";
if (isupper(input))
cout << "The letter you entered is uppercase.\n";
if (isspace(input))
cout << "That's a whitespace character.\n";
// Character case conversion
cout << "\nPress <enter> to continue";
cin.get();
cin.ignore();
cout << endl << line << endl << setw(45) << right << "Characters case conversion" << endl << line << endl;
cout << "\nPlease enter your first initial in uppercase: ";
cin.get(initial);
cout << "Your first initial is: " << char(tolower(initial)) ;
cout << "\nPlease enter your Last name initial in lowercase: ";
cin.ignore();
cin.get(initial);
cout << "Your last name initial is: " << char(toupper(initial)) << endl;
// C-string
/*Because an array is a sequence of consecutive memory locations that store values of
the same type, a C-string is really a null-terminated array of characters.
C-strings can appear in a program in one of three forms:
“Hard-coded” string literals
Programmer-defined arrays of character
Pointers to character */
cout << "\nPress <enter> to continue";
cin.get();
cin.ignore();
cout << line << endl << setw(45) << right << "C-strings" << endl << line << endl;
const char *aPtr = nullptr;
char *fnamePtr;
aPtr = "C-String Literal";
cout << "This is a " << aPtr;
char fname[20]="Rafael ";
char lname[]="Orta";
char fullName[20];
cout << "\nMy name is: " << endl;
for (int s=0 ; s<7 ; s++)
{
cout << endl <<fname[s];
}
// Pointer to the array.
cout << "\nPress <enter> to continue:";
cin.get();
cin.ignore();
fnamePtr = fname;
cout << "\nMy name is: " << fnamePtr << " and it has " << strlen(fname)-1 << " characters " << endl;
cout << "My full name is: " << strcat(fname,lname);
strcpy(fullName,fname);
cout << "\nMy full Name is: " << fullName;
if (strcmp(fname, fullName)==0)
cout << "\nThey are the same" << endl;
else
cout << "\nThey are different" << endl;
// Converting to Strings
cout << "\nPress <enter> to continue:";
cin.get();
cin.ignore();
cout << line << endl << setw(45) << right << "Converting to Strings" << endl << line << endl;
int a = 5;
string str = to_string(a*a);
cout << "The square of 5 is " << str << endl;
// The String Class
cout << "\nPress <enter> to continue:";
cin.get();
cin.ignore();
cout << line << endl << setw(45) << right << "The String class" << endl << line << endl;
string f1name = "Rafael Orta";
cout << "The full name: " << f1name << " has " << f1name.size() << " characters ";
cout << "\nThe space is located in the position: " << f1name.find(' ',0);
cout << "\nThe charater at the position 7 is: " << f1name.at(7);
cout << "\nThe last name is: " << f1name.substr(7,4);
return 0;
}