Using and Formatting Strings


You have already seen several different variable types and we have used them for input and output. There is another type of variable we have not used yet. This is the string variable. A string is essentially a bunch of characters all together that you can use to store text. Up to this point we simply used an array of characters. You can still do this, but now you have other options. Now, C++ has a string variable that you can use. To use the string variable you will have to include the string header file. Here is an example.

Example 3.6

Step 1: Enter the following code into your favorite text editor.

#include <iostream> #include <string> using namespace std ; using std::cout;   using std::cin; int main() {  string s = "C++ is sooo coool! \n";  cout <<s;   return 0; }

Step 2: Compile and run the program. You will see something similar to what is shown in Figure 3.5.

click to expand
Figure 3.5: The C++ string.

When you run this you will see how the string variable works. Essentially it is an open-ended character array you can use to store string data.

With C++, your string functions are defined in the string header file. Each string is an object that has methods and properties that you can use. Objects and classes will be covered in Chapter 10. For now, remember that an object is a special type of variable that has functions associated with it. These functions are called methods. The length property is a commonly used property of strings, which is easy to implement and understand.

Example 3.7

Step 1:

#include <iostream> #include <string>     // include for C++ standard string  // class using namespace std; int main() {      string stringA = "C++"; string stringB = "Is Cool"; cout << "Length of stringA = " << stringA.length() <<     endl; cout << "Length of stringB = " << stringB.length() <<     endl;  return 0; }

Step 2: When you run this code you should see something similar to what is shown in Figure 3.6.

click to expand
Figure 3.6: String operations.

You can see that, in C++, the string is an object with properties and methods. There are a number of properties and methods with this object; the most commonly used are summarized in Table 3.2.

Table 3.2: Properties and Methods of the String Class

Property/Method

Purpose

Length

Property that returns the length of the string.

Copy

This copies from one string to another.

Erase

This method erases the string.

Insert

This inserts a set of characters into a string.

Most C++ compilers still support C header files, and C functions. These C-style string functions are still used frequently. Therefore, a summary of some of these functions and an example are included here. The more important and commonly used functions from string.h are summarized in Table 3.3.

Table 3.3: String Functions

Function

Purpose

Format

strcpy

To copy one string to another.

char * strcpy (char * dest, const char * src);

Note: The * is a special operator that will be explained in detail in the chapter on pointers.

strlen

To return the length of a string.

size_t strlen ( const char * string);

strncat

Append a sub string onto another string.

char * strncat (char * dest, const char * src, sizet_t num);

strstr

Find one string inside of another string.

char * strstr (const char * string1, const char * string2);

There are several other functions in the string header file, but these are the most commonly used functions. The following example illustrates the use of these functions.

Example 3.8

Step 1:

#include <iostream> #include <string > using std::cout;   using std::cin;   int main()  {  char firststring[40],secondstring[40],thirdstring[40];  int size;  cout << "Please enter a word \n";  cin.getline(firststring,40);   cout << "Please enter another word  \n"; cin.getline(secondstring,40);  size = strlen(firststring);         strcat(firststring,secondstring);  cout << "The length of the first string you entered  is" << size << "\n";  cout << "Both strings you entered are " <<   thirdstring<< "\n";  return 0; }

This example, although simple, should show you how to use some of the functions you will find in the string header file.

Hint!

The previous example is using C-styled string manipulations. This is actually supported by many of the commonly used C++ compilers.




C++ Programming Fundamentals
C++ Programming Fundamentals (Cyberrookies)
ISBN: 1584502371
EAN: 2147483647
Year: 2005
Pages: 197
Authors: Chuck Easttom

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net