Lesson Home

Strings

 

Character and their Cases

 

Character Cases

Each alphabetic character in the English language has two representations: lowercase and uppercase. Characters in lowercase are: a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, and z. Their equivalent characters in uppercase are represented as A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, and Z. Characters used for counting are called numeric characters; each one of them is called a digit. They are 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. There are other characters used to represent things in computer applications, mathematics, and others. Some of these characters, also called symbols are ~ , ! @ # $ % ^ & * ( ) _ + { } ` | = [ ] \ : " ; ' < > ? , . / These characters are used for various reasons and under different circumstances. For example, some of them are used as operators in mathematics or in computer programming. Regardless of whether a character is easily identifiable or not, in C++, all these symbols are character types and can be declared using the char data type followed by a name.

The C/C++ char data type is an 8-bit value. To accommodate for Unicode issues, in Lesson 3, we saw that we could use the __wchar_t data type. To initialize a __wchar_t variable using one of the symbols, we saw that we could type the character between single quotes on the right side of an assignment operator. Here are examples:

using namespace System;

int main()
{
    __wchar_t Gender = 'm';
    __wchar_t MoneySymbol = '$';
    __wchar_t Multiplication = '*';
    __wchar_t NumberOne = '1';

    Console::WriteLine("A few characters");
    Console::WriteLine("Gender:         {0}", Gender);
    Console::WriteLine("MoneySymbol:    {0}", MoneySymbol);
    Console::WriteLine("Multiplication: {0}", Multiplication);
    Console::WriteLine("NumberOne:      {0}", NumberOne);

    Console::WriteLine();
    return 0;
}

This would produce:

A few characters
Gender:         m
MoneySymbol:    $
Multiplication: *
NumberOne:      1

Press any key to continue...

When declaring a variable using __wchar_t, to indicate that the character should be considered as a normal char value, you can precede its declaration with L. To declare the variable on the managed heap, you can create it as a handle. Here are examples:

using namespace System;

int main()
{
    __wchar_t ^ Gender         = L'm';
    __wchar_t ^ MoneySymbol    = L'$';
    __wchar_t ^ Multiplication = L'*';
    __wchar_t ^ NumberOne      = L'1';

    Console::WriteLine("A few characters");
    Console::WriteLine("Gender:         {0}", Gender);
    Console::WriteLine("MoneySymbol:    {0}", MoneySymbol);
    Console::WriteLine("Multiplication: {0}", Multiplication);
    Console::WriteLine("NumberOne:      {0}", NumberOne);

    Console::WriteLine();
    return 0;
}

In the case, when the program terminates, the garbage collector would "clean" the heap memory and delete the variables. Still, if you want, you can delete them yourself using the delete operator. Here are examples:

using namespace System;

int main()
{
    __wchar_t ^ Gender         = gcnew __wchar_t(L'm');
    __wchar_t ^ MoneySymbol    = gcnew __wchar_t(L'$');
    __wchar_t ^ Multiplication = gcnew __wchar_t(L'*');
    __wchar_t ^ NumberOne      = gcnew __wchar_t(L'1');

    Console::WriteLine("A few characters");
    Console::WriteLine("Gender:         {0}", Gender);
    Console::WriteLine("MoneySymbol:    {0}", MoneySymbol);
    Console::WriteLine("Multiplication: {0}", Multiplication);
    Console::WriteLine("NumberOne:      {0}", NumberOne);

    delete Gender;
    delete MoneySymbol;
    delete Multiplication;
    delete NumberOne;

    Console::WriteLine();
    return 0;
}
 

Converting a Character to Uppercase

An alphabetic character, for any reason judged necessary, can be converted from one case to another. The other characters, non-alphabetic symbols, and the numbers, do not have a case and therefore cannot be converted in cases. The C/C++ language provides a function used to convert a character from one case to the other.

To convert a lowercase character to uppercase, you can use the toupper() function that is defined in the std namespace of the iostream library. Its syntax is:

int toupper(int c);

This function simply takes one argument, the character that needs to be converted, c. If the character c is already in uppercase, nothing would change. If the character is a lowercase alphabetic character, the function would return it in uppercase. If the argument is not an alphabetic character, it would be kept “as-is”. Here is an example:

#include <iostream>

using namespace std;
using namespace System;

int main()
{
    __wchar_t gdr = L'm';
    __wchar_t Gender = toupper(gdr);

    Console::WriteLine(L"Gender: {0}", gdr);
    Console::WriteLine(L"Gender: {0}", Gender);

    Console::WriteLine();
    return 0;
}

This would produce:

Gender: m
Gender: M

Press any key to continue

Converting a Character to Lowercase

To convert a character to lowercase, you can use the tolower() function. Its syntax is:

int tolower(int c);

This takes one argument as the character to convert. If the argument c is not an alphabetic character, it would be kept “as-is”. If c is an uppercase alphabetic character, it would be converted to lowercase. If c is in lowercase, it would not be converted.

Validating a Character

Every time the user types a character, the action is referred to as a keystroke. When performing data entry, even if the user presses two keys simultaneously (to enter an uppercase letter or to type a special character), only one character is entered at a time. You can find out what character the user has entered in your application using appropriate functions. Some functions are used to categorize the types of characters on the keyboard. The functions used for these validations are as follows:

Function Meaning
 int isalpha(int c); Returns true if c is an alphabetic character. Otherwise, returns false
 int islower(int c); Returns true if c is an alphabetic character in lowercase. Otherwise, returns false.
 int isupper(int c); Returns true if c is an alphabetic character in uppercase. Otherwise, returns false
 int isdigit(int c); Returns true if c is a digit. Otherwise, returns false
 int isxdigit(int c); Returns true if c is a hexadecimal digit. c must be one of the following 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f, A, B, C, D, E, or F. Otherwise, returns false.
 int isalnum(int c); Returns true is c is between 0 and 9 or if c is between a and z or if c is between A and Z. Otherwise, returns false.
 int isascii(int c); Returns true if c is an ASCII character. Otherwise, returns false
 int ispunct(int c); Returns true if c is a punctuation character. Otherwise, returns false.
 int isprint(int c); Returns true if c is a printable character. Otherwise, returns false.
 int isgraph(int c); Returns true if c is a printable character and is not an empty space.
 int isspace(int c); Returns true if c is an empty space
 

Introduction to Strings

 

The String as a Series of Characters

A string is a group of characters with the last character being "\0". A typical string, such as Pacifique, can be graphically represented as follows:

P a c i f i q u e \0

The last character "\0" is called the null-terminating character. For this reason, a string is said to be null-terminated. The C/C++ language ships with many functions that allow you to perform various types of operations on null-terminated strings.

String Declaration With the string Class

The C++ and its parent the C languages don't have a string data type. One of the solutions you can use consists of declaring a pointer to char, Char, or __wchar_t. You can then initialize such a variable using the assignment operator. Here are examples:

char *city = "Buenos Aires";

When declaring a string variable using __wchar_t, to consider the string as a pointer to char, you can precede it with L as follows:

__wchar_t *sentence = L"Managed C++ Programming";

Such a variable can be passed to the Write() or the WriteLine() methods of the Console class to display on the screen.

C++ uses a library called Standard Template Library (STL) to solve some of the string issues of a C++ program. To declare a string in C++, you can use the string class. Here is an example:

string bookTitle;

To initialize a string variable, you can assign it a word or sentence between double-quotes. Here is an example:

string bookTitle = "C++ Programming";

The string class also provides various constructors used to declare it. One of these constructors allows you to initialize a string variable using parentheses. You can use this constructor to declare and initialize the above string variable as follows:

string bookTitle("C++ Programming");
 

Introduction to the String Class

AS we saw in Lesson 7, so support strings, the C++/CLI language uses the String class. Based on this, to declare a string using the String class, use the ^ operator. Here is an example:

String ^ sentence;

If you declare a String variable without initializing it, its memory space is made empty. Therefore, before using the variable, you should initialize it. The String class provides three main options to initialize a String variable. One of the techniques you can use involves a constructor and the String class provides many of them. After initializing the variable, you can pass it to the Write() or WriteLine() method of the Console class. Here is an example:

using namespace System;

int main()
{
    String ^ bookTitle = L"C++/CLI Programming";

    Console::Write("Book Title: {0}", bookTitle);

    Console::WriteLine();
    return 0;
}

As we saw in previous lessons, when initializing a String variable or assigning a value to it, you can precede the opening double-quote with L to indicate that the string will be converted to a regular letter-based string. 

The Length of a String

In many operations, you will need to know the number of characters a string consists of. To get the size of a string, The String class provides the Length member variable.

Copying a String 

 

Introduction

After declaring and initializing a String, one way you can get another string is to make a copy of the first and store it in another string. To support this operation, the String class is equipped with the Copy() method. Its syntax is:

public:
    static String^ Copy(String^ str);

This function takes as argument an existing String object that holds a value. To use it, call it as a static method.

Copying a Few Characters

The String::Copy() method is used to copy all characters of one string into another another. If you want to copy only a few characters, use the String::CopyTo() method. Its syntax is:

public:
    void CopyTo (
	int sourceIndex, 
	array<wchar_t>^ destination, 
	int destinationIndex, 
    	int count);
 

String Cases

 

Conversion to Uppercase

As mentioned previously, a letter is considered in lowercase it is a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y,or z. If you have a string with letters, to assist you with converting to uppercase the letters that are in lowercase, the String class provide the ToUpper() method. It is overloaded in two versions. The simplest uses the following syntax:

public:
    String^ ToUpper ()

When a String variable calls this method, each lowercase character would be converted to uppercase. Any character or symbol that is not in lowercase would not be changed. Here is an example:

using namespace System;

int main()
{
    String ^ Country = L"eau";

    Console::WriteLine("Country: {0}", Country->ToUpper());

    Console::WriteLine();
    return 0;
}

This would produce:

Country: EAU

Press any key to continue . . .

Conversion to Lowercase

To convert a string to lowercase, the String class is equipped with the ToLower() method. It is overloaded with two versions. The syntax of the simplest one is:

public:
    String^ ToLower ()

This function considers the characters of the variable that called it. Each letter in uppercase would be converted to lowercase. Each letter is lowercase and each non-alphabetical character would remain "as is". Here is an example:

using namespace System;

int main()
{
    String ^ FirstName = L"Leonard", ^ LastName = L"Hoard";

    Console::WriteLine("Email Address: {0}{1}@mailer.com",
			FirstName->ToLower(), LastName->ToLower());

    Console::WriteLine();
    return 0;
}

This would produce:

Email Address: leonardhoard@mailer.com

Press any key to continue . . .
 

Strings Comparisons

 

Introduction

String comparison consists of examining the characters of two strings with a character of one string compared to a character of the other string with both characters at the same positions. To support this operation, the String class is equipped with the Compare() method that is overloaded in many versions. One of the versions uses the following syntax:

public:
    static int Compare(String^ String1, String^ String2);

Notice that this method is declared static and it takes two arguments. When it starts, the first character if the first argument is compared to the first character of the second string. Alphabetically, if the first character of the first string has a lower alphabetical index than the first character of the second, this method returns a negative value. If the first character of the first string has a higher alphabetical index than the first character of the second, this method returns a positive value. If the first characters of both strings are the same, the method continues with the second character of each string. If both string have the exact same characters, the method returns 0. This can be resumed as follows. The method returns

Here is an example:

using namespace System;

int main()
{
    String ^ FirstName1 = "Andy";
    String ^ LastName1  = "Stanley";
    String ^ FirstName2 = "Charles";
    String ^ LastName2  = "Stanley";

	int Value1 = String::Compare(FirstName1, FirstName2);
    int Value2 = String::Compare(FirstName2, FirstName1);
    int Value3 = String::Compare(LastName1, LastName2);

    Console::Write("The result of comparing ");
    Console::Write(FirstName1);
    Console::Write(" and ");
    Console::Write(FirstName2);
    Console::Write(" is\t");
    Console::WriteLine(Value1);
    Console::Write("The result of comparing ");
    Console::Write(FirstName2);
    Console::Write(" and ");
    Console::Write(FirstName1);
    Console::Write(" is\t");
    Console::WriteLine(Value2);
    Console::Write("The result of comparing ");
    Console::Write(LastName1);
    Console::Write(" and ");
    Console::Write(LastName2);
    Console::Write(" is\t");
    Console::WriteLine(Value3);
    Console::WriteLine();
    return 0;
}

This would produce:

The result of comparing Andy and Charles is     -1
The result of comparing Charles and Andy is     1
The result of comparing Stanley and Stanley is  0

Press any key to continue...

When using this version of the String::Compare() method, the case (upper or lower) of each character is considered. If you don't want to consider this factor, the String class proposes another version of the method. Its syntax is:

public:
    static int Compare(String^ String1, String^ String2, bool ignoreCase);

The third argument allows you to ignore the case of the characters when performing the comparison.

 

String Equality

In the previous section, we saw that the indexed-equivalent characters of two strings can be compared to know whether one is lower or higher than the other's. If you are only interested to know whether two strings are equivalent, you can call the Equals() method of the String class. It is overloaded with various versions. Two versions use the following syntaxes:

public:
    virtual bool Equals(Object^ obj);
    virtual bool Equals(String^ value);

To use one of these versions, use an Object object or a String variable that calls it. The method takes one argument. The variable that calls the method is compared to the value passed as argument. If both values are the exact same, the method returns true. The comparison is performed considering the case of each character. If you don't want to consider the case, use the following version of the method:

public:
    bool Equals(String^ value, StringComparison comparisonType);

An alternative to the second syntax is to use a static version of this method whose syntax is:

public:
    static bool Equals(String^ a, String^ b);

This method takes two String arguments and compares them. If they are the same, the method returns true. This method considers the cases of the characters. If you don't want this factor taken into consideration, use the following version of the method:

public:
    static bool Equals(String^ a, String^ b, StringComparison comparisonType);

String Concatenation

 

Introduction

String concatenation consists of adding one string to another. To perform this operation, you can use the addition operator. Here is an example:

using namespace System;

int main()
{
    String ^ FirstName = "Andy ";
    String ^ LastName  = "Stanley";

    String ^ FullName = FirstName + FirstName;
    
    Console::WriteLine(L"First Name: {0}", FirstName);
    Console::WriteLine(L"Last Name:  {0}", LastName);
    Console::WriteLine(L"Full Name:  {0}", FullName);

    Console::WriteLine();
    return 0;
}

Concatenating a String

The String class provides its own mechanism to concatenate two or more strings. To support this operation, the class is equipped with the Concat() method that is overloaded with various versions. The syntax of one these versions is:

static String^ Concat(String^ str0, String^ str1);

This static method takes two arguments that would be added. During the operation, the first argument is added to the second. If the concatenation is successful, the method returns a new string. Here is an example:

using namespace System;

int main()
{
    String ^ FirstName = "Andy ";
    String ^ LastName  = "Stanley";

    String ^ FullName = String::Concat(FirstName, FirstName);
    
    Console::WriteLine(L"First Name: {0}", FirstName);
    Console::WriteLine(L"Last Name:  {0}", LastName);
    Console::WriteLine(L"Full Name:  {0}", FullName);

    Console::WriteLine();
    return 0;
}

This would produce:

First Name: Andy
Last Name:  Stanley
Full Name:  Andy Andy

Press any key to continue . . .

Working With Sub-Strings

 

Introduction

A sub-string is a section or part of a string. To create a sub-string, you first need a string and can retrieve one or more values from it. To support this, the String class is equipped with the Substring() method that is overloaded in two versions. The syntax of one is:

public:
    String^ Substring(int startIndex);

The integer argument specifies the position of the first character from the variable that called the method. The return value is a new String that is made of the characters from startIndex to the end of the string.

Sub-String Creation

Probably the most consistent way to create a string is to control the beginning and end of the original string. To support this, the String class is equipped with another version of the Substring() method. Its syntax is:

public:
    String^ Substring(int startIndex, int length);

The first argument specifies the index of the character to start from the String variable that calls this method. The second argument specifies the length of the string.


Previous Copyright © 2006 FunctionX, Inc. Next