//Devin Scheu CS1A Chapter 10, P. 588, #1
//
/**************************************************************
*
* DETERMINE LENGTH OF C-STRING
* ____________________________________________________________
* This program finds the number of characters in a C-string
* entered by the user.
* ____________________________________________________________
* INPUT
* inputString : The C-string entered by the user
*
* OUTPUT
* stringLength : The number of characters in the string
*
**************************************************************/
#include <iostream>
#include <iomanip>
using namespace std;
// Function to count length of C-string using pointer notation
int getStringLength(const char* str) {
const char* ptr = str;
while (*ptr != '\0') {
ptr++;
}
return ptr - str;
}
int main () {
//Variable Declarations
const int MAX_SIZE = 100; //OUTPUT - Maximum size of the input string
char inputString[MAX_SIZE]; //INPUT - The C-string entered by the user
int stringLength; //OUTPUT - The number of characters in the string
//Prompt for Input and Echo
cout << "Enter a string (max 99 characters): ";
cin.getline(inputString, MAX_SIZE);
cout << inputString << endl;
//Calculate Length
stringLength = getStringLength(inputString);
//Separator and Output Section
cout << "-------------------------------------------------------" << endl;
cout << "OUTPUT:" << endl;
//Output Result
cout << left << setw(25) << "String Length:" << right << setw(15) << stringLength << endl;
} //end of main()