11.7 String Processing


11.7 String Processing

In addition to standard string operations such as adding two strings together, erasing characters, and replacing them, a programmer needs to know how strings can be changed and processed manually. For example, given a std::string object, you may be asked to reverse the string, so "hello" becomes "olleh". Since there is no reverse method of the std::string class, the string must be reversed manually. The following sections explore how strings can be processed and details how a string can be reversed.

11.7.1 Cycling through a String

One of the simplest things a programmer may do with a std::string is cycle through every letter of the string and print each letter on a new line. This in itself is not especially useful, but doing so demonstrates how loops can be used to easily process strings. Consider the following code:

      #include <iostream>      int main()      {         std::string Str = "hello"; //Assign hello to string         for(int counter=0; counter < Str.length(); counter++)         {            std::string letter = Str.substr(counter, 1);            std::cout<<letter + "\n";         }         return 0;      } 

The above code creates a For loop that is set to repeat the number of times equal to the number of letters in the std::string object. Beginning at the first letter, the substr method is used to extract a substring of one letter on each iteration of the loop. The first letter will be "h", then "e", then "l", and so on until the word "hello" is complete.

11.7.2 Cycling Backward through a String

Cycling backward through a string may also seem a trivial thing to do with a string, but the process demonstrates how powerful loops are in processing strings and most other array forms of data. This time, the idea is to cycle through a string and print each letter on a new line, but now the string will be processed backward, beginning at the last letter.

      #include <iostream>      int main()      {         std::string Str = "hello"; //Assign hello to string         for(int counter=Str.length(); counter > 0; counter--)         {            std::string letter = Str.substr(counter-1, 1);            std::cout<<letter + "\n";         }         return 0;      } 

11.7.3 Reversing a String

The process of taking a string object and returning a new string that is the reverse of the original is simple now that the string can be cycled in reverse. The idea is to loop through the letters in reverse and on each iteration add each letter to a new string such that, as the loop completes, the new string will be exactly the reverse of the original. The following code demonstrates how to do this.

      #include <iostream>      int main()      {         std::string Str = "hello"; //Assign hello to string         std::string reverseStr = ""; //The reverse string            for(int counter=Str.length(); counter > 0; counter--)            {                reverseStr += Str.substr(counter-1, 1);            }         std::cout<<reverseStr;         return 0;      } 




Introduction to Game Programming with C++
Introduction to Game Programming with C++ (Wordware Game Developers Library)
ISBN: 1598220322
EAN: 2147483647
Year: 2007
Pages: 225
Authors: Alan Thorn

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