Strings and String Functions

(BICS 265 Notes.9)

 

 

Learn how to use common ANSI string functions in C++...

 

 

Many business-oriented programs need to manipulate strings. For example, data such as customer nameemployee address, and part description would all be recorded and processed in string form.

Because C++ has no string data type, programmers are forced to manipulate character arrays containing the strings to be processed.

Over time, the software industry has simplified string processing by developing a set of standardized external functions - the American National Standards Institute (ANSI) string functions.

The ANSI string functions solve many common string processing problems...

 

 

Common ANSI String Functions

 

 

strlen()

Determines the length of a string within an array. Because a string is delimited by a NULL character, its length differs from the array size.

 

strcmp()

Compares the contents of two arrays containing strings. This function solves the problem of trying to use an array name in a relational expression:

 

char Buffer[40];

  :

if(Buffer == "FSU")     // ERROR!

 

strcpy()

Copies the string in one array to another array.  This function solves the problem of trying to use an array name in an assignment statement:

 

char Buffer[40];

  :

Buffer = "FSU";        // ERROR!

 

strcat()

Concatenates (appends) one string to another. This makes it easy to form one large string from two small ones.

 

Notes

  1. All string functions begin with the letters str

  2. To use string functions, you must include the header file string.h

  3. There are several other string functions that require the use of pointers. They will be covered in a future lesson.

 

"If you take BICS 365 - Advanced C++ Programming, you will learn about the string class.

It makes string processing even easier..."

 

 

The easiest string function to use is strlen( ).

It requires a single parameter (the name of the array containing the string) and returns an integer indicating the number of characters in the string (not counting the string delimiter).

Don't confuse string length with array size. They are different.

 

Example

 

#include <string.h>

  :

char Buffer[10] = "BICS365";

  :

cout << "Array size: " << sizeof(Buffer) << endl;

cout << "String size: " << strlen(Buffer);

 

Note

The output displayed by the above code would indicate an array size of   10  and a string size of  7   because the array would contain the following:

 

B

I

C

S

3

6

5

\x0

?

?

 

strlen( ) is more useful determining the size of a variable length string entered by the user - as shown in the following example:

 

char Buffer[128];

  :

cin.getline(Buffer, sizeof(Buffer));

cout << strlen(Buffer) << " characters entered";

 

Note

To be properly delimited, the largest string the user could enter would be 127 characters long. The actual number of characters they entered would be displayed by the cout statement...

 

A common requirement in a program is to compare the contents of two strings. In C++, this can be done with the ANSI strcmp( ) function...

 

 

Comparing Two Strings with strcmp( )

 

 
  • Two arrays (containing valid strings) are passed to the function

  • The function compares corresponding characters (from left to right)

  • The function stops on an inequality or the end of either string

  • An integer is returned to indicate the result of the comparison

 

Result

Meaning

< 0

String one comes first alphabetically

> 0

String two comes first alphabetically

   0

The strings are identical

 

 

Example

(Testing a user's entry for the string "EXIT")

 

char Buffer[40];

  :

cin.getline(Buffer, sizeof(Buffer));

  :

if( strcmp(Buffer, "EXIT") == 0)

{

    return 0;

}

 

Notes

  1. strcmp( ) is typically called inside a relational expression that tests its returned value.

  2. In this example, the second string is a string constant. It could have been a second character array containing a variable string.

  3. The comparison is case sensitive. If the user types "Exit" or "exit", the returned value would not be zero. There is a variation on strcmp( ) named stricmp( ) that ignores case. It temporarily translates everything to uppercase before doing the compare...

 

 

To copy a string from one character array to another, use the strcpy( ) function...

 

 

Copying a String with strcpy( )

 

 
  • The function is passed two character arrays as follows:

 

strcpy(target-array, source-array)

 

  • The string in source-array is copied into target-array (overlaying any string currently in the array)

 

Example

(Copying "FSU" to an array named Message)

 

strcpy(Message, "FSU");

 

Notes

  1. In this example, the source array is a string constant. It could have been a character array containing a variable string.

  2. It is the programmer's responsibility to guarantee that the target array is large enough to hold the string being copied. If it is too small, the copy will stop when the target array is full - and it will not contain a string delimiter...

 

 

Often, it is necessary to concatenate (append) two or more strings to create a longer string.

Think of concatenation as sticking one string on the end of another. For example, a person's last name string may be appended to their first name string to construct their full name.

The strcat( ) function is fairly easy to use. It requires two parameters that are both character arrays containing valid strings. The string in the second parameter will be concatenated to the string in the first parameter (overlaying its delimiter). The result is a longer string in the first parameter array.

 

Example

The following instructions will append a person's last name to their first name. Assume First and Last are character arrays declared as indicated and containing the data shown:

 

char First[20];

 

M a r y \x0 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

 

char Last[10];

 

J o n e s \x0 ? ? ? ?

 

strcat(First, " ");      // Append space

strcat(First, Last);     // Append last name

 

Notes

  1. After the first strcat( ), First would contain

 

M a r y   \x0 ? ? ? ? ? ? ? ? ? ? ? ? ? ?

 

  1. After the second strcat( ), First would contain

 

M a r y   J o n e s \x0 ? ? ? ? ? ? ? ? ?

 

  1. The second parameter in a call to strcat( ) may be a string constant (but the first parameter may not).

  2. It is the programmer's responsibility to declare the first parameter array large enough to hold the concatenated result. If it isn't big enough, strcat( ) will stop when the array is full - but will not provide a string delimiter!

  3. strcat( ) returns the beginning address of the first parameter array. This makes "cascading" possible. The two strcat( ) function calls could have been nested as follows:

strcat(strcat(First, " "), Last);

 

 

Some additional ANSI string functions will be introduced in a few lessons (after you've learned about pointers)...

 

Sample Program

 

Programming Exercise