fork download
  1. //Cecilia Rangel CSC5 Chapter 10, P.588, #1
  2. //
  3. /**************************************************************
  4.  *
  5.  * COMPUTE TEST SCORES
  6.  * ____________________________________________________________
  7.  *This program returns an integer and accepts a
  8.  * pointer to a C-string as an arguement. The
  9.  * function counts the number of characters in the
  10.  * string and returns that number.
  11.  *_____________________________________________________________
  12.  * INPUT
  13.  * userinput : INPUT a string from the user
  14.  *
  15.  * OUTPUT
  16.  * num : OUTPUT the number of characters in a string
  17.  *
  18.  * ***************************************************************/
  19. #include <iostream>
  20. #include <iomanip>
  21. using namespace std;
  22.  
  23. //Function prototype
  24. int CountNumChar(char* ptr);
  25.  
  26. int main() {
  27. //
  28. //Declare variables and pointers
  29. int SIZE = 100; //Initialize size to 100
  30. char userInput[SIZE]; //User input
  31. int numChar; //Number of characters
  32. char* ptr; //Pointer to character datatype
  33. ptr = userInput; //assign pointer to array
  34. //
  35. //Prompt user to enter string
  36. cout << "Enter a character string.\n";
  37. cin.getline(userInput, SIZE);
  38. //
  39. //Call function
  40. numChar = CountNumChar(ptr);
  41. //
  42. //Output number of characters
  43. cout << "The number of characters in your string is ";
  44. cout << numChar;
  45. return 0;
  46. }
  47. //
  48. //Return number of characters
  49. int CountNumChar(char* ptr)
  50. {
  51. int count;
  52. int size = 0;
  53. int numChar = 0;
  54. //
  55. //Find number of elements in C-string
  56. for (count = 0; *(ptr + count) != '\0'; count++)
  57. size++;
  58. //
  59. //Find number of characters
  60. for (count = 0; *(ptr + count) > size; count ++)
  61. {
  62. if (*(ptr + count) != ' ')
  63. numChar ++;
  64. }
  65. return numChar;
  66. }
Success #stdin #stdout 0s 544KB
stdin
I'll be fine and dandy
stdout
Enter a character string.
The number of characters in your string is 18