Home > database >  C program for calculating alphabetical strings
C program for calculating alphabetical strings

Time:12-16

Task:

Create a function maxRepeatingLetter that receives a string and as a result returns the letter that is repeated the most times in the given string (if more than one letter meets the given condition, return the first letter of the English alphabet). It is assumed that there will be at least one letter in the string.

Code:

#include <stdio.h>

char maxRepeatingLetter(const char *s)
{
int i, max = 0, maxIndex = 0;
int letter[26] = {0}; 
const char *pom = s;
while (*pom != '\0') 
{
  if ((*pom >= 'a' && *pom <= 'z'))
  {
     letter[*pom - 97]  ;
  }
  if (*pom >= 'A' && *pom <= 'Z')
  {
     letter[*pom - 65]  ;
  }
 pom  ; 
}
 
for (i = 0; i < 26; i  )
{
   if (letter[i] > max) 
   { 
    max =letter[i]; maxIndex = I;
   }
 return maxIndex   65;
}

int main () 
{
 printf("Most repeating letter is: %c", 
 maxRepeatingLetter("input for letter to repeat"));
 return 0;
}

My current task is being able to explain the code above and how it works. And I need to input a minor change into it, for example, to add something to the code, or make it simpler. But not to lose the main functions the code has.

Is anyone willing to explain the code above in 2-3 lines? And if you could, assist me or hint me, or even show me, what changes to the code I could apply.

CodePudding user response:

I can see that you have to distinguish lowercase and uppercase and you can have only letters not symbols such as ? | ^ ! ecc... so I'll try to give you some advice:

  1. Try to indent the code, it will be more readable to an external eye.
  2. Using the variable letter is a good idea but i don't get the point of using pom.
  3. If you can use use the function strlen() from the library string.h, otherwise implementing it by yourself could be a good exercise.
  4. letter[*pom - 97] , letter[*pom - 65] and maxIndex 65 are dependant from the ASCII table, try letter[*pom - 'a'] , letter[*pom - 'A'] and maxIndex 'A'.
  5. The for loop doesn't work, you missed a brackets so the if isn't in the for.

The code explanation is pretty easy, first of all you use the arrayletter of 26 elements because in the alphabet we have 26 letters, so the i-th element correspond to the i-th letter of the alphabet.

You loop once on the string and save in the i-th element letter the number of occurrence of the i-th letter.

With the if in for loop you are simply finding the max in that array, once found it, you return the index of the max, the index is the letter occuring more often.

Sorry for my bad English tell me if you need more help.

CodePudding user response:

It is very straightforward if you are looking for an explanation of the above code. I recommend using multiple prints to understand how things are happening. Here are my few tips.

  1. function maxRepeatingLetter() is updating the counter table letter[] each time the character appears.
  2. After that, the code tries to find the highest number in the counter table letter[].
  • Related