Character Testing and String Manipulation in C++ - Prof. Curtis E. Dyreson, Study notes of Computer Science

An overview of character testing functions in c++ and their meanings. It also covers character case conversion and the internal storage of c-strings. Furthermore, it introduces library functions for working with c-strings and the c++ string class, which offers advantages over c-strings. Examples and explanations of various functions and operators.

Typology: Study notes

Pre 2010

Uploaded on 07/31/2009

koofers-user-rn2-1
koofers-user-rn2-1 🇺🇸

9 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 1400
Chapter 10
10.1 Character Testing
There are several functions that answer
questions about characters
Each has one char parameter
Each returns true or false
Character Testing
require cctype header file ?
FUNCTION MEANING
isalpha true if arg. is a letter, false otherwise
isalnum true if arg. is a letter or digit, false otherwise
isdigit true if arg. is a digit 0-9, false otherwise
islower true if arg. is lowercase letter, false otherwise
isprint true if arg. is a printable character, false otherwise
ispunct true if arg. is a punctuation character, false otherwise
isupper true if arg. is an uppercase letter, false otherwise
isspace true if arg. is a whitespace character, false otherwise
Example
10.2 Character Case Conversion
Require cctype header file.?
Functions:
toupper: if char argument is lowercase letter,
return uppercase equivalent; otherwise, return input
unchanged
char ch1 = 'H';
char ch2 = 'e';
char ch3 = '!';
char c1 = toupper(ch1); // assigns 'H'
char c2 = toupper(ch2); // assigns 'E'
char c3 = toupper(ch3); // assigns '!'
Character Case Conversion
Functions:
tolower: if char argument is uppercase letter, return
lowercase equivalent; otherwise, return input
unchanged
char ch1 = 'H';
char ch2 = 'e';
char ch3 = '!';
char c1 = toupper(ch1); // assigns h'
char c2 = toupper(ch2); // assigns e'
char c3 = toupper(ch3); // assigns '!'
pf3
pf4
pf5

Partial preview of the text

Download Character Testing and String Manipulation in C++ - Prof. Curtis E. Dyreson and more Study notes Computer Science in PDF only on Docsity!

CS 1400

Chapter 10

10.1 Character Testing

• There are several functions that answer

questions about characters

• Each has one char parameter

• Each returns true or false

Character Testing

  • require cctype header file?

FUNCTION MEANING

isalpha true if arg. is a letter, false otherwise isalnum true if arg. is a letter or digit, false otherwise isdigit true if arg. is a digit 0-9, false otherwise islower true if arg. is lowercase letter, false otherwise isprint true if arg. is a printable character, false otherwise ispunct true if arg. is a punctuation character, false otherwise isupper (^) true if arg. is an uppercase letter, false otherwise isspace true if arg. is a whitespace character, false otherwise

Example

10.2 Character Case Conversion

  • Require cctype header file.?
  • Functions: toupper: if char argument is lowercase letter, return uppercase equivalent; otherwise, return input unchanged char ch1 = 'H'; char ch2 = 'e'; char ch3 = '!'; char c1 = toupper(ch1); // assigns 'H' char c2 = toupper(ch2); // assigns 'E' char c3 = toupper(ch3); // assigns '!'

Character Case Conversion

  • Functions: tolower: if char argument is uppercase letter, return lowercase equivalent; otherwise, return input unchanged char ch1 = 'H'; char ch2 = 'e'; char ch3 = '!'; char c1 = toupper(ch1); // assigns ‘h' char c2 = toupper(ch2); // assigns ‘e' char c3 = toupper(ch3); // assigns '!'

10.3 Review of the Internal

Storage of C-Strings

  • C-string: sequence of characters stored in

adjacent memory locations and terminated by

NULL character

  • String literal (string constant): sequence of

characters enclosed in double quotes " " :

"Hi there!"

H i t h e r e! \

Review of the Internal

Storage of C-Strings

  • Array of chars can be used to define storage for string: const int SIZE = 20; char city[SIZE];
  • Leave room for NULL at end
  • Can enter a value using cin or >>
    • Input is whitespace-terminated
    • No check to see if enough space
  • For input containing whitespace, and to control amount of input, use cin.getline()
  • can write entire array using cout

cout<<city;

Review of the Internal Storage of

C-Strings

10.4 Library Functions for

Working with C-Strings

  • These functions require c-strings as parameters (actually the address of a c-string.) This can be accomplished by - sending the name of an array holding a c-string - a char pointer (we'll learn about these later) - a string constant
  • Some of these c-string functions lead to potential security hazards and have been deprecated.
  • These will produce a warning in the Visual Studio environment
  • Although the textbook uses the deprecated versions of these functions, we will use the newer, more secure versions

Library Functions for

Working with C-Strings

Functions:

  • strlen(str): returns length of C-string str char city[SIZE] = "Missoula"; cout << strlen(city); // prints 8
  • strcat_s(str1, str2): appends str2 to the end of str const int SIZE = 100; char location[SIZE] = "Missoula, "; char state[3] = "MT"; strcat_s(location, state); // location now has "Missoula, MT"

Library Functions for

Working with C-Strings

Functions:

  • strcpy_s(str1, str2): copies str2 to

str

const int SIZE = 100; char fname[SIZE] = "Maureen", name[SIZE]; strcpy_s(name,fname);

Input into a string Object

• Use getline function to put a line of

input, possibly including spaces, into a

string:

string address;

cout << "Enter your address: ";

getline(cin,address);

string Comparison

  • Can use relational operators directly to compare string objects: string str1 = "George", str2 = "Georgia"; if (str1 < str2) cout << str1 << " is less than " << str2;
  • Comparison is performed similar to strcmp function. Result is true or false

Example strings

• It also works with the + operator

• The + operator performs concatenation:

fullName=first + " " + last;

• You can use the array operator [] to

access a single a character.

title="Ice Age";

cout<<title[4];//prints A

Other Definitions of C++ strings

  • There are multiple ways to define a string

Definition Meaning string name; defines an empty string object string myname("Chris"); (^) defines a string and initializes it string yourname(myname);

defines a string and initializes it string aname(myname, 3);

defines a string and initializes it with first 3 characters of myname string verb(myname,3,2);

defines a string and initializes it with 2 characters from myname starting at position 3 string noname('A', 5); (^) defines string and initializes it to 5 'A's

string Operators

OPERATOR MEANING

extracts characters from stream up to whitespace, insert into string << inserts string into stream = assigns string on right to string object on left += (^) appends string on right to end of contents on left

  • (^) concatenates two strings [] (^) references character in string using array notation

, >=, <, <=, ==, !=

relational operators for string comparison. Return true or false

string Operators

string word1, phrase; string word2 = " Dog"; cin >> word1; // user enters "Hot Tamale" // word1 has "Hot" phrase = word1 + word2; // phrase has // "Hot Dog" phrase += " on a bun"; for (int i = 0; i < 16; i++) cout << phrase[i]; // displays // "Hot Dog on a bun"

string Member Functions

  • Are behind many overloaded operators
  • Categories:
    • assignment: assign, copy, data
    • modification: append, clear, erase, insert, replace, swap
    • space management: capacity, empty, length, resize, size
    • substrings: find, substr
    • comparison: compare
  • See Table 10-7 for a list of functions

string Member Functions

string word1, word2, phrase; cin >> word1; // word1 is "Hot" word2.assign(" Dog"); phrase.append(word1); phrase.append(word2); // phrase has "Hot Dog" phrase.append(" with mustard relish", 13); // phrase has "Hot Dog with mustard" phrase.insert(8, "on a bun "); cout << phrase << endl; // displays // "Hot Dog on a bun with mustard"

Arrays of strings

• These are 1-D arrays:

string titles[size];