As a part of a my task I need a C code using only the stdio.h libary that get a pointer to a text string (made out of letters, gaps, numbers etc.) and return the letter which is shown the most times in the string (the letter has to be printed a s a capital letter even if its not. exmple: c is printed as C).
If more than one letter has the same number appearances, the function will print the letter closer to the beginning of the alphabet.
What I wrote so far is a function that gets a string and counts the numer of times a letter is shown in it:
#include <stdio.h>
/*Q1 Part A: Count_Letter*/
int Count_Letter (char* Str, char letter)
{
if ((letter >= 'A' && letter <= 'Z') || (letter >= 'a' && letter <= 'z'))
{
int i=0, count=0;
while (Str[i] != '\0')
{
if ((Str[i] >= 'a' && Str[i] <= 'z') || (Str[i] >= 'A' && Str[i] <= 'Z'))
{
if (Str[i] == letter)
count++;
else
{
if (letter <= 'Z')
{
if (Str[i] == letter + 32)
count++;
}
else
if (letter >= 'a')
{
if (Str[i] == letter - 32)
count++;
}
}
}
else
return 0;
i++;
}
return count;
}
else
return 0;
}
char Popular_Letter (char* Str)
{
/*Add the relevant changes*/
/**************************/
}
/******MAIN******/
void main()
{
int i;
/*Q1A*/
printf("%d\n",Count_Letter("Jim loves pizza",'z'));/*2*/
printf("%d\n",Count_Letter("Jim loves pizza",'Z')); /*2*/
printf("%d\n",Count_Letter("I can't believe it's not butter!!!",'y')); /*0*/
printf("%d\n",Count_Letter("I can't believe it's not butter!!!",'!')); /*-1*/
/*Q1B*/
printf("%c\n",Popular_Letter("Jim loves pizzzza")); /*z*/
printf("%c\n",Popular_Letter("Jim loves pizza")); /*i*/
}
And I want to have the popular letter function ( char Poular_letter (char* Str) ) as a different function, using the first one I wrote. the instructions say that i must use the first function (count_letter) in the second one (popular_letter).
hope I wrote it right :] english isn't exactly my native language
btw
i just tested the first function and found it that it dosent work :( it prints zeros instead of
2
2
0
-1
can you please help me with the second function and with finding out what went wrong with the first?


Sign In
Create Account


Back to top









