Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- void myFgets(char str[], int len);
- void lettersInStr(char str[], int lett[], int strSize);
- void checkMax(int letter[], int letterSize);
- void exchangeLetters(char str[], int strSize);
- char maxCh='a';
- char secMaxCh='a';
- int main (void)
- {
- char string[100]={0};
- int letters[26]={0};
- printf("Enter a string:\n");
- myFgets(string, 100);
- lettersInStr(string, letters, strlen(string));
- checkMax(letters, AMOUNT_OF_LETTERS);
- exchangeLetters(string, strlen(string));
- printf("Most common: %c, 2nd most common: %c\n", maxCh, secMaxCh);
- printf("Swapped:\n");
- printf("%s", string);
- return 0;
- }
- //a function that removes the enter
- void myFgets(char str[], int len)
- {
- fgets(str, len, stdin);
- str[strcspn(str, "\n")]=0;
- }
- // a function that check how many times every latter was in the string
- void lettersInStr(char str[], int letter[], int strSize)
- {
- int i=0;
- for(i=0; i<strSize; i++)
- {
- letter[str[i]-'a']++;
- }
- }
- // a function that checks what is the latter that appeared the most times
- void checkMax(int letter[], int letterSize)
- {
- int i=0;
- int j=0;
- int tmp=0;
- int max=0;
- int secMax=0;
- max=-1;
- secMax=-1;
- for(i=0; i<letterSize; i++)
- {
- if(letter[i]>max) /
- {
- max=letter[i];
- maxCh=i+'a';
- tmp=i;
- }
- }
- for(j=0; j<letterSize; j++)
- {
- if(letter[j]>secMax && letter[j]<=max && j!=tmp)
- {
- secMax=lett[j];
- secMaxCh=j+'a';
- }
- }
- }
- void exchangeLetters(char str[], int strSize)
- {
- int i=0;
- for(i=0; i<strSize; i++)
- {
- if(str[i]==maxCh)
- {
- str[i]=secMaxCh;
- }
- else if(str[i]==secMaxCh)
- {
- str[i]=maxCh;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement