Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 10 – Characters and Strings

Similar presentations


Presentation on theme: "Chapter 10 – Characters and Strings"— Presentation transcript:

1 Chapter 10 – Characters and Strings

2 10.1 Character Testing The C++ library provides macros for testing characters. Be sure to include ctype.h

3 Table 10-1

4 Program 10-1 // This program demonstrates some of the character testing // macros. #include <iostream.h> #include <ctype.h> void main(void) { char Input; cout << "Enter any character: "; cin.get(Input); cout << "The character you entered is: " << Input << endl; cout << "Its ASCII code is: " << int(Input) << endl;

5 Program continues 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"; }

6 Program Output With Example Input
Enter any character: A [Enter] The character you entered is: A Its ASCII code is: 65 That's an alphabetic character. The letter you entered is uppercase. Program Output With Other Example Input Enter any character: 7 [Enter] The character you entered is: 7 Its ASCII code is: 55 That's a numeric digit.

7 Program 10-2 // This program tests a customer number to determine if it is // in the proper format. #include <iostream.h> #include <ctype.h> // Function prototype int TestNum(char []); void main(void) { char Customer[8]; cout << "Enter a customer number in the form "; cout << "LLLNNNN\n"; cout << "(LLL = letters and NNNN = numbers): "; cin.getline(Customer, 8);

8 Program continues if (TestNum(Customer))
cout << "That's a valid customer number.\n"; else { cout << "That is not the proper format of the "; cout << "customer number.\nHere is an example:\n"; cout << " ABC1234\n"; } // Definition of function TestNum. int TestNum(char CustNum[]) // Test the first three characters for alphabetic letters for (int Count = 0; Count < 3; Count++)

9 Program continues if (!isalpha(CustNum[Count])) return 0; }
// Test the last 4 characters for numeric digits for (int Count = 3; Count < 7; Count++) { if (!isdigit(CustNum[Count])) return 1;

10 Program Output With Example Input
Enter a customer number in the form LLLNNNN (LLL = letters and NNNN = numbers): RQS4567 [Enter] That's a valid customer number. Program Output With Other Example Input (LLL = letters and NNNN = numbers): AX467T9 [Enter] That is not the proper format of the customer number. Here is an example: ABC1234

11 10.2 Character Case Conversion
The C++ library offers functions for converting a character to upper or lower case. Be sure to include ctype.h

12 Table 10-2

13 Program 10-3 // This program calculates the area of a circle. It asks the user // if he or she wishes to continue. A loop that demonstrates the // toupper function repeats until the user enters 'y', 'Y', // 'n', or 'N'. #include <iostream.h> #include <ctype.h> void main(void) { const float Pi = ; float Radius; char Go; cout << "This program calculates the area of a circle.\n"; cout.precision(2); cout.setf(ios::fixed);

14 Program continues 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 >> Go; } while (toupper(Go) != 'Y' && toupper(Go) != 'N'); } while (toupper(Go) == 'Y'); }

15 Program Output With Example Input
This program calculates the area of a circle. Enter the circle's radius: 10 [Enter] The area is Calculate another? (Y or N) b Enter] Calculate another? (Y or N) y [Enter] Enter the circle's radius: 1 [Enter] The area is 3.14 Calculate another? (Y or N) n [Enter]

16 10.3 Review of the Internal Storage of Strings
In C++, a sting is a sequence of characters stored in consecutive memory locations, terminated by a null character. Figure 10-1

17 Program 10-4 // This program contains string constants
#include <iostream.h> void main(void) { char Again; do cout << "C++ programming is great fun!" << endl; cout << "Do you want to see the message again? "; cin >> Again; } while (Again == 'Y' || Again == 'y'); }

18 Program 10-5 // This program cycles through a character array, displaying // each element until a null terminator is encountered. #include <iostream.h> void main(void) { char Line[80]; int Count = 0; cout << "Enter a sentence of no more than 79 characters:\n"; cin.getline(Line, 80); cout << "The sentence you entered is:\n"; while (Line[Count] != '\0') cout << Line[Count]; Count++; }

19 Program Output with Example Input
Enter a sentence of no more than 79 characters: C++ is challenging but fun! [Enter] The sentence you entered is: C++ is challenging but fun!

20 10.4 Library Functions for Working with Strings
The C++ library has numerous functions for handling string.s These functions perform various tests and manipulations. Functions discussed in this section require the inclusion of string.h

21 Figure 10-2

22 Table 10-3

23 Program 10-6 // This program uses the strstr function to search an array // of strings for a name. #include <iostream.h> #include <string.h> // For strstr void main(void) { char Prods[5][27] = {"TV inch Television", "CD257 CD Player", "TA677 Answering Machine", "CS109 Car Stereo", "PC955 Personal Computer"}; char LookUp[27], *StrPtr = NULL; int Index;

24 Program continues cout << "\tProduct Database\n\n";
cout << "Enter a product number to search for: "; cin.getline(LookUp, 27); for (Index = 0; Index < 5; Index++) { StrPtr = strstr(Prods[Index], LookUp); if (StrPtr != NULL) break; } if (StrPtr == NULL) cout << "No matching product was found.\n"; else cout << Prods[Index] << endl;

25 Program Output With Example Input
Product Database Enter a product to search for: CD257 [Enter] CD257 CD Player Program Output With Example Input Enter a product to search for: CS [Enter] CS109 Car Stereo Program Output With Other Example Input Enter a product to search for: AB [Enter] No matching product was found.

26 10.5 String/Numeric Conversion Functions
The C++ library provides functions for converting a string representation of a number to a numeric data type, and vice-versa. The functions in this section require the stdlib.h file to be included.

27 Table 10-4

28 Program 10-7 // This program demonstrates the strcmp and atoi functions. #include <iostream.h> #include <string.h> // For strcmp #include <stdlib.h> // For atoi void main(void) { char Input[20]; int Total = 0, Count = 0; float Average; cout << "This program will average a series of numbers.\n"; cout << "Enter the first number or Q to quit: "; cin.getline(Input, 20);

29 Program continues while ((strcmp(Input, "Q") != 0)&&(strcmp(Input, "q") != 0)) { Total += atoi(Input); // Keep a running total Count++; // Keep track of how many numbers entered cout << "Enter the next number or Q to quit: "; cin.getline(Input, 20); } if (Count != 0) Average = Total / Count; cout << "Average: " << Average << endl;

30 Program Output With Example Input
This program will average a series of numbers. Enter the first number or Q to quit: 74 [Enter] Enter the next number or Q to quit: 98 [Enter] Enter the next number or Q to quit: 23 [Enter] Enter the next number or Q to quit: 54 [Enter] Enter the next number or Q to quit: Q [Enter] Average: 62

31 10.6 Focus on Software Engineering: Writing Your Own String-Handling Functions
You can design your own specialized functions for manipulating strings.

32 Program 10-8 // This program uses a function to copy a string into an array. #include <iostream.h> void StringCopy(char [], char []); // Function prototype void main(void) { char First[30], Second[30]; cout << "Enter a string with no more than 29 characters:\n"; cin.getline(First, 30); StringCopy(First, Second); cout << "The string you entered is:\n" << Second << endl; }

33 Program continues void StringCopy(char String1[], char String2[]) {
int Index = 0; while (String1[Index] != '\0') String2[Index] = String1[Index]; Index++; } String2[Index] = '\0';

34 Program Output With Example Input
Enter a string with no more than 29 characters: Thank goodness it’s Friday! [Enter] The string you entered is: Thank goodness it's Friday!

35 Program 10-9 // This program uses the function NameSlice to "cut" the last // name off of a string that contains the user's first and // last names. #include <iostream.h> void NameSlice(char []); // Function prototype void main(void) { char Name[41]; cout << "Enter your first and last names, separated "; cout << "by a space:\n"; cin.getline(Name, 41); NameSlice(Name); cout << "Your first name is: " << Name << endl; }

36 Program continues // Definition of function NameSlice. This function accepts a // character array as its argument. It scans the array looking // for a space. When it finds one, it replaces it with a null // terminator. void NameSlice(char UserName[]) { int Count = 0; while (UserName[Count] != ' ' && UserName[Count] != '\0') Count++; if (UserName[Count] == ' ') UserName[Count] = '\0'; }

37 Program Output With Example Input
Enter your first and last names, separated by a space: Jimmy Jones [Enter] Your first name is: Jimmy

38 Figure 10-3

39 Figure 10-4

40 Program 10-10 // This program demonstrates a function, CountChars, that counts // the number of times a specific character appears in a string. #include <iostream.h> // Function prototype int CountChars(char *, char); void main(void) { char UserString[51], Letter; cout << "Enter a string (up to 50 characters): "; cin.getline(UserString, 51); cout << "Enter a character and I will tell you how many\n"; cout << "times it appears in the string: "; cin >> Letter; cout << Letter << " appears "; cout << CountChars(UserString, Letter) << " times.\n"; }

41 Program continues // Definition of CountChars. The parameter StrPtr is a pointer // that points to a string. The parameter Ch is a character that // the function searches for in the string. The function returns // the number of times the character appears in the string. int CountChars(char *StrPtr, char Ch) { int Times = 0; while (*StrPtr != '\0') if (*StrPtr == Ch) Times++; StrPtr++; } return Times;

42 Program Output With Example Input
Enter a string (up to 50 characters):Starting Out With C++ [Enter] Enter a character and I will tell you how many times it appears in the string: t [Enter] t appears 4 times.


Download ppt "Chapter 10 – Characters and Strings"

Similar presentations


Ads by Google