Skip to content

Instantly share code, notes, and snippets.

@MadMaxMcKinney
Last active October 26, 2017 01:52
Show Gist options
  • Save MadMaxMcKinney/d7d59e745756b0583f9189b15ca61301 to your computer and use it in GitHub Desktop.
Save MadMaxMcKinney/d7d59e745756b0583f9189b15ca61301 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
string formatSentence(string sentence) {
// Store the formatted sentence here
string formattedSentence;
// Add the first letter of the word to the new sentence
formattedSentence += (sentence[0]);
// Starting past the first letter loop through the original sentence
for(int i = 1; i < sentence.length(); i++) {
// Use isupper to check if the current character is a capital character and that the previous character is not a space
// isupper is a built-in c++ function
if(isupper(sentence[i]) && sentence[i-1] != ' ')
formattedSentence += ' ';
// If it's not a captial just add the letter
formattedSentence += sentence[i];
}
return formattedSentence;
}
int main()
{
string str1;
string str2;
string str3;
string str4;
string str5;
str1 = "TodayIsTheFirstDayOfTheRestOfYourLife";
str2 = "AnAttitudeOfGratitude";
str3 = "AbsenceMakesTheHeartGrowFonder";
str4 = "AnAppleADayKeepsTheDoctorAway";
str5 = "ThisLabIsNowFinished";
cout << "Original sentence: " << str1 << " Formatted Sentence: " << formatSentence(str1) << endl;
cout << "Original sentence: " << str2 << " Formatted Sentence: " << formatSentence(str2) << endl;
cout << "Original sentence: " << str3 << " Formatted Sentence: " << formatSentence(str3) << endl;
cout << "Original sentence: " << str4 << " Formatted Sentence: " << formatSentence(str4) << endl;
cout << "Original sentence: " << str5 << " Formatted Sentence: " << formatSentence(str5) << endl;
return(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment