fork download
  1. #include <iostream>
  2. #include <string.h>
  3. using namespace std;
  4. #define MAX 300
  5.  
  6. int myStrcmp(char s1[], char s2[]);
  7. int myStrlen(char s[], int k);
  8. void myStrcpy(char s[], int vt, char s1[], int k);
  9. void mySubstr(char s[], int b, int count, char ss[]);
  10. bool myStrcat(char s1[], char s2[]);
  11. void StringReverse(char st[]);
  12.  
  13. int main()
  14. {
  15. char s[MAX];
  16. fgets(s,300,stdin);
  17. if (myStrcmp(s, "") == 0)
  18. cout << "Chuoi rong." << endl;
  19. else
  20. {
  21. StringReverse(s);
  22. cout << s << endl;
  23. }
  24. return 0;
  25. }
  26. int myStrlen1(char s[]) {
  27. int length = 0;
  28. while (s[length] != '\0') {
  29. ++length;
  30. }
  31. return length;
  32. }
  33.  
  34. int myStrcmp(char s1[], char s2[]) {
  35. int m = myStrlen1(s1);
  36. int n = myStrlen1(s2);
  37. for (int i = 0; i < min(m, n); ++i) {
  38. if (s1[i] > s2[i]) {
  39. return 1;
  40. }
  41. else if (s1[i] < s2[i]) {
  42. return -1;
  43. }
  44. }
  45. if (m > n) return 1;
  46. else if(m < n) return -1;
  47. else return 0;
  48. }
  49.  
  50. void myStrcpy1(char s[], char s1[]) {
  51. int i = 0;
  52. while (s1[i] != '\0') {
  53. s[i] = s1[i];
  54. ++i;
  55. }
  56. s[i] = '\0';
  57. }
  58.  
  59. void myStrcat1(char s1[], char s2[]) {
  60. int i = 0;
  61. while (s1[i] != '\0') {
  62. ++i;
  63. }
  64. int j = 0;
  65. while (s2[j] != '\0') {
  66. s1[i] = s2[j];
  67. ++i;
  68. ++j;
  69. }
  70. s1[i] = '\0';
  71. }
  72.  
  73. void StringReverse(char s[]) {
  74. s[myStrlen1(s) - 1] = '\0'; // Bỏ ký tự xuống dòng cuối cùng
  75. if (myStrcmp(s, "") == 0) {
  76. cout << "Chuoi rong." << endl;
  77. return;
  78. }
  79. char a[100][100];
  80. int n = 0;
  81. char* token = strtok(s, " ");
  82. while (token != NULL) {
  83. myStrcpy1(a[n], token);
  84. ++n;
  85. token = strtok(NULL, " ");
  86. }
  87. for (int i = 0; i < n; ++i) {
  88. int len = myStrlen1(a[i]);
  89. for (int j = 0; j < len / 2; ++j) {
  90. char temp = a[i][j];
  91. a[i][j] = a[i][len - j - 1];
  92. a[i][len - j - 1] = temp;
  93. }
  94. }
  95. myStrcpy1(s, a[0]);
  96. for (int i = 1; i < n; ++i) {
  97. myStrcat1(s, " ");
  98. myStrcat1(s, a[i]);
  99. }
  100. }
Success #stdin #stdout 0s 5288KB
stdin
Standard input is empty
stdout
�V�x