//Maxwell Brewer CS1A Chapter 10, p. 588, #1
//
/*******************************************************************************
* COUNT ARRAY CHARACTERS
* _____________________________________________________________________________
* This program will take a string and output the number
* of characters contained in the string array.
*
* INPUT:
* input_string : user input of characters
*
* OUTPUT:
* charCount : array of string characters
*
******************************************************************************/
#include <iostream>
using namespace std;
// Function prototype
int charCount(char*);
int main() {
// Declare char array for the C-string, size 21
//(20 characters + null terminator)
char input_string[21];
// Prompt user to enter input
cout << "Enter a string with a maximum of 20 characters:\n";
cout << "I will output the number of characters it has!\n";
// Read input up to 20 characters
cin.getline(input_string, 21); // not using cin >>
// since it stops at
// whitespace
// Call function and display the result
cout << "The string you entered has "
<< charCount(input_string)
<< " characters.\n";
return 0; // Return 0 to indicate successful program termination
}
// Function to count the number of characters in a C-string
int charCount(char* ptr) {
int i = 0; // Initialize character count
// Loop through each character in the string until
// null terminator is reached
while (*ptr != '\0'){
i++; // Increment the counter
ptr++; // Move to the next character
}
return i; // Return the character count
}