diff --git a/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.1 Character Testing/10-1.cpp b/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.1 Character Testing/10-1.cpp new file mode 100644 index 0000000..f3e7c28 --- /dev/null +++ b/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.1 Character Testing/10-1.cpp @@ -0,0 +1,26 @@ +// This program demonstrated som character-testing functions. +#include +#include +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; +} \ No newline at end of file diff --git a/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.1 Character Testing/10-2.cpp b/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.1 Character Testing/10-2.cpp new file mode 100644 index 0000000..23a28d2 --- /dev/null +++ b/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.1 Character Testing/10-2.cpp @@ -0,0 +1,49 @@ +// This program tests a customer number to determine whether +// it is in the proper format +#include +#include +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; +} \ No newline at end of file diff --git a/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.2 Character Case Conversion/10-3.cpp b/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.2 Character Case Conversion/10-3.cpp new file mode 100644 index 0000000..402b31e --- /dev/null +++ b/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.2 Character Case Conversion/10-3.cpp @@ -0,0 +1,34 @@ +#include +#include +#include +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; +} \ No newline at end of file diff --git a/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.3 C-String/10-4.cpp b/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.3 C-String/10-4.cpp new file mode 100644 index 0000000..13e0124 --- /dev/null +++ b/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.3 C-String/10-4.cpp @@ -0,0 +1,16 @@ +#include +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; +} \ No newline at end of file diff --git a/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.3 C-String/10-5.cpp b/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.3 C-String/10-5.cpp new file mode 100644 index 0000000..3a3eb87 --- /dev/null +++ b/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.3 C-String/10-5.cpp @@ -0,0 +1,22 @@ +#include +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; +} \ No newline at end of file diff --git a/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.4 Library Functions for Working with C-Strings/10-6.cpp b/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.4 Library Functions for Working with C-Strings/10-6.cpp new file mode 100644 index 0000000..9764481 --- /dev/null +++ b/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.4 Library Functions for Working with C-Strings/10-6.cpp @@ -0,0 +1,39 @@ +// This program uses the strstr function to search an array. +#include +#include // 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; +} \ No newline at end of file diff --git a/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.4 Library Functions for Working with C-Strings/10-7.cpp b/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.4 Library Functions for Working with C-Strings/10-7.cpp new file mode 100644 index 0000000..17bbc52 --- /dev/null +++ b/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.4 Library Functions for Working with C-Strings/10-7.cpp @@ -0,0 +1,21 @@ +#include +#include +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; +} \ No newline at end of file diff --git a/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.4 Library Functions for Working with C-Strings/10-8.cpp b/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.4 Library Functions for Working with C-Strings/10-8.cpp new file mode 100644 index 0000000..c1bf3c4 --- /dev/null +++ b/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.4 Library Functions for Working with C-Strings/10-8.cpp @@ -0,0 +1,31 @@ +#include +#include +#include +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; +} \ No newline at end of file diff --git a/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.4 Library Functions for Working with C-Strings/10-9.cpp b/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.4 Library Functions for Working with C-Strings/10-9.cpp new file mode 100644 index 0000000..cecbb65 --- /dev/null +++ b/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.4 Library Functions for Working with C-Strings/10-9.cpp @@ -0,0 +1,24 @@ +#include +#include +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; +} \ No newline at end of file diff --git a/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.5 String Numeric Conversion Functions/10-10.cpp b/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.5 String Numeric Conversion Functions/10-10.cpp new file mode 100644 index 0000000..f1bde4a --- /dev/null +++ b/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.5 String Numeric Conversion Functions/10-10.cpp @@ -0,0 +1,32 @@ +#include +#include +#include +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(total) / count; + cout << "Average: " << average << endl; + } + + return 0; +} \ No newline at end of file diff --git a/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.5 String Numeric Conversion Functions/10-11.cpp b/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.5 String Numeric Conversion Functions/10-11.cpp new file mode 100644 index 0000000..b311802 --- /dev/null +++ b/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.5 String Numeric Conversion Functions/10-11.cpp @@ -0,0 +1,37 @@ +#include +#include +#include +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; +} \ No newline at end of file diff --git a/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.6 Focus on Software Engineering: Writing Your Own C-string-Handling Functions/10-12.cpp b/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.6 Focus on Software Engineering: Writing Your Own C-string-Handling Functions/10-12.cpp new file mode 100644 index 0000000..5fb8f7a --- /dev/null +++ b/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.6 Focus on Software Engineering: Writing Your Own C-string-Handling Functions/10-12.cpp @@ -0,0 +1,33 @@ +#include +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'; +} \ No newline at end of file diff --git a/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.6 Focus on Software Engineering: Writing Your Own C-string-Handling Functions/10-13.cpp b/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.6 Focus on Software Engineering: Writing Your Own C-string-Handling Functions/10-13.cpp new file mode 100644 index 0000000..c357f4d --- /dev/null +++ b/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.6 Focus on Software Engineering: Writing Your Own C-string-Handling Functions/10-13.cpp @@ -0,0 +1,28 @@ +#include +using namespace std; + +void nameSlice(char[]); + +int main() +{ + const int SIZE = 41; + char name[SIZE]; + + cout << "Enter you first and last names, separated " ; + cout << "by a space:\n"; + cin.getline(name, SIZE); + nameSlice(name); + cout << "Your first name is: " << name << endl; + + return 0; +} +void nameSlice(char userName[]) +{ + int count = 0; + + while (userName[count] != ' ' && userName[count] != '\0') + count++; + + if (userName[count] == ' ') + userName[count] = '\0'; +} \ No newline at end of file diff --git a/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.6 Focus on Software Engineering: Writing Your Own C-string-Handling Functions/10-14.cpp b/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.6 Focus on Software Engineering: Writing Your Own C-string-Handling Functions/10-14.cpp new file mode 100644 index 0000000..5abee6f --- /dev/null +++ b/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.6 Focus on Software Engineering: Writing Your Own C-string-Handling Functions/10-14.cpp @@ -0,0 +1,37 @@ +#include +using namespace std; + +int countChars(char *, char); + +int main() +{ + const int SIZE = 51; + char userString[SIZE]; + char letter; + + cout << "Enter a string (up to 50 characters): "; + cin.getline(userString, SIZE); + + cout << "Enter a character and I will tell you how many\n" + << "times it appears in the string: "; + cin >> letter; + + cout << letter << " appears "; + cout << countChars(userString, letter) << " times.\n"; + + return 0; +} + +int countChars(char *strPtr, char ch) +{ + int times = 0; + + while (*strPtr != '\0') + { + if (*strPtr == ch) + times++; + strPtr++; + } + + return times; +} \ No newline at end of file diff --git a/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.7 More about the C++ string Class/10-15.cpp b/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.7 More about the C++ string Class/10-15.cpp new file mode 100644 index 0000000..ace7368 --- /dev/null +++ b/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.7 More about the C++ string Class/10-15.cpp @@ -0,0 +1,13 @@ +#include +#include +using namespace std; + +int main() +{ + string movieTitle; + + movieTitle = "Wheels of Fury"; + cout << "My favorite movie is " << movieTitle << endl; + + return 0; +} \ No newline at end of file diff --git a/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.7 More about the C++ string Class/10-16.cpp b/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.7 More about the C++ string Class/10-16.cpp new file mode 100644 index 0000000..8d515cf --- /dev/null +++ b/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.7 More about the C++ string Class/10-16.cpp @@ -0,0 +1,14 @@ +#include +#include +using namespace std; + +int main() +{ + string name; + + cout << "What is you name? "; + cin >> name; + cout << "Good morning " << name << endl; + + return 0; +} \ No newline at end of file diff --git a/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.7 More about the C++ string Class/10-17.cpp b/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.7 More about the C++ string Class/10-17.cpp new file mode 100644 index 0000000..47a52c5 --- /dev/null +++ b/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.7 More about the C++ string Class/10-17.cpp @@ -0,0 +1,27 @@ +#include +#include +#include +using namespace std; + +int main() +{ + const double APRICE = 249.0; + const double BPRICE = 199.0; + string partNum; + + cout << "The headphone part numbers are:\n" + << "\tNoise canceling, part number S147-29A\n" + << "\tWireless, part number S147-29B\n" + << "Enter the part number of the desired headphones: "; + cin >> partNum; + cout << fixed << showpoint << setprecision(2); + + if (partNum == "S147-29A") + cout << "The price is $" << APRICE << endl; + else if (partNum == "S147-29B") + cout << "The price is $" << BPRICE << endl; + else + cout << partNum << " is not a valid part number.\n"; + + return 0; +} \ No newline at end of file diff --git a/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.7 More about the C++ string Class/10-18.cpp b/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.7 More about the C++ string Class/10-18.cpp new file mode 100644 index 0000000..d93c741 --- /dev/null +++ b/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.7 More about the C++ string Class/10-18.cpp @@ -0,0 +1,24 @@ +#include +#include +using namespace std; + +int main() +{ + string name1, name2; + + cout << "Enter a name (last name first): "; + getline(cin, name1); + + cout << "Enter another name: "; + getline(cin, name2); + + cout << "Here are the names sorted alphabetically:\n"; + if (name1 < name2) + cout << name1 << endl << name2 << endl; + else if (name1 > name2) + cout << name2 << endl << name1 << endl; + else + cout << "You entered the same name twice!\n"; + + return 0; +} \ No newline at end of file diff --git a/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.7 More about the C++ string Class/10-19.cpp b/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.7 More about the C++ string Class/10-19.cpp new file mode 100644 index 0000000..39b96ca --- /dev/null +++ b/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.7 More about the C++ string Class/10-19.cpp @@ -0,0 +1,14 @@ +#include +#include +using namespace std; + +int main() +{ + string greeting; + string name("William Smith"); + + greeting = "Hello "; + cout << greeting << name << endl; + + return 0; +} \ No newline at end of file diff --git a/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.7 More about the C++ string Class/10-20.cpp b/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.7 More about the C++ string Class/10-20.cpp new file mode 100644 index 0000000..3fca765 --- /dev/null +++ b/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.7 More about the C++ string Class/10-20.cpp @@ -0,0 +1,21 @@ +#include +#include +using namespace std; + +int main() +{ + string str1, str2, str3; + + str1 = "ABC"; + str2 = "DEF"; + str3 = str1 + str2; + + cout << str1 << endl; + cout << str2 << endl; + cout << str3 << endl; + + str3 += "GHI"; + cout << str3 << endl; + + return 0; +} \ No newline at end of file diff --git a/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.7 More about the C++ string Class/10-21.cpp b/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.7 More about the C++ string Class/10-21.cpp new file mode 100644 index 0000000..b60a582 --- /dev/null +++ b/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.7 More about the C++ string Class/10-21.cpp @@ -0,0 +1,14 @@ +#include +#include +using namespace std; + +int main() +{ + string town; + cout << "Where do you live? "; + cin >> town; + cout << "Your town's name has " << town.length() + << " characters\n"; + + return 0; +} \ No newline at end of file diff --git a/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.7 More about the C++ string Class/10-22.cpp b/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.7 More about the C++ string Class/10-22.cpp new file mode 100644 index 0000000..6a4856d --- /dev/null +++ b/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/10.7 More about the C++ string Class/10-22.cpp @@ -0,0 +1,21 @@ +#include +#include +using namespace std; + +int main() +{ + string str1 = "ABC", + str2 = "DEF", + str3 = str1 + str2; + + for (int x = 0; x < str3.size(); x++) + cout << str3[x]; + cout << endl; + + if (str1 < str2) + cout << "str1 is less than str2\n"; + else + cout << "str1 is not less than str2\n"; + + return 0; +} \ No newline at end of file diff --git a/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/Checkpoints/10.1-10.5.cpp b/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/Checkpoints/10.1-10.5.cpp new file mode 100644 index 0000000..915fb05 --- /dev/null +++ b/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/Checkpoints/10.1-10.5.cpp @@ -0,0 +1,58 @@ +// 10.1 Write a short description of each of the following functions: +// +// isalpha - Returns true (a nonzero number) if the argument is a letter of the alphabet +// isalnum - Retruns true if the argument is a letter of the alphabet or a digit +// isdigit - Returns true if the argument is a digit from 0 through 9. +// islower - Returns true if the argument is a lowercase letter. +// isprint - Returns true if the argument is a printable character (including a space). +// ispunct - Returns true if the arguemt is a printable character other than a digit, letter, or space. +// isupper - Returns true if the argument is an uppercase letter. +// isspace - Returns true if the argument is a whitespace character. +// toupper - Returns the uppercase equivalent of its argument. +// tolower - Returns the lowercase equivalent of its argument. +// +#include +#include +using namespace std; +int main() +{ + // 10.2 Write a statement that will convert the contents of the + // char variable big to lowercase. The converted value should be + // assigned to the variable little. + char big = 'A', + little = tolower(big); + + cout << "big = " << big << endl; + cout << "little = " << little << endl; + + // 10.3 Write an if statement that will display the word “digit” + // if the variable ch contains a numeric digit. Otherwise, it + // should display “Not a digit.” + char ch = 'c'; + cout << (isdigit(ch) ? "digit" : "Not a digit") << endl; + + // 10.4 What is the output of the following statement? + cout << toupper(tolower('A')) << endl; // A + + // 10.5 Write a loop that asks the user "Do you want to + // repeat the program or quit? (R/Q)". The loop should + // repeat until the user has entered an R or Q (either + // uppercase or lowercase). + char user_choice; + do + { + cout << "Do you want to repeat the program or quit? (R/Q)"; + cin >> user_choice; + + while (toupper(user_choice) != 'R' && toupper(user_choice) != 'Q') + { + cout << "Enter either Q or R: "; + cin >> user_choice; + } + + + } while (user_choice == 'R'); + + cout << endl; + return 0; +} diff --git a/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/Checkpoints/10.11-10.15.cpp b/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/Checkpoints/10.11-10.15.cpp new file mode 100644 index 0000000..cfaac0d --- /dev/null +++ b/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/Checkpoints/10.11-10.15.cpp @@ -0,0 +1,49 @@ +/** + * 10.11 Write a short description of + * each of the following functions: + * + * atoi = converts a C-string to an integer and returns that value. + * atol = converts a C-string to a long integer and returns that value. + * atof = converts a C-string to a double and returns that value. + * itoa = converts an integer to a C-string and returns that value. + * stoi = returns a string argument converted to an int + * stol = returns a string argument converted to a long + * stof = returns a string argument converted to a float + * stod = returns a string argument converted to a double + **/ +#include +#include +using namespace std; +int main() +{ + // 10.12 Write a statement that will convert the string "10" + // to an integer and store the result in the variable num. + string string_number = "10"; + int num = stoi(string_number); + cout << "string_number = " << string_number << endl; + cout << "num = " << num << endl; + + // 10.13 Write a statement that will convert the string "100000" + // to a long and store the result in the variable num. + string_number = "100000"; + num = stol(string_number); + cout << "string_number = " << string_number << endl; + cout << "num = " << num << endl; + + // 10.14 Write a statement that will convert the string "7.2389" + // to a double and store the result in the variable num. + string_number = "7.2389"; + double num_double = stod(string_number); + cout << "string_number = " << string_number << endl; + cout << "num_double = " << num_double << endl; + + // 10.15 Write a statement that will convert the integer 127 + // to a string, and assign the result to a string object + // named str. + num = 127; + string_number = to_string(num); + cout << "num = " << num << endl; + cout << "string_number = " << string_number << endl; + + return 0; +} \ No newline at end of file diff --git a/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/Checkpoints/10.16.cpp b/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/Checkpoints/10.16.cpp new file mode 100644 index 0000000..f59eeee --- /dev/null +++ b/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/Checkpoints/10.16.cpp @@ -0,0 +1,29 @@ +/** + * 10.16 What is the output of the following program? + */ +#include +using namespace std; + +void mess(char[]); + +int main() +{ + char stuff[] = "Tom Talbert Tried Trains"; + cout << stuff << endl; + mess(stuff); + cout << stuff << endl; + + return 0; +} + +void mess(char str[]) +{ + int step = 0; + + while (str[step] != '\0') + { + if (str[step] == 'T') + str[step] = 'D'; + step++; + } +} \ No newline at end of file diff --git a/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/Checkpoints/10.6-10.10.cpp b/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/Checkpoints/10.6-10.10.cpp new file mode 100644 index 0000000..44d7999 --- /dev/null +++ b/Chapter-10-Characters-C-Strings-and-More-about-the-string-Class/Checkpoints/10.6-10.10.cpp @@ -0,0 +1,47 @@ +// 10.6 Write a short description of each of the following functions: +// strlen: returns the length of a C-string (not including the null terminator) +// strcat: appends the contents of the second string to the first C-string. +// strcpy: copies the second C-string to the first C-string. +// strncat: third argument indicates the maximum number of characters to copy from the second C-string to the first C-string +// strncpy: third argument indicates the maximum number of characters to copy from the second C-string to the first C-string +// - if the 3rd argument (int) is greater that the length of the 2nd argument (string length), the null terminator +// is not automatically appended to the 1st argument. +// - if the 3rd argument is greater than the length of the 2nd argument, then the 1st argument is padded with '\0' characters. +// strcmp: if argument 1 and 2 are the same, the function returns 0. +// - If 2nd argument is alphabetically greater than the 1st argument, it returns a negative number. +// - If 2nd arguemnt is alphabetically less than the 1st argument, it returns a positive number. +// strstr: searches for the first occurrence of the 2nd argument in the 1st argument. +// - if an occurrence of argument 2 is found, the function returns a pointer to it. +// Otherwise, it returns nullptr (address 0). +#include +#include +#include +using namespace std; + +int main() +{ + // 10.7 What will the following program segment display? + char dog[] = "Fido"; + cout << strlen(dog) << endl; // 4 + + // 10.8 What will the following program segment display? + char string1[16] = "Have a "; + char string2[9] = "nice day"; + strcat(string1, string2); + cout << string1 << endl; // Have a nice day + cout << string2 << endl; // nice day + + // 10.9 Write a statement that will copy the string "Beethoven" to the array composer. + char composer[strlen( "Beethoven")]; + strcpy(composer, "Beethoven"); + cout << composer << endl; + + // 10.10 When complete, the following program skeleton will search for the string "Windy" + // in the array place. If place contains "Windy" the program will display the + // message "Windy found." Otherwise, it will display "Windy not found." + char place[] = "The Cindy City"; + + cout << ((strstr(place, "Windy") != nullptr) ? "Windy found." : "Windy not found.") << endl; + + return 0; +} \ No newline at end of file diff --git a/Chapter-9-Pointers/Review Questions and Exercises/Programming Challenges/07.cpp b/Chapter-9-Pointers/Review Questions and Exercises/Programming Challenges/07.cpp index af4c80a..a3f93fb 100644 --- a/Chapter-9-Pointers/Review Questions and Exercises/Programming Challenges/07.cpp +++ b/Chapter-9-Pointers/Review Questions and Exercises/Programming Challenges/07.cpp @@ -1,4 +1,4 @@ -/** +/** ++ * 7. Case Study Modification #2 * Modify Program 9-19 (the United Cause case study program) * so the arrptr array is sorted in descending order instead @@ -28,6 +28,7 @@ int main() arrSelectSort(arrPtr, NUM_DONATIONS); + // Changed to descending... cout << "The donations, sorted in descending order, are: \n"; showArrPtr(arrPtr, NUM_DONATIONS); @@ -48,6 +49,8 @@ void arrSelectSort(int *arr[], int size) minElem = arr[startScan]; for (int index = startScan + 1; index < size; index++) { + // Changed from less than (<) to + // greater than (>) if (*(arr[index]) > *minElem) { minElem = arr[index];