Advertisement
Guest User

Untitled

a guest
Jan 19th, 2020
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. void myFgets(char str[], int len);
  6. void lettersInStr(char str[], int lett[], int strSize);
  7. void checkMax(int letter[], int letterSize);
  8. void exchangeLetters(char str[], int strSize);
  9.  
  10. char maxCh='a';
  11. char secMaxCh='a';
  12.  
  13. int main (void)
  14. {
  15. char string[100]={0};
  16. int letters[26]={0};
  17.  
  18. printf("Enter a string:\n");
  19. myFgets(string, 100);
  20.  
  21. lettersInStr(string, letters, strlen(string));
  22. checkMax(letters, AMOUNT_OF_LETTERS);
  23.  
  24. exchangeLetters(string, strlen(string));
  25.  
  26. printf("Most common: %c, 2nd most common: %c\n", maxCh, secMaxCh);
  27. printf("Swapped:\n");
  28. printf("%s", string);
  29. return 0;
  30. }
  31.  
  32. //a function that removes the enter
  33. void myFgets(char str[], int len)
  34. {
  35. fgets(str, len, stdin);
  36. str[strcspn(str, "\n")]=0;
  37. }
  38.  
  39. // a function that check how many times every latter was in the string
  40. void lettersInStr(char str[], int letter[], int strSize)
  41. {
  42. int i=0;
  43.  
  44. for(i=0; i<strSize; i++)
  45. {
  46. letter[str[i]-'a']++;
  47. }
  48.  
  49. }
  50. // a function that checks what is the latter that appeared the most times
  51. void checkMax(int letter[], int letterSize)
  52. {
  53. int i=0;
  54. int j=0;
  55. int tmp=0;
  56.  
  57. int max=0;
  58. int secMax=0;
  59.  
  60. max=-1;
  61. secMax=-1;
  62. for(i=0; i<letterSize; i++)
  63. {
  64. if(letter[i]>max) /
  65. {
  66. max=letter[i];
  67. maxCh=i+'a';
  68. tmp=i;
  69. }
  70. }
  71.  
  72.  
  73. for(j=0; j<letterSize; j++)
  74. {
  75. if(letter[j]>secMax && letter[j]<=max && j!=tmp)
  76.  
  77. {
  78. secMax=lett[j];
  79. secMaxCh=j+'a';
  80. }
  81. }
  82. }
  83.  
  84.  
  85. void exchangeLetters(char str[], int strSize)
  86. {
  87. int i=0;
  88.  
  89. for(i=0; i<strSize; i++)
  90. {
  91. if(str[i]==maxCh)
  92. {
  93. str[i]=secMaxCh;
  94. }
  95. else if(str[i]==secMaxCh)
  96. {
  97. str[i]=maxCh;
  98. }
  99. }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement