Hi
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:
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).Code:#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*/
}
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 workit 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?
Do you mean non-letter character in the string should be ignored?
Does case matter when counting letters?
In any case, you can use a
Code:unsigned count_of_charater[256]; // initialized all to 0 // for each char c in the string, ++count_of_character[c]; // go through count_of_character, remember the first index that has the greatest value // output it as Capital letter // you;re done.
If any answers to my questions is yes, you may (in the latter case, must combine) use smaller buffer, but that should not matter that much.
And yes, it can be done in some other way, but probably not as efficiently.
Two issues:
1) if the letter isn't in a-z or A-Z, you return 0 instead of -1
2) if Str[i] isn't in a-z or A-Z you return 0 instead of moving to the next i.
thanks, fixed and improved!
Code:#include <stdio.h>
int Count_Letter (char* Str, char letter)
{
char opposite;
if(letter >= 'A' && letter <= 'Z') opposite = letter + 32;
else if(letter >= 'a' && letter <= 'z') opposite = letter - 32;
else return -1;
int i=0, count=0;
while (Str[i] != '\0')
{
if(Str[i] == letter || Str[i] == opposite) count++;
i++;
}
return count;
}
/******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*/
scanf ("&d", &i);
}
but im still lost on the second function.
the trick is that it MUST use the first function that counts how many times a letter is shown in a string.
"an example for the popular letter function:
Given the following text string:
I was promoted to a new position at work today.
O is the most popular letter
"
the Poular_letter function (aka second function) needs to call the Count_letter function (aka first function), receive how many times each letter appears in the text and return the capital letter of the letter that is shown the most times.. how the hell..
thanks, fixed and improved!
Code:#include <stdio.h>
int Count_Letter (char* Str, char letter)
{
char opposite;
if(letter >= 'A' && letter <= 'Z') opposite = letter + 32;
else if(letter >= 'a' && letter <= 'z') opposite = letter - 32;
else return -1;
int i=0, count=0;
while (Str[i] != '\0')
{
if(Str[i] == letter || Str[i] == opposite) count++;
i++;
}
return count;
}
/******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*/
scanf ("&d", &i);
}
but im still lost on the second function.
the trick is that it MUST use the first function that counts how many times a letter is shown in a string.
"an example for the popular letter function:
Given the following text string:
I was promoted to a new position at work today.
O is the most popular letter
"
the Poular_letter function (aka second function) needs to call the Count_letter function (aka first function), receive how many times each letter appears in the text and return the capital letter of the letter that is shown the most times..
I know it has something to do with
but how can i develop it to what i need?Code:
for(int i=0; i<27; i++) {
countAll[i] = Count_Letter (Str, 'A'+i);
}
Make it simple:
have two variables, current_letter and current_count. If you get a higher count, update current_count and current_letter. You'll also need temp_count.
The key is, you don't need to know all the letters' frequencies, just the highest.Code:for (char letter='a'; letter <='z'; letter++) { //do work }
Your functions can be fixed, and it will work as designed. And indeed you're encouraged to submit you assignment the way you're doing because it more faithfully reflected your current level.
The algorithm proposed by me, however, is expected to be more effecient. But don't worry about that yet, you'll come to know that very soon.
Please don't submit as your assignment.
Code:void find_most_occurred_char(const char * str) { int cnts[256]; char c; for(int i=0; i<256; ++i) cnts[i]=0; // go through the string once, accumulator occurance // of each characters. // while( (c=*str)!='\0' ) ++ cnts[c]; // go through the cnts array the find the one with greatest occurance int best_cnt=0, best_index=-1; for(int i=0; i<256; ++i) if(cnts[i]>best_cnt) { best_cnt=cnts[i]; best_index=i; } // now best_cnt has the best count, and best_index, if transformed to a char // and capitalized, is the char you 're going to output // if only letter is concerned, tune the code slightly. // the remaining part is left as blank on purpose. }
char c;
should be
unsigned char c;
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks