#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
usingnamespace std;
string getphrase(void) { //define the getphrase function - this function returns a string but does not expect anything
char localBuffer[120]; //declare an array of 120 type 'char' (e.g. single characters)
printf ("\nPlease provide a multi word phrase (all lowercase): "); //use the c++ printf function to output the user instruction
fgets(localBuffer, 120, stdin); //instead of cin use the c++ function fgets to get a max of 120 characters from the stdin device
return string(localBuffer); //return the users phrase but converted to a string datatype rather than an array of chars
}
void formatphrase (string p_phrase)
{
bool makeUpper = false;
p_phrase.erase(std::remove(p_phrase.begin(), p_phrase.end(), '\n'), p_phrase.end()); //remove some stuff at the end
cout << "\nThe original phrase was: " << p_phrase;
for(
int i=0; i<p_phrase.length(); i++) {
if(i==0){
makeUpper = true;
}
if(makeUpper == true) {
makeUpper = false;
p_phrase[i]=toupper(p_phrase[i]);
}
cout << "\nletter[" << i << "]: " << p_phrase[i];
}
cout << "\n\nFinal version, each word now starts with a capital letter:\n" << p_phrase << endl;
}
int main(){
string phrase;
phrase= getphrase();
formatphrase(phrase);
}
You've mixed the old and new style include files (with/without .h suffix). Use the new style only.
Prefer cout to printf()
Use getline() to get a string. This has the advantage of not needing to erase the (possible) newline at the end of the string.
There's no need to compare a bool to true or false. Just say if (makeUpper) or if (!makeUpper)
So when should you capitalize? It's best to work this out in your head first before you try
committing it to code. You use a bool called makeUpper. So the rule should probably be:
- capitalize the first letter you see when makeUpper is true.
- Set makeUpper to true when you see a space.
- Initialize makeUpper to true to you capitalize the first word.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
usingnamespace std;
string
getphrase(void)
{
//define the getphrase function - this function returns a string but does not expect anything
cout << "\nPlease provide a multi word phrase (all lowercase): "; //use the c++ printf function to output the user instruction
string str;
getline(cin, str); //instead of cin use the c++ function fgets to get a max of 120 characters from the stdin device
return str;
}
void
formatphrase(string p_phrase)
{
bool makeUpper = true;
p_phrase.erase(std::remove(p_phrase.begin(), p_phrase.end(), '\n'), p_phrase.end()); //remove some stuff at the end
cout << "\nThe original phrase was: " << p_phrase;
for (size_t i = 0; i < p_phrase.length(); i++) {
if (isalpha(p_phrase[i])) {
if (makeUpper) {
makeUpper = false;
p_phrase[i] = toupper(p_phrase[i]);
}
} elseif (isspace(p_phrase[i])) {
makeUpper = true;
}
}
cout << "\n\nFinal version, each word now starts with a capital letter:\n" << p_phrase
<< endl;
}
int
main()
{
string phrase;
phrase = getphrase();
formatphrase(phrase);
}