-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
25933cb
commit 2217aa7
Showing
27 changed files
with
764 additions
and
1 deletion.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
...r-10-Characters-C-Strings-and-More-about-the-string-Class/10.1 Character Testing/10-1.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// This program demonstrated som character-testing functions. | ||
#include <iostream> | ||
#include <cctype> | ||
using namespace std; | ||
|
||
int main() | ||
{ | ||
char input; | ||
|
||
cout << "Enter any 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"; | ||
|
||
return 0; | ||
} |
49 changes: 49 additions & 0 deletions
49
...r-10-Characters-C-Strings-and-More-about-the-string-Class/10.1 Character Testing/10-2.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// This program tests a customer number to determine whether | ||
// it is in the proper format | ||
#include <iostream> | ||
#include <cctype> | ||
using namespace std; | ||
|
||
// Function prototype | ||
bool testNum(char[], int); | ||
|
||
int main() | ||
{ | ||
const int SIZE = 8; | ||
char customer[SIZE]; | ||
|
||
// Get the customer number. | ||
cout << "Enter a customer number in the form "; | ||
cout << "LLLNNNN\n"; | ||
cout << "(LLL = letters and NNNN = number): "; | ||
cin.getline(customer, SIZE); | ||
|
||
// Determine whether it is valid. | ||
if (testNum(customer, SIZE)) | ||
cout << "That's a valid customer number.\n"; | ||
else | ||
{ | ||
cout << "That is not the proper format of the " | ||
<< "customer number.\nHere is an example:\n" | ||
<< " ABC1234\n"; | ||
} | ||
return 0; | ||
} | ||
bool testNum(char custNum[], int size) | ||
{ | ||
int count; | ||
|
||
for (count = 0; count < 3; count++) | ||
{ | ||
if (!isalpha(custNum[count])) | ||
return false; | ||
} | ||
|
||
for (count = 3; count < size - 1; count++) | ||
{ | ||
if (!isdigit(custNum[count])) | ||
return false; | ||
} | ||
|
||
return true; | ||
} |
34 changes: 34 additions & 0 deletions
34
...racters-C-Strings-and-More-about-the-string-Class/10.2 Character Case Conversion/10-3.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#include <iostream> | ||
#include <cctype> | ||
#include <iomanip> | ||
using namespace std; | ||
|
||
int main() | ||
{ | ||
const double PI = 3.14159; | ||
double radius; | ||
char goAgain; | ||
|
||
cout << "This program calculates the area of a circle.\n"; | ||
cout << fixed << setprecision(2); | ||
|
||
do | ||
{ | ||
cout << "Enter the circle's radius: "; | ||
cin >> radius; | ||
cout << "The area is " << (PI * radius * radius); | ||
cout << endl; | ||
|
||
cout << "Calculate another? (Y or N) "; | ||
cin >> goAgain; | ||
|
||
while (toupper(goAgain) != 'Y' && toupper(goAgain) != 'N') | ||
{ | ||
cout << "Please enter Y or N: "; | ||
cin >> goAgain; | ||
} | ||
|
||
} while (toupper(goAgain) == 'Y'); | ||
|
||
return 0; | ||
} |
16 changes: 16 additions & 0 deletions
16
Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.3 C-String/10-4.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#include <iostream> | ||
using namespace std; | ||
int main() | ||
{ | ||
char again; | ||
|
||
do | ||
{ | ||
cout << "C++ programming is great fun!" << endl | ||
<< "Do you want to see the message again? "; | ||
cin >> again; | ||
|
||
} while (again == 'Y' || again == 'y'); | ||
|
||
return 0; | ||
} |
22 changes: 22 additions & 0 deletions
22
Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.3 C-String/10-5.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#include <iostream> | ||
using namespace std; | ||
|
||
int main() | ||
{ | ||
const int SIZE = 80; | ||
char line[SIZE]; | ||
int count = 0; | ||
|
||
cout << "Enter a sentence of no more than " | ||
<< (SIZE - 1) << " characters:\n"; | ||
cin.getline(line, SIZE); | ||
|
||
cout << "The sentence you entered is:\n"; | ||
while (line[count] != '\0') | ||
{ | ||
cout << line[count]; | ||
count++; | ||
} | ||
|
||
return 0; | ||
} |
39 changes: 39 additions & 0 deletions
39
...nd-More-about-the-string-Class/10.4 Library Functions for Working with C-Strings/10-6.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// This program uses the strstr function to search an array. | ||
#include <iostream> | ||
#include <cstring> // For strstr | ||
using namespace std; | ||
|
||
int main() | ||
{ | ||
const int NUM_PRODS = 5; | ||
const int LENGTH = 27; | ||
|
||
char products[NUM_PRODS][LENGTH] = | ||
{ "TV327 31-inch Television", | ||
"CD257 CD Player", | ||
"TA677 Answering Machine", | ||
"CS109 Car Stereo", | ||
"PC955 Personal Computer" }; | ||
|
||
char lookUp[LENGTH]; | ||
char *strPtr = nullptr; | ||
int index; | ||
|
||
cout << "\tProduct Database\n\n" | ||
<< "Enter a product number to search for: "; | ||
cin.getline(lookUp, LENGTH); | ||
|
||
for (index = 0; index < NUM_PRODS; index++) | ||
{ | ||
strPtr = strstr(products[index], lookUp); | ||
if (strPtr != nullptr) | ||
break; | ||
} | ||
|
||
if (strPtr != nullptr) | ||
cout << products[index] << endl; | ||
else | ||
cout << "No matching product was found.\n"; | ||
|
||
return 0; | ||
} |
21 changes: 21 additions & 0 deletions
21
...nd-More-about-the-string-Class/10.4 Library Functions for Working with C-Strings/10-7.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#include <iostream> | ||
#include <string> | ||
using namespace std; | ||
|
||
int main() | ||
{ | ||
const int LENGTH = 40; | ||
char firstString[LENGTH], secondString[LENGTH]; | ||
|
||
cout << "Enter a string: "; | ||
cin.getline(firstString, LENGTH); | ||
cout << "Enter another string: "; | ||
cin.getline(secondString, LENGTH); | ||
|
||
if (strcmp(firstString, secondString) == 0) | ||
cout << "You entered the same string twice.\n"; | ||
else | ||
cout << "The strings are not the same.\n"; | ||
|
||
return 0; | ||
} |
31 changes: 31 additions & 0 deletions
31
...nd-More-about-the-string-Class/10.4 Library Functions for Working with C-Strings/10-8.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#include <iostream> | ||
#include <cstring> | ||
#include <iomanip> | ||
using namespace std; | ||
|
||
int main() | ||
{ | ||
const double A_PRICE = 99.0, | ||
B_PRICE = 199.0; | ||
|
||
const int PART_LENGTH = 9; | ||
char partNum[PART_LENGTH]; | ||
|
||
cout << "The MP3 player part numbers are:\n" | ||
<< "\t16 Gigabyte, part number S147-29A\n" | ||
<< "\t32 Gigabyte, part number S147-29B\n" | ||
<< "Enter the part number of the MP3 player you\n" | ||
<< "wish to purchase: "; | ||
|
||
cin >> partNum; | ||
|
||
cout << showpoint << fixed << setprecision(2); | ||
if (strcmp(partNum, "S147-29A") == 0) | ||
cout << "The price is $" << A_PRICE << endl; | ||
else if (strcmp(partNum, "S147-29B") == 0) | ||
cout << "The price is $" << B_PRICE << endl; | ||
else | ||
cout << partNum << " is not a valid part number.\n"; | ||
|
||
return 0; | ||
} |
24 changes: 24 additions & 0 deletions
24
...nd-More-about-the-string-Class/10.4 Library Functions for Working with C-Strings/10-9.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#include <iostream> | ||
#include <string> | ||
using namespace std; | ||
|
||
int main() | ||
{ | ||
const int NAME_LENGTH = 30; | ||
char name1[NAME_LENGTH], name2[NAME_MAX]; | ||
|
||
cout << "Enter a name (last name first): "; | ||
cin.getline(name1, NAME_LENGTH); | ||
cout << "Enter another name: "; | ||
cin.getline(name2, NAME_LENGTH); | ||
|
||
cout << "Here are the names sorted alphabetically:\n"; | ||
if (strcmp(name1, name2) < 0) | ||
cout << name1 << endl << name2 << endl; | ||
else if (strcmp(name1, name2) > 0) | ||
cout << name2 << endl << name1 << endl; | ||
else | ||
cout << "You entered the same name twice!\n"; | ||
|
||
return 0; | ||
} |
32 changes: 32 additions & 0 deletions
32
...trings-and-More-about-the-string-Class/10.5 String Numeric Conversion Functions/10-10.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#include <iostream> | ||
#include <cctype> | ||
#include <string> | ||
using namespace std; | ||
|
||
int main() | ||
{ | ||
string input; | ||
int total = 0, | ||
count = 0; | ||
double average; | ||
|
||
cout << "This program will average a series of numbers.\n" | ||
<< "Enter the first number or Q to quite: "; | ||
getline(cin, input); | ||
|
||
while(tolower(input[0]) != 'q') | ||
{ | ||
total += stoi(input); | ||
count++; | ||
cout << "Enter the next number of Q to quit: "; | ||
getline(cin, input); | ||
} | ||
|
||
if (count != 0) | ||
{ | ||
average = static_cast<double>(total) / count; | ||
cout << "Average: " << average << endl; | ||
} | ||
|
||
return 0; | ||
} |
37 changes: 37 additions & 0 deletions
37
...trings-and-More-about-the-string-Class/10.5 String Numeric Conversion Functions/10-11.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#include <iostream> | ||
#include <string> | ||
#include <iomanip> | ||
using namespace std; | ||
|
||
int main() | ||
{ | ||
string input, | ||
name; | ||
int idNumber, | ||
age; | ||
double income; | ||
|
||
cout << "What is your ID number? "; | ||
getline(cin, input); | ||
idNumber = stoi(input); | ||
|
||
cout << "What is your name? "; | ||
getline(cin, name); | ||
|
||
cout << "What is your age? "; | ||
getline(cin, input); | ||
age = stoi(input); | ||
|
||
cout << "What is your annual income? "; | ||
getline(cin, input); | ||
income = stod(input); | ||
|
||
cout << setprecision(2) << fixed << showpoint; | ||
cout << "Your name is " << name | ||
<< ", your are " << age | ||
<< " years old, \nand you make $" | ||
<< income << " per year.\n"; | ||
|
||
|
||
return 0; | ||
} |
33 changes: 33 additions & 0 deletions
33
...0.6 Focus on Software Engineering: Writing Your Own C-string-Handling Functions/10-12.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#include <iostream> | ||
using namespace std; | ||
|
||
void stringCopy(char[], char[]); | ||
|
||
int main() | ||
{ | ||
const int LENGTH = 5; | ||
char first[LENGTH], | ||
second[LENGTH]; | ||
|
||
cout << "Enter a string with no more than " | ||
<< (LENGTH - 1) << " characters:\n"; | ||
cin.getline(first, LENGTH); | ||
|
||
stringCopy(first, second); | ||
|
||
cout << "The string you entered is:\n" << second << endl; | ||
|
||
return 0; | ||
} | ||
void stringCopy(char string1[], char string2[]) | ||
{ | ||
int index = 0; | ||
|
||
while (string1[index] != '\0') | ||
{ | ||
string2[index] = string1[index]; | ||
index++; | ||
} | ||
|
||
string2[index] = '\0'; | ||
} |
Oops, something went wrong.