Q:

Write a C++ program to capitalize the first letter of each word of a given string. Words must be separated by only one space

0

Write a C++ program to capitalize the first letter of each word of a given string. Words must be separated by only one space

Sample Output:

Write A C++ Program
Cpp String Exercises

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <iostream>
#include <string>

using namespace std;

string Capitalize_first_letter(string text) {

	for (int x = 0; x < text.length(); x++)
	{
		if (x == 0)
		{
			text[x] = toupper(text[x]);
		}
		else if (text[x - 1] == ' ')
		{
			text[x] = toupper(text[x]);
		}
	}

	return text;
}

int main() 
{
	cout << Capitalize_first_letter("Write a C++ program");
	cout << "\n" << Capitalize_first_letter("cpp string exercises");
	return 0;
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now