Here is the implementation of the
In the above code, the
The
Note that the
ucfirst
function in C++:
Code:
c++
#include <iostream>
#include <string>
using namespace std;
string ucfirst(string str) {
if (str.empty()) {
return str;
}
str[0] = toupper(str[0]);
return str;
}
int main() {
string input;
cout << "Enter a string: ";
getline(cin, input);
cout << "Upper case first letter is: " << ucfirst(input) << endl;
return 0;
}
In the above code, the
ucfirst
function takes a string as input and returns the same string with the first letter converted to uppercase. The
main
function takes input from the user using getline
and passes it to the ucfirst
function. The returned string is then printed to the console.Note that the
toupper
function is used to convert the first character to uppercase. The if
statement is used to handle the case where the input string is empty.