//Cameron Pham CS1A Chapter 10, p. 588, #1
//
/******************************************************************************
* COUNT STRING LENGTH
* ______________________________________________________________________________
* This program accepts a string and passes it to a function. The function counts
* the number of characters in the C-string. It then returns the total character
* count.
* ______________________________________________________________________________
* INPUT
* String // Characters inputted
*
* OUTPUT
* Length // Counted characters
*******************************************************************************/
#include <iostream>
using namespace std;
// Function prototype
int stringLength(char* str);
int main()
{
const int SIZE = 100;
char userString[SIZE];
int length;
// Get a string from the user
cout << "Enter a string: ";
cin.getline(userString, SIZE);
// Calculate the string length
length = stringLength(userString);
// Display the string length
cout << "The length of the string is: " << length << endl;
return 0;
}
// Count the characters in a C-string
int stringLength(char* str)
{
int count = 0;
// Count characters until the null terminator is reached
while (*(str + count) != '\0')
{
count++;
}
return count;
}