0% found this document useful (0 votes)
43 views21 pages

Characters and Strings: Namiq Sultan

This document is a chapter from a C++ programming textbook. It discusses character testing and manipulation functions in C++, including functions to test properties of characters like isalpha and isdigit. It also covers functions for converting between uppercase and lowercase as well as common string functions like strlen, strcat, and strcmp. Examples of using these functions are provided.

Uploaded by

omer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views21 pages

Characters and Strings: Namiq Sultan

This document is a chapter from a C++ programming textbook. It discusses character testing and manipulation functions in C++, including functions to test properties of characters like isalpha and isdigit. It also covers functions for converting between uppercase and lowercase as well as common string functions like strlen, strcat, and strcmp. Examples of using these functions are provided.

Uploaded by

omer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 21

Chapter 10

Characters and Strings

Namiq Sultan
University of Duhok
Department of Electrical and Computer Engineerin

Reference: Starting Out with C++, Tony Gaddis, 2nd Ed.

C++ Programming, Namiq Sultan 1


10.1 Character Testing
• The C++ library provides several macros for testing
characters.
– Be sure to include ctype.h header file

C++ Programming, Namiq Sultan 2


Table 10-1
Character Description
Macro
is a lp h a Returns true (a nonzero number) if the argument is a letter of
the alphabet. Returns 0 if the argument is not a letter.
is d ig it Returns true (a nonzero number) if the argument is a digit 0–9.
Otherwise it returns 0.
islo w e r Returns true (a nonzero number) if the argument is a lowercase
letter. Otherwise, it returns 0.
is u p p e r Returns true (a nonzero number) if the argument is an
uppercase letter. Otherwise, it returns 0.
is s p a c e Returns true (a nonzero number) if the argument is a
whitespace character. Whitespace characters are any of the
following:

C++ Programming, Namiq Sultan 3


Program 10-1
// This program demonstrates some of the character
// testing functions.
#include <iostream.h>
#include <ctype.h>

void main()
{
char input;

cout << "Enter any character: ";


cin.get(input); // Read one character
cout << "The character you entered is: " << input << endl;
cout << "Its ASCII code is: " << int(input) << endl;

C++ Programming, Namiq Sultan 4


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

C++ Programming, Namiq Sultan 5


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.

C++ Programming, Namiq Sultan 6


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

C++ Programming, Namiq Sultan 7


Table 10-2

Function De scription

to u p p e r Re turns the uppercas e equiva le nt of its a rgume nt.

to lo w e r Re turns the lowe rca s e e quiva le nt of its a rgume nt.

C++ Programming, Namiq Sultan 8


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()
{
const float pi = 3.14159;
float radius;
char go;

cout << "This prog calculates the area of a circle.\n";

C++ Programming, Namiq Sultan 9


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

C++ Programming, Namiq Sultan 10


Program Output With Example input
This program calculates the area of a circle.
Enter the circle's radius: 10 [Enter]
The area is 314.16
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]

C++ Programming, Namiq Sultan 11


10.3 Review of the Internal Storage of Strings
• A string is a sequence of characters stored in consecutive
memory locations, terminated by a null character.

Figure 10-1

C++ Programming, Namiq Sultan 12


Program 10-5
// This prog cycles through character array, displaying each element
// until a null terminator is encountered.
#include <iostream.h>

void main()
{
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++;
}
}

C++ Programming, Namiq Sultan 13


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!

C++ Programming, Namiq Sultan 14


10.4 Library Functions for Working with 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

C++ Programming, Namiq Sultan 15


Figure 10-2

C++ Programming, Namiq Sultan 16


Table 10-3
Function Description
s trle n Accepts a string and returns the length of the string. Example: len =
strlen(name);
s trc a t Accepts two strings and appends them. Example: strcat(string1, string2); The
result in string1.
s trc p y Accepts two strings and copies the second string to the first string.
Example: strcpy(string1, string2);
s trc m p Accepts two strings. If string1 and string2are the same, this function returns
0. If string2 is alphabetically greater than string1, it returns a negative
number. If string2 is alphabetically less than string1, it returns a positive
number. Example: if (strcmp(string1, string2))
s tr s tr Accepts two strings or pointers to two strings as arguments, searches for the
first occurrence of string2 in string1. If an occurrence of string2 is found, the
function returns a pointer to it. Otherwise, it returns a NULL pointer.
Example Usage: cout << strstr(string1, string2);

C++ Programming, Namiq Sultan 17


Using Pointers to pass String arguments
• Very useful
• Can assume string exists from address pointed to by the
pointer up to the ‘\0’

C++ Programming, Namiq Sultan 18


Program 10-10
// This prog 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()
{
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";
}
C++ Programming, Namiq Sultan 19
// 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;
}

C++ Programming, Namiq Sultan 20


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.

C++ Programming, Namiq Sultan 21

You might also like