(BICS 265 Notes.9)
|
|
| Many business-oriented programs need to manipulate strings. For
example, data such as customer name, employee
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
|
Notes
|
| 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
Note
strlen( ) is more useful determining the size of a variable length string entered by the user - as shown in the following example:
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( )
|
||||||||
Example (Testing a user's entry for the string "EXIT")
Notes
|
| To copy a string from one character array to another, use the strcpy( ) function... |
Copying a String with strcpy( )
|
Example (Copying "FSU" to an array named Message)
Notes
|
| 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
Notes
strcat(strcat(First, " "), Last);
|
| Some additional ANSI string functions will be introduced in a few lessons (after you've learned about pointers)... |