Strings

Strings are actually character arrays. The array points to the first character. String are terminated with a '\0'. String constants in a C program are enclosed in double quotes.

Some, but not all C++ implementations offera string class.

  1. Declaring strings.

    When declaring a string remember to include room for the `\0`. Strings may be initialized when declared as:
    void declare()
    {
      // Character arrays
    
      char a[20]="George Washington";
      char b[]  ="John Adams";
    
      cout << "1. a size = " << sizeof(a) << " = " << a << endl
           << "2. b size = " << sizeof(b) << " = " << b << endl;
              
      // String type
    
      string c = "Thomas Jefferson" ;
    
      cout << "3. c size = " << sizeof(c) << " = " << c << endl;
    
    }
    1. a size = 20 = George Washington
    2. b size = 11 = John Adams
    3. c size = 16 = Thomas Jefferson
    
  2. Line Oriented input

    To grab a line at a time use getline.
    void line_input()
    {
       char a[20];
       char b[20];
       char x;
       
       
       // Character arrays
       
       cout << " Please enter a string";
       cin >> a;
       cout << "4. " << a << endl;
       do { 
          cin.get(x);
       } while (x != '\n');   /// clear the line
       cout << " Please enter a string";
       cin.getline(b,20,'\n');
       cout << "5. " << b << endl;
       
       // string type 
       string c;
       
       cout << " Please enter a string";
       cin >> c;
       cout << "6. " << c << endl;
       do { 
          cin.get(x);
       } while (x != '\n');   /// clear the line
       cout << " Please enter a string ";
       getline(cin,c );
    }
     Please enter a string James Madison
    4. James
     Please enter a string James Madison
    5.  James Madison
     Please enter a stringJames Monroe
    6. James
     Please enter a string James Monroe
    7. James Monroe
    
  3. String functions.

    Because strings are used in many programming applications there is a C library of common string functions. Note all string functions assumed the string is terminated with '\0'. To use these functions the header file <string>
    void copycat()
    {
    // Character arrays;
       
       int a;
       char b[30]="John Q. Adams";
       a=strlen(b);
       cout <<"8. " <<  b << "size = " << sizeof(b) 
            << " length = " << strlen(b) << endl;
       
       char c[30]="Andrew Jackson";
       strcpy(b,c);
       cout << "9. " <<  b << endl;
       
       char d[30]="";
       strncpy(d,c,6);
       cout << "10. " <<  d << endl;
         
       char e[30]="Martin ";
       strcat(e,"Van Buren");
       cout << "11. " <<  e << endl;
       
       char f[30];
       strcat(strcpy(f,"William")," Henry Harrison");
       cout << "12. " << f << endl;
       
       // string types;
       
       string g;
       g="John Tyler";
       cout << "13. " << g << endl;
       
       string h="James ";
       h = h + "Polk";
       cout << "14. " << h <<endl;
    
    }
    8. John Q. Adamssize = 30 length = 13
    9. Andrew Jackson
    10. Andrew
    11. Martin Van Buren
    12. William Henry Harrison
    13. John Tyler
    14. James Polk
    
    void compare()
    {
       // Character arrays;
       
       char i[30]="Zachry Taylor";
       char j[30]="Abraham Lincoln";
        
       if(strcmp(i,j)!=0) {
          cout <"15. " << i << " Is no " << j <<endl;
       }
       if(strncmp(i,"Zach",4) == 0){
          cout <g< "16. but is a Zach" <<endl;
       }
       
       // string types
       string k = "Millard Fillmore";
       string l = "Abraham Lincoln";
       
       if(k != l ) {
          cout <g< "17. " << k << " is no " << l << endl;
       }
       if( k.substr(0,7) == "Millard") {
          cout <g<"18.  but is a  Millard" << endl;
       }
    }
    15. Zachry Taylor Is no Abraham Lincoln
    16. but is a Zach
    17. Millard Fillmore is no Abraham Lincoln
    18.  but is a  Millard
    
    void reverse()
       {
       
       // character arrays
       
       char m[30]="Franklin Pierce";
       char *p;   
       cout << "19. " << m << endl;
       p=strchr(m,' ');
       *p='\0';
       p++;
       cout << "20. " << p << ", " << m << endl;
       
       // string types
       
       string n ="James Buchanan";
       int here;
       string::size_type there;
       cout << "20. " << n << endl;
       here = n.find(' ');
       there = n.length();
       cout << "21. " << n.substr(here+1,there) << ", " << n.substr(0,here) << endl;
    
    }
    19. Franklin Pierce
    20. Pierce, Franklin
    20. James Buchanan
    21. Buchanan, James
    
  4. example

    coneverting binary and decimal

    /*
     reads in numbers as strings
     binary number preceeded with 0 (zero)
     converts to other base. handles base ten and 2.
    
      BLUE code uses string types, Gray code (not blue) uses
      character arrays
    */
    
    #ifdef BLUE
      #include <string> 
    #else
      #include <string.h>
    #endif
    
    #include <iostream>
    using namespace std;
    
    
    
    #ifdef BLUE
      int  input(string,int);   // Protoype for using string
    #else
      int  input(char*,int);     // prototype for using char*
    #endif
    
    void print(int,int);
    
    void main()
    {
    
    #ifdef BLUE
       string s1;
    #else
       char s1[20];
    #endif
    
      int inbase,outbase,number;
    
    #ifdef BLUE
       cout << " Running BLUE code " << endl;
    #else
       cout << " Running Gray code " << endl;
    #endif
    
       do {
          cout << " Enter the number, q to quit :";
          cin >> s1;
          if(s1[0] == 'q' ) break;
          if(s1[0] == '0' ) {
               inbase =2;
               outbase=10;
          } else {
               inbase=10;
               outbase=2;
          }
          number=input(s1,inbase);
          print(number,outbase);
          cout << endl;
       } while (1);
    }
    #ifdef BLUE
    int input(string s1, int inbase)
    {
       string::size_type len;
       int  i, value =0;
    
       len=s1.length();
       for(i=0;i<len;i++)
          value=value*inbase + s1[i] -'0';
       return value;
    }
    #else
    int input(char *s1,int inbase)
    {
       int value=0;
       while(*s1) {
          value=value * inbase + *s1 - '0';
          s1++;
       }
       return value;
    }
    #endif
    void print(int number, int outbase)
    {
       int q,r;
    
       q=number/outbase;
       r=number%outbase;
       if(q) {
          print(q,outbase);
       }
       cout << r;
    }
    
     Running Gray code
     Enter the number, q to quit :010101
    21
     Enter the number, q to quit :21
    10101
     Enter the number, q to quit :q
    Press any key to continue
    
    Then add 
    #define BLUE to code;
    recompile and rerun:
    
     Running BLUE code
     Enter the number, q to quit :011011011
    219
     Enter the number, q to quit :219
    11011011
     Enter the number, q to quit :q
    Press any key to continue