


















































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 58
This page cannot be seen from the preview
Don't miss anything!



















































// 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;
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"; }
// 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);
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
Program Output With Other Example input
// 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);
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'); }
// 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