OOPortal OOPortal
Programming C++  «Prev  Next»
Lesson 7 String functions
Objective Explore some of the functions in the cstring package.

C++ String Functions

The C and C++ community has "agreed" to treat the type char* as a form of string type. The understanding is that these strings will be terminated by the char value zero, and that the cstring package of functions will be called on this abstraction.
The language partly supports this abstraction by defining string literals as being null-terminated. A char* or char[] can be initialized with a literal string. Note that the terminating zero is part of the initializer list:

char* s = "c++"; // s[0] = 'c', s[1] = '+',
                 // s[2] = '+', s[3] = '\0';

Functions in cstring

The cstring package contains more than 20 functions, including:
  1. size_t strlen(const char* s);
    

    Computes the string length. The number of characters before the null terminator 0 is returned.
  2. char* strcpy(char* s1,  const char* s2);
    

    Copies the string s2 into s1. The value of s1 is returned.
  3. int strcmp(const char* s1,  const char* s2);
    
    Returns an integer that reflects the lexicographic comparison of s1 and s2. When the strings are the same, zero is returned. When s1 is less than s2, a negative integer is returned. When s2 is less than s1, a positive integer is returned.

By adhering to the above conventions, you will be able to reuse a lot of string code.
The library routines ensure that portable, readily understood code is available.

Functions for Classifying Characters

Function Operation

isupper(int c) Tests whether or not c is an uppercase letter, by default 'A' to 'Z'.
islower(int c) Tests whether or not c is a lowercase letter, by default 'a' to 'z'.
isalpha(int c) Tests whether or not c is an upper- or lowercase letter.
isdigit(int c) Tests whether or not c is a digit, 0 to 9.
isxdigit(int c) Tests whether or not c is a hexadecimal digit, 0 to 9, 'a' to 'f', or 'A' to 'F'.
isalnum(int c) Tests whether or not c is a letter or a digit (i.e., an alphanumeric character).
isspace(int c) Tests whether or not c is whitespace, which can be a space, a newline, a carriage return, a
form feed, or a horizontal or vertical tab.
iscntrl(int c) Tests whether or not c is a control character.
isprint(int c) Tests whether or not c is a printable character, which can be an upper- or lowercase letter,
a digit, a punctuation character, or a space.
isgraph(int c) Tests whether or not c is a graphic character, which is any printable character other than
a space.
ispunct(int c) Tests whether or not c is a punctuation character, which is any printable character that is not
a letter or a digit. This will be either a space or one of the following:
_ { } [ ] # ( ) < > % : ; . ? * + - / ^ & | ~ ! = , \ " '.

Each of these functions returns a value of type int. The value will be non-zero (true) if the character is of the type being tested for, and 0 (false) if it is not. You may be wondering why these functions do not return a bool value, which would make much more sense. The reason they do not return a bool value is that they originate from the C standard library and predate type bool in C++. The locale header provides the two functions shown in the table above for converting between upper and lowercase characters. The result will be returned as type int so you need to explicitly cast it if you want to store it as type char for instance.

String Equality - Exercise

Click the Exercise link below to write a function that implements a string equality test.
String Equality - Exercise