fork download
  1. //Ryan Shahriyarpour CS1A Chapter 10 Question 14
  2. /*****************************************************************************
  3.  *
  4.  * Word Separator
  5.  *
  6.  *********************
  7.  *
  8.  * This program accepts a sentence in which all of the words are run
  9.  * together, but the first character of each word is uppercase. It converts
  10.  * the sentence to one where the words are separated by spaces and only the
  11.  * first word starts with an uppercase letter.
  12.  * For example, "StopAndSmellTheRoses." becomes "Stop and smell the roses."
  13.  *
  14.  * INPUT
  15.  * sentence - the run-together sentence entered by the user
  16.  *
  17.  * OUTPUT
  18.  * The sentence with spaces between the words
  19.  *
  20.  ****************************************************************************/
  21.  
  22. #include <iostream>
  23. #include <string>
  24. #include <cctype>
  25. using namespace std;
  26.  
  27. int main()
  28. {
  29. // DATA DICTIONARY
  30. // sentence - the run-together sentence entered by the user
  31. // separated - the sentence after the words have been separated
  32. // index - loop counter
  33. string sentence;
  34. string separated;
  35. unsigned int index = 0;
  36.  
  37. // Get the sentence
  38. cout << "Enter a sentence with the words run together, each word\n"
  39. << "starting with an uppercase letter: ";
  40. getline(cin, sentence);
  41.  
  42. // Insert a space before each uppercase letter (except the first
  43. // character) and convert that letter to lowercase
  44. for (index = 0; index < sentence.length(); index++)
  45. {
  46. if (index > 0 && isupper(sentence[index]))
  47. {
  48. separated += ' ';
  49. separated += tolower(sentence[index]);
  50. }
  51. else
  52. separated += sentence[index];
  53. }
  54.  
  55. // Display the result
  56. cout << "\nSeparated: " << separated << endl;
  57.  
  58. return 0;
  59. }
Success #stdin #stdout 0s 544KB
stdin
ThisIsACodeTest
stdout
Enter a sentence with the words run together, each word
starting with an uppercase letter: 
Separated: This is a code test