Create your own user-defined function my_upper( ) that takes one string parameter and returns a string. The function should convert all the characters in the string passed through arguments to uppercase. Then, call that function in the main ( ) function where you should ask the user to input a string, pass that string to the function created by you, and display the result returned by your function.
Sample Input:
CODESMASHERS
Sample Output:
codesmashers
#include <iostream>
#include <string>
using namespace std;
string my_upper(string &str)
{
for (int i = 0; i < str.length(); i++)
{
if (str[i] >= 97 && str[i] <= 122)
{
str[i] = str[i] - 32;
}
else
{
str[i] = str[i];
}
}
return str;
}
int main()
{
string str;
getline(cin, str);
cout << my_upper(str);
return 0;
}
cpp programming exercise with solution |