void function

I'm trying to create a function that will set the first character in every string to uppercase and the rest of the characters to lowercase. When I run the program the printed strings are the same as what was entered. for example, if i enter "hELLO, hOW aRE yOU dOING?" the same strings are printed. I need it to print "Hello, How Are Yo[/s]u Doing?" if anyone can help with this thanks!!!

my current program.

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
#include <iostream>
#include <string>
using namespace std;

void upper_lower(string);

int main ()
{
    string word = "";
    
    cin >> word;
    while (cin) {
        upper_lower(word);
        cout << word << " ";
        cin >> word;
    }
    return 0;
}

void upper_lower(string x) {
    
    for(int i = 0; i < 1; i++)
    {
        x[i] = toupper(x[i]);
    }
    
    for(int i = 1; i < x.length(); i++)
    {
        x[i] = tolower(x[i]);
    }
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

using namespace std;

const string & getString( string &firstCharUpper);


int main() {

//a function that will set the first character in every string to uppercase

    string input;
    cin >> input;
    getString(input);
    cout << input << endl;

    return 0;
}

const string & getString(string &firstCharUpper) {
    firstCharUpper[0] = toupper(firstCharUpper[0]);
    return  firstCharUpper;
}
i need every character after the first to be changed to lower case as well. hELLO would be converted to Hello.
You current program is almost working: you need only to change void upper_lower(string x) to void upper_lower(string& x)
Topic archived. No new replies allowed.