Advertisement
giang123b

Đếm từ xuất hiện nhiều nhất trong chuỗi

Jun 19th, 2018
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. /*
  2. Tìm tu xuat hien nhieu nhat trong chuoi (không phân biet hoa thuong).
  3. K?t qu? in ra t? xu?t hien nhieu nhat (? d?ng in thu?ng) và sa lan xuat hien cua chúng
  4. INPUT
  5. aaa bbb ccc AA bb aa ccc aa
  6. OUTPUT
  7. aa 3
  8. */
  9. #include<stdio.h>
  10. #include<conio.h>
  11. #include<string.h>
  12. char s[100], *p, a[50][50];
  13. int i, j, dem=0, b[100], c[100], max, d;
  14.  
  15. void tach(){
  16. p=strtok(s, " ");
  17. while (p!='\0'){
  18. strcpy(a[dem], p);
  19. dem++;
  20. p=strtok(NULL, " ");
  21. }
  22. }
  23.  
  24. void demm(){
  25. for(i=0; i<dem; i++){
  26. b[i]=1;
  27. c[i]=1;
  28. }
  29. for(i=0; i<dem; i++){
  30. if (b[i]==1){
  31. for(j=i+1; j<dem+1; j++){
  32. if (strcmp(a[i],a[j]) == 0){
  33. c[i]++;
  34. b[j]=0;
  35. }
  36. }
  37. }
  38. }
  39. }
  40.  
  41. void inkq(){
  42. max=c[0];
  43. d=0;
  44. for(i=1; i<dem; i++){
  45. if(c[i]>max){
  46. max=c[i];
  47. d=i;
  48. }
  49. }
  50. printf("%s %d", a[d], max);
  51. }
  52.  
  53. main(){
  54. gets(s);
  55. strlwr(s);
  56. tach();
  57. demm();
  58. inkq();
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement