106k views
0 votes
Create a program that includes a function called toUpperCamelCase that takes a string (consisting of lowercase words and spaces) and returns the string with all spaces removed. Moreover, the first letter of each word is to be forced to its corresponding uppercase. For example, given "hello world" as the input, the function should return "HelloWorld". The main function should prompt the user to input a string until the user types "Q". For each string input call the function with the string and display the result. (Hint: You may need to use the toupper function defined in the header file.)

User Crcvd
by
5.5k points

1 Answer

1 vote

Answer:

#include<iostream>

using namespace std;

//method to remove the spaces and convert the first character of each word to uppercase

string

toUpperCameICase (string str)

{

string result;

int i, j;

//loop will continue till end

for (i = 0, j = 0; str[i] != '\0'; i++)

{

if (i == 0) //condition to convert the first character into uppercase

{

if (str[i] >= 'a' && str[i] <= 'z') //condition for lowercase

str[i] = str[i] - 32; //convert to uppercase

}

if (str[i] == ' ') //condition for space

if (str[i + 1] >= 'a' && str[i + 1] <= 'z') //condition to check whether the character after space is lowercase or not

str[i + 1] = str[i + 1] - 32; //convert into uppercase

if (str[i] != ' ') //condition for non sppace character

{

result = result + str[i]; //append the non space character into string

}

}

//return the string

return (result);

}

//driver program

int main ()

{

string str;

char ch;

//infinite loop

while (1)

ch == 'Q') //is user will enter Q then terminatethe loop

break;

cout << toUpperCameICase (str);

cout << endl;

return 0;

}

User Jeroen
by
6.0k points