fork download
  1. // Timothy Price CS1A Ch 10 Challenge 5
  2. /******************************************************************************
  3.  * Format capitalization
  4.  *
  5.  * This program receives a number of sentences and capitalizes the first letter
  6.  * of each sentence.
  7.  *
  8.  * Input:
  9.  * sentence(s)
  10.  *
  11.  * Output:
  12.  * same sentence(s) with reformatted capitalization
  13. ******************************************************************************/
  14.  
  15. #include <cctype>
  16. #include <iostream>
  17.  
  18. using namespace std;
  19.  
  20. // Function prototype
  21. void capitalizeSentences(char* str);
  22.  
  23. int main()
  24. {
  25. // Data dictionary
  26. const int MAX_LENGTH = 256;
  27. char input[MAX_LENGTH];
  28.  
  29. // Receive input and output confirmation
  30. cout << "Sentence Capitalizer" << endl;
  31. cout << "----------------------------------------" << endl;
  32. cout << "Enter a sentence or sentances: ";
  33. cin.getline(input, MAX_LENGTH);
  34. cout << input << endl;
  35.  
  36. // Call function
  37. capitalizeSentences(input);
  38.  
  39. // Output result
  40. cout << "Formatted sentance(s): " << input << endl;
  41.  
  42. return 0;
  43. }
  44.  
  45. // Function definition
  46. void capitalizeSentences(char* str)
  47. {
  48. if (str == nullptr)
  49. {
  50. return;
  51. }
  52.  
  53. bool capitalizeNext = true;
  54.  
  55. while (*str != '\0')
  56. {
  57. if (capitalizeNext && isalpha(static_cast<unsigned char>(*str)))
  58. {
  59. *str = static_cast<char>(
  60. toupper(static_cast<unsigned char>(*str))
  61. );
  62. capitalizeNext = false;
  63. }
  64. else if (*str == '.' || *str == '!' || *str == '?')
  65. {
  66. capitalizeNext = true;
  67. }
  68.  
  69. str++;
  70. }
  71. }
Success #stdin #stdout 0s 556KB
stdin
hello. my name is Clyde. what is your name? goodbye.
stdout
Sentence Capitalizer
----------------------------------------
Enter a sentence or sentances: hello. my name is Clyde. what is your name? goodbye.
Formatted sentance(s): Hello. My name is Clyde. What is your name? Goodbye.