fork download
  1. #include <iostream>
  2. #include <string.h>
  3. using namespace std;
  4. #define MAX 300
  5.  
  6. void DemTieng(char s1[]);
  7. int myStrcmp(char s1[], char s2[]);
  8. int myStrlen(char s[], int k);
  9. void myStrcpy(char s[], int vt, char s1[], int k);
  10. void myMemmove(char s[], int vt, int k);
  11. int myStrstr(char s[], char s1[]);
  12. void mySubstr(char s[], int b, int count, char ss[]);
  13. bool myStrcat(char s1[], char s2[]);
  14.  
  15. struct Words{
  16. char str[MAX];
  17. int soLan = 0;
  18. };
  19.  
  20.  
  21. Words A[MAX];
  22. int phantu_A=0;
  23.  
  24. Words B[MAX];
  25. int phantu_B=0;
  26.  
  27.  
  28.  
  29. void myStrcpy(char s[], int vt, char s1[], int k){ //gan chuoi s1 bang chuoi s tu vi tri vt cua chuoi s
  30. for (int i = 0; i < k; i++){
  31. s1[i] = s[i + vt];
  32. }
  33. s1[k] = '\0';
  34.  
  35.  
  36. }
  37.  
  38.  
  39. int myStrlen(char s[], int k){
  40. int length = 0;
  41. for (int i = k; s[i] != '\0'; i++){
  42. length++;
  43. }
  44. return length;
  45.  
  46. }
  47.  
  48. int myStrcmp(char s1[], char s2[]){ //so sanh hai chuoi co giong nhau khong, giong nhau tra ve 1 , khong giong tra ve 0
  49. bool onlyWhitespace=true;
  50.  
  51. for (int i=0;i<myStrlen(s1,0);i++) if (s1[i]!=' ') {
  52. onlyWhitespace=false;
  53. //cout << myStrlen(s2,0) << endl;
  54. break;
  55. }
  56.  
  57. if (onlyWhitespace==true) return 0;
  58. if (myStrlen(s2,0)==0) return -1; //
  59. if (myStrlen(s1,0)!=myStrlen(s2,0)) return 0;
  60. for (int i=0;i<myStrlen(s1,0);i++)
  61. if (s1[i]!=s2[i]) return 0;
  62. return 1;
  63.  
  64. }
  65.  
  66. bool check_ton_tai(char s[]){
  67. for (int i=1;i<=phantu_B;i++)
  68. if (myStrcmp(s,B[i].str) == 1) return false;
  69.  
  70. return true;
  71.  
  72. }
  73.  
  74.  
  75.  
  76.  
  77. void DemTieng(char s1[]){
  78. int length = myStrlen(s1,0);
  79.  
  80.  
  81. // Chuyen tat ca cac tu them vao mang A
  82. int i=0;
  83. while (i<length){
  84. while (s1[i]==' '&&i<length) ++i;
  85. if (i<length){
  86. ++phantu_A;
  87. int pos_start=i;
  88. int pos_end;
  89. while (s1[i]!=' '&&i<length) ++i;
  90. if (i<length) pos_end=i-1; else pos_end=length-1;
  91. myStrcpy(s1,pos_start,A[phantu_A].str,pos_end-pos_start+1);
  92. }
  93.  
  94. }
  95.  
  96.  
  97.  
  98.  
  99. for (int i=1;i<=phantu_A;i++){
  100. if (check_ton_tai(A[i].str)==true){
  101. ++phantu_B;
  102. myStrcpy(A[i].str,0,B[phantu_B].str,myStrlen(A[i].str,0));
  103. int dem = 0;
  104. for (int j=1;j<=phantu_A;j++)
  105. if (myStrcmp(A[j].str,B[phantu_B].str)==1) ++dem;
  106. B[phantu_B].soLan = dem;
  107.  
  108. }
  109.  
  110. }
  111.  
  112. for (int i=1;i<=phantu_B;i++) cout << B[i].str << ": " << B[i].soLan << endl;
  113.  
  114.  
  115. }
  116.  
  117.  
  118.  
  119.  
  120. int main()
  121. {
  122. char s[MAX];
  123. cin.getline(s,MAX);
  124. if (myStrcmp(s, "") == 0)
  125. cout << "Chuoi rong." << endl;
  126. else
  127. DemTieng(s);
  128. return 0;
  129. }
  130.  
  131.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Chuoi rong.