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