fork download
  1. #include <iostream>
  2. #include <sstream>
  3. #include <vector>
  4. #include <cctype>
  5. using namespace std;
  6.  
  7. string convertName(const string &name) {
  8. stringstream ss(name);
  9. vector<string> words;
  10. string word;
  11.  
  12. while (ss >> word) {
  13. words.push_back(word);
  14. }
  15.  
  16. if (words.size() < 2) return name; // Trả về nguyên gốc nếu không đủ phần họ và tên
  17.  
  18. string lastName = words[0];
  19. for (char &c : lastName) c = toupper(c); // Viết hoa họ
  20.  
  21. string fullName = "";
  22. for (size_t i = 1; i < words.size(); i++) {
  23. words[i][0] = toupper(words[i][0]);
  24. for (size_t j = 1; j < words[i].size(); j++) {
  25. words[i][j] = tolower(words[i][j]);
  26. }
  27. fullName += words[i] + " ";
  28. }
  29.  
  30. fullName.pop_back(); // Xóa khoảng trắng cuối cùng
  31. return fullName + ", " + lastName;
  32. }
  33.  
  34. int main() {
  35. int N;
  36. cin >> N;
  37. cin.ignore(); // Loại bỏ ký tự xuống dòng sau khi nhập số
  38.  
  39. while (N--) {
  40. string name;
  41. getline(cin, name);
  42. cout << convertName(name) << endl;
  43. }
  44.  
  45. return 0;
  46. }
Success #stdin #stdout 0.02s 5288KB
stdin
Standard input is empty
stdout