Thread: Counting all characters in a string

  1. #1
    Unregistered
    Guest

    Question Counting all characters in a string

    How do i count all the characters in a string?

  2. #2
    Registered User raimo's Avatar
    Join Date
    Jun 2002
    Posts
    107
    With size() method. Or if you are using char arrays, then strlen() (#include <cstring>). Or if you want to do it yourself , just go through the characters with while loop.

  3. #3
    Unregistered
    Guest
    Code:
    int Length(char* a) {
    
      int count = 0;
      char *aux = a;
    
      while (*aux != '\0') {
        count++;
        aux++;
        }
    
      return count;
      }

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Or...

    int size = sizeof( Array ) / sizeof( char );

    where array is the variable name and char is the type of
    data stored in your array.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  2. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  3. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  4. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM
  5. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM