Return to Topic Menu | Computer Science Main Page | MathBits.com | Terms of Use

Demo Standard Character String Arrays

 Sample program:
(This program shows how to use standard C++ character string arrays.  Notice the limitations placed upon their manipulation.  The AP Class apstring will allow us to deal more efficiently with such string data.)

#include <iostream.h>
int main(void)
{
      //assigning strings to character arrays with  = sign must be done in the
      //declaration statement!
   
     char animalOne[6] = "Lions";  //reserves cells for 5 letters and the null character
     char animalTwo[ ] = "tigers";   //lets the computer do the counting!!
     char animalThree[ ] = "bears";

     cout<<animalOne<<" and ";   //notice no brackets needed to print array

     cout<<animalTwo<<" and "<<endl;

     cout<<animalThree[0]<<endl   //to print array one cell at a time, use subscripts
            <<animalThree[1]<<endl   //one at a time.
            <<animalThree[2]<<endl
            <<animalThree[3]<<endl
            <<animalThree[4];

     //to change the word "Lions"  to "dogs", you must change ONE letter at a time.
     animalOne[0]='d';
     animalOne[1]='o';
     animalOne[2]='g';
     animalOne[3]='s';
     animalOne[4]=' ';
     cout<<endl<<animalOne;

     cout<<"\n\aOh my!!            :-0";

     cout<<endl<<endl<<endl;

     return 0;
}

 

Screen Display:

Lions and tigers and
b
e
a
r
s
dogs
Oh my!!           :-0

 

Return to Topic Menu | Computer Science Main Page | MathBits.com | Terms of Use