Strings


There are some problems with C-strings as they are normally used in C++. For one thing, you can’t use the perfectly reasonable expression strDest = strSrc; to set one string equal to another. (In some languages, like BASIC, this is perfectly all right.) The Standard C++ string class we’ll examine in the next section will take care of this problem, but for the moment let’s see if we can use object-oriented technology to solve the problem ourselves. Creating our own string class will give us an insight into representing strings as objects of a class, which will illuminate the operation of the Standard C++ string class.
Let's move ahead

Operations of String


 s1.erase(0, 7);               //remove “Quick! “ 
   s1.replace(9, 5, s2);         //replace “Count” with “Lord”
   s1.replace(0, 1, “s”);        //replace ‘S’ with ‘s’
   s1.insert(0, s3);             //insert “Don’t “ at beginning 
   s1.erase(s1.size()-1, 1);     //remove ‘.’ s1.append(3, ‘!’);  

              and many more......
         

Array


Like other variables in C++, an array must be defined before it can be used to store information. And, like other definitions, an array definition specifies a variable type and a name. But it includes another feature: a size. The size specifies how many data items the array will contain. It immediately follows the name, and is surrounded by square brackets. Figure 7.1 shows the syntax of an array definition. In the REPLAY example, the array is type int. The name of the array comes next, followed immediately by an opening bracket, the array size, and a closing bracket. The number in brackets must be a constant or an expression that evaluates to a constant, and should also be an integer. In the example we use the value 4.
example :

        #include  using namespace std;
        class Stack { private: enum { MAX = 10 };        
                                      
      public: Stack()                   //constructor
       { top = 0; }
    void push(int var)        //put number on stack
   { st[++top] = var; } 
          int pop()                 //take number off stack 
         { return st[top--]; } };