Characters, Strings and the String Class - C plus plus - Lecture Slides, Slides of Computer Engineering and Programming

Characters, Strings and the String Class in C++. The C++ library provides several macros for testing characters such as isalpha, isalnum, Isdigit, islower, Isprint, ispunct, isupper, Isspace. Description for C++ functions strlen, strcat, strcpy, strncpy, strcmp, strstr, atoi, atol, atof, itoa is given in this lecture.

Typology: Slides

2012/2013

Uploaded on 12/19/2013

atifarifasif
atifarifasif 🇵🇰

4.2

(14)

39 documents

1 / 58

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Characters, Strings, and the string
Class
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a

Partial preview of the text

Download Characters, Strings and the String Class - C plus plus - Lecture Slides and more Slides Computer Engineering and Programming in PDF only on Docsity!

Characters, Strings, and the string

Class

10.1 Character Testing

• The C++ library provides several macros

for testing characters.

– Be sure to include ctype.h header file

Program 10- 1

// This program demonstrates some of the character testing // functions. #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;

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"; }

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 bool 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);

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. bool testNum(char custNum[]) { // Test the first three characters for alphabetic letters for (int count = 0; count < 3; count++) {

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

Enter a customer number in the form LLLNNNN

(LLL = letters and NNNN = numbers): AX467T9 [Enter]

That is not the proper format of the customer number.

Here is an example:

ABC

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 header file

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 = 3.14159; float radius; char go; cout << "This program calculates the area of a circle.\n"; cout.precision(2); cout.setf(ios::fixed);

Program continues

do { cout << "Enter the circle's radius: "; cin >> radius; cout << "The area is " << (pi * radius * radius); cout << endl; do { cout << "Calculate another? (Y or N) "; cin >> go; } while (toupper(go) != 'Y' && toupper(go) != 'N'); } while (toupper(go) == 'Y'); }

10.3 Review of the Internal Storage of

C-strings

• A C-string is a sequence of characters stored

in consecutive memory locations,

terminated by a null character.

Figure 10- 1

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'); }

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!

10.4 Library Functions for Working with

C-strings

• The C++ library has numerous functions for

handling C-strings These functions perform

various tests and manipulations.

• Functions discussed in this section require

the inclusion of string.h header file