Capitalise the first letter of each new word.

Hi, im looking to find out how to capitalise the first letter of each word entered. any help would be greatly appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>

using namespace 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);
}
Last edited on
closed account (z05DSL3A)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <string>


void Capitalise (std::string & str);

int main ()
{
    using std::string;
    using std::cout;

    string str{"jackdaws love my  big \tsphinx of quartz! "};
    cout << str << "\n";
    Capitalise (str);
    cout << str << "\n";
    return 0;
}

void Capitalise (std::string& str)
{
    using std::string;

    string::size_type pos{ 0 };
    do
    {
        if (pos > 0) pos++;
        if (pos == str.length()) 
            return;

        if (isalpha (str[pos]))
        {
            str[pos] = toupper (str[pos]);
            pos++;
        }
        
    } while (string::npos != (pos = str.find_first_of (" \t", pos)));
}
Last edited on
You should indent you code consistently.

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace 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]);
	    }
	} else if (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);
}

Topic archived. No new replies allowed.