C++ Characters, Strings, and Associated Functions

Characters

Compare single characters directly. e.g.

	char inChar;
	cin >> inChar;

	if ( inChar =='a') 
	...
If you want to test for upper or lower case, it is better to use one of the cctype functions. i.e. Instead of if (inChar >= 'a' && inChar <= 'z') it is better to say if (islower(inChar)) The following table shows some of the cctype functions that you can use.

Function Integer Value Returned
isalpha(ch) A non-zero value if ch is a letter, otherwise: 0
isalnum(ch) A non-zero value if ch is an alpha-numeric, otherwise: 0
isdigit(ch) A non-zero value if ch is a digit 0-9, otherwise: 0
islower(ch) A non-zero value if ch is a lower-case, otherwise: 0
isupper(ch) A non-zero value if ch is a upper-case, otherwise: 0
ispace(ch)A non-zero value if ch is a space, otherwise: 0
toupper(ch) If ch is a lower-case character it is converted to upper-case. Otherwise ch remains as it was before the call.
tolower(ch) If ch is an upper-case character it is converted to lower-case. Otherwise ch remains as it was before the call.

Here is a little program you can experiment with to explore these functions.

/*  Filename:   ~ftp/pub/class/cplusplus/Csyntax-files/Cpgms/cctypes.cpp
    Purpose:    To explore the cctypes functions for characters
*/
#include <iostream>
using namespace std;
int main()
{
    char ch;

    cout << endl << "This program experiments with cctype functions."
         << endl;

    cout << "First function to be examined is: isalpha(ch)" << endl;
    ch = 'A';
    cout << "isalpha(ch) for 'A' = " << isalpha(ch) << endl;
    ch = '$';
    cout << "isalpha(ch) for '$' = " << isalpha(ch) << endl;
    cout << "Enter a value to check or a '+' to move on: ";
    cin >> ch;
    cout << endl;
    while ( ch != '+')
         {
         cout << "isalpha(ch) for " << ch << " = " << isalpha(ch) << endl;
         cout << "Enter a value to check or a '+' to move on: ";
         cin >> ch;
         cout << endl;
         }

} // end main

Character Arrays

A string, represented as a character array, is called a C String.
To declare a C string: char MyString[8];
To declare and initialize a C string: char MyString[8] "a word";
Or just: char MyString[] "a word";
To assign a value to a C string: strcpy(MyString, "new word");
To get the length of a C string: cout << strlen(MyString);
To convert a C string into its numeric integer equivalent:
( aSCII characters to integer value)
cout << atoi(MyString);
To convert a C string into its numeric float equivalent:
( aSCII characters to float value)
cout << atof(MyString);

STL Strings

There are some disadvantages to using characters arrays for strings but that was all there was available in 'ordinary' C. However, in C++, we have the STL String Class Library. Because it is a class, you need to add this statement to your program to use strings: #include <string>. Here are a few functions.
To declare an STL string: string MyString;
To declare and initialize an STL string: string MyString("a word");
To assign a value to an STL string: MyString = "a word";
Note: You could not do a simple assignment like this with character arrays. You had to use the strcpy function.
To assign a different value to an STL string: MyString = "a much longer string";
Note: With character arrays you were restricted by the size of the array.
To join two or more STL strings: string MyString1 = "first word";
string MyString2 = "first word";
string MyLongString;
MyLongString = MyString1 + MyString2;

Note: You couldn't do this with character arrays.
To look at individual characters in an STL string:
(Using square brackets for subscripts like an array.)
cout << "First letter is: " << MyString[0];
To find the length of an STL string: cout << "Length is " << MyString.length();
To insert a string into another STL string: MyString.insert(starting_position, other_string);
To erase part of an STL string: MyString.erase(starting_position, number_of_characters);
To replace part of an STL string: MyString.replace(starting_position, number_of_characters_to_replace, replacement_string);
To convert an STL string into a C string:
You need to convert for some existing functions.
See next two lines of this table.
MyString.c_str();
To use an STL string in the atoi() function: atoi(MyString.c_str());
To use an STL string in the atof() function: atof(MyString.c_str());
To use an STL string in the file open() function: file_identifier.open(MyString.c_str());

This page has been accessed     times.
Last modified: Friday, 21-Aug-2020 15:28:13 CST
Copyright 2002 Department of Computer Science, University of Regina.


 CS Dept Home Page

Teaching Material