fork download
  1. // Elaine Torrez CS1A
  2. // _____________________________________________________________
  3. //
  4. // CAPITALIZE SENTENCES IN STRING
  5. // _____________________________________________________________
  6. // This program prompts the user to enter a string containing
  7. // one or more sentences. It then updates the string so that
  8. // the first letter of each sentence is capitalized. The modified
  9. // string is displayed to the user.
  10. // _____________________________________________________________
  11. // INPUT
  12. // userStr : The original sentence(s) entered by the user
  13. //
  14. // OUTPUT
  15. // userStr : The modified string with sentences capitalized
  16. // _____________________________________________________________
  17.  
  18. #include <iostream>
  19. using namespace std;
  20.  
  21. // FUNCTION PROTOTYPE
  22. void capitalizeSentences(char *str);
  23.  
  24. int main()
  25. {
  26. /****************************************************
  27.   * VARIABLE DECLARATIONS
  28.   ****************************************************/
  29. const int SIZE = 300;
  30. char userStr[SIZE]; // INPUT and OUTPUT string
  31.  
  32. /****************************************************
  33.   * INPUT
  34.   ****************************************************/
  35. cout << "Enter a sentence: ";
  36. cin.getline(userStr, SIZE);
  37.  
  38. /****************************************************
  39.   * PROCESS
  40.   ****************************************************/
  41. capitalizeSentences(userStr);
  42.  
  43. /****************************************************
  44.   * OUTPUT
  45.   ****************************************************/
  46. cout << "\nModified sentence:\n";
  47. cout << userStr << endl;
  48.  
  49. return 0;
  50. }
  51.  
  52. // *************************************************************
  53. // capitalizeSentences
  54. // -------------------------------------------------------------
  55. // Modifies a C-string so that the first letter of each sentence
  56. // is capitalized. A sentence is considered to end with '.', '?',
  57. // or '!'.
  58. // *************************************************************
  59. void capitalizeSentences(char *str)
  60. {
  61. bool startOfSentence = true;
  62.  
  63. for (int i = 0; str[i] != '\0'; i++)
  64. {
  65. // Skip leading spaces before a sentence starts
  66. if (startOfSentence && str[i] == ' ')
  67. continue;
  68.  
  69. // Capitalize the first letter of a sentence
  70. if (startOfSentence && str[i] >= 'a' && str[i] <= 'z')
  71. {
  72. str[i] = str[i] - 32; // Convert to uppercase
  73. startOfSentence = false;
  74. }
  75. // If it's already uppercase, just start sentence tracking
  76. else if (startOfSentence && (str[i] >= 'A' && str[i] <= 'Z'))
  77. {
  78. startOfSentence = false;
  79. }
  80.  
  81. // Detect sentence-ending punctuation
  82. if (str[i] == '.' || str[i] == '?' || str[i] == '!')
  83. {
  84. startOfSentence = true;
  85. }
  86. }
  87. }
  88.  
Success #stdin #stdout 0.01s 5320KB
stdin
hello. my name is joe. what is your name?
stdout
Enter a sentence: 
Modified sentence:
Hello. My name is joe. What is your name?