topical media & game development

talk show tell print

basic-program-code-04-Ex4-10.c

? / basic-program-code-04-Ex4-10.c


  // EX4_10.CPP
  // Counting string characters using a pointer
  include <iostream>
  using std::cin;
  using std::cout;
  using std::endl;
  
  int main()
  {
     const int MAX = 80;                 // Maximum array dimension
     char buffer[MAX];                   // Input buffer
     char* pbuffer = buffer;             // Pointer to array buffer
  
     cout << endl                        // Prompt for input
          << "Enter a string of less than "
          << MAX << " characters:"
          << endl;
  
     cin.getline(buffer, MAX, '\n');     // Read a string until \n
  
     while(*pbuffer)                     // Continue until \0
        pbuffer++;
  
     cout << endl
          << "The string \"" << buffer
          << "\" has " << pbuffer - buffer << " characters.";
     cout << endl;
     return 0;
  }
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.