fork download
  1. //Zachary Abdollahi CS1A Chapter 10, P. 589, #5
  2. /*******************************************************************************
  3. *
  4. * CAPITALIZE SENTENCES IN STRING
  5. *_______________________________________________________________________________
  6. * This program accepts a C-string from the user, passes a
  7. * pointer to the C-string to a function that capitalizes the
  8. * first character of each sentence in the string, and displays
  9. * the modified string.
  10. *_______________________________________________________________________________
  11. * INPUT
  12. * userString : C-string entered by the user
  13. *
  14. * OUTPUT
  15. * userString : Modified C-string with capitalized sentences
  16. *
  17. *******************************************************************************/
  18.  
  19. #include <iostream>
  20. #include <cctype>
  21. using namespace std;
  22.  
  23. // Function Prototype
  24. void capitalizeSentences(char *strPtr);
  25.  
  26. int main ()
  27. {
  28. const int SIZE = 250; // Maximum C-string size
  29. char userString[SIZE]; //INPUT & OUTPUT
  30.  
  31. // Input String
  32. cout << "Enter a string: ";
  33. cin.getline(userString, SIZE);
  34.  
  35. // Capitalize First Character of Each Sentence
  36. capitalizeSentences(userString);
  37.  
  38. // Output Result
  39. cout << "Modified string: " << userString << endl;
  40.  
  41. return 0;
  42. }
  43.  
  44. /*******************************************************************************
  45. *
  46. * FUNCTION capitalizeSentences
  47. *_______________________________________________________________________________
  48. * This function accepts a pointer to a C-string and converts
  49. * the first alphabetic character at the start of the string and
  50. * after each sentence-ending punctuation mark (., !, ?) to
  51. * uppercase.
  52. *_______________________________________________________________________________
  53. * INPUT
  54. * strPtr : Pointer to a null-terminated C-string
  55. *
  56. * OUTPUT
  57. * strPtr : Modified C-string in place
  58. *
  59. *******************************************************************************/
  60. void capitalizeSentences(char *strPtr)
  61. {
  62. bool newSentence = true; // Flag indicating start of a new sentence
  63.  
  64. while (*strPtr != '\0')
  65. {
  66. if (*strPtr == '.' || *strPtr == '!' || *strPtr == '?')
  67. {
  68. newSentence = true;
  69. }
  70. else if (newSentence && isalpha(*strPtr))
  71. {
  72. *strPtr = toupper(*strPtr);
  73. newSentence = false;
  74. }
  75. else if (!isspace(*strPtr))
  76. {
  77. newSentence = false;
  78. }
  79.  
  80. strPtr++;
  81. }
  82. }
Success #stdin #stdout 0s 544KB
stdin
hello. my name is Joe. what is your name?
stdout
Enter a string: Modified string: Hello. My name is Joe. What is your name?