//Cecilia Rangel CSC5 Chapter 10, P.588, #1
//
/**************************************************************
*
* COMPUTE TEST SCORES
* ____________________________________________________________
*This program returns an integer and accepts a
* pointer to a C-string as an arguement. The
* function counts the number of characters in the
* string and returns that number.
*_____________________________________________________________
* INPUT
* userinput : INPUT a string from the user
*
* OUTPUT
* num : OUTPUT the number of characters in a string
*
* ***************************************************************/
#include <iostream>
#include <iomanip>
using namespace std;
//Function prototype
int CountNumChar(char* ptr);
int main() {
//
//Declare variables and pointers
int SIZE = 100; //Initialize size to 100
char userInput[SIZE]; //User input
int numChar; //Number of characters
char* ptr; //Pointer to character datatype
ptr = userInput; //assign pointer to array
//
//Prompt user to enter string
cout << "Enter a character string.\n";
cin.getline(userInput, SIZE);
//
//Call function
numChar = CountNumChar(ptr);
//
//Output number of characters
cout << "The number of characters in your string is ";
cout << numChar;
return 0;
}
//
//Return number of characters
int CountNumChar(char* ptr)
{
int count;
int size = 0;
int numChar = 0;
//
//Find number of elements in C-string
for (count = 0; *(ptr + count) != '\0'; count++)
size++;
//
//Find number of characters
for (count = 0; *(ptr + count) > size; count ++)
{
if (*(ptr + count) != ' ')
numChar ++;
}
return numChar;
}