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

  • برمجة سي بلس بلس
  • برمجة

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

الأجوبة

#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;
}
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...