fork download
  1. #include <iostream>
  2. #include <cctype>
  3. #include <string>
  4. using namespace std;
  5.  
  6. string toLowerCase(const string& str) {
  7. string result = str;
  8. for (char& ch : result) {
  9. ch = tolower(ch);
  10. }
  11. return result;
  12. }
  13.  
  14. string toUpperCase(const string& str) {
  15. string result = str;
  16. for (char& ch : result) {
  17. ch = toupper(ch);
  18. }
  19. return result;
  20. }
  21.  
  22. string capitalizeWords(const string& str) {
  23. string result = str;
  24. bool capitalize = true;
  25. for (char& ch : result) {
  26. if (isspace(ch)) {
  27. capitalize = true;
  28. } else if (capitalize) {
  29. ch = toupper(ch);
  30. capitalize = false;
  31. } else {
  32. ch = tolower(ch);
  33. }
  34. }
  35. return result;
  36. }
  37.  
  38. string normalizeString(const string& str) {
  39. string result;
  40. bool inWord = false;
  41. for (char ch : str) {
  42. if (!isspace(ch)) {
  43. if (inWord && !result.empty() && isspace(result.back())) {
  44. result += ' ';
  45. }
  46. result += ch;
  47. inWord = true;
  48. } else if (inWord) {
  49. result += ' ';
  50. inWord = false;
  51. }
  52. }
  53. if (!result.empty() && isspace(result.back())) {
  54. result.pop_back();
  55. }
  56. return result;
  57. }
  58.  
  59. int main() {
  60. string input;
  61. cout << "Nhap chuoi: ";
  62. getline(cin, input);
  63.  
  64. cout << "Chuoi ky tu thuong: " << toLowerCase(input) << endl;
  65. cout << "Chuoi ky tu hoa: " << toUpperCase(input) << endl;
  66. cout << "Chuoi viet hoa moi tu: " << capitalizeWords(input) << endl;
  67. cout << "Chuoi chuan hoa: " << normalizeString(input) << endl;
  68.  
  69. return 0;
  70. }
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
Nhap chuoi: Chuoi ky tu thuong: 
Chuoi ky tu hoa: 
Chuoi viet hoa moi tu: 
Chuoi chuan hoa: