Jump to content

letter counter - C function !

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
10 replies to this topic

#1
sharonf

sharonf

    Newbie

  • Members
  • PipPip
  • 17 posts
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:


#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?

#2
Lance

Lance

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 276 posts
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
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.




#3
Lance

Lance

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 276 posts
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.

#4
sharonf

sharonf

    Newbie

  • Members
  • PipPip
  • 17 posts

Lance said:

Do you mean non-letter character in the string should be ignored?
if its a non-letter theres suppose to be a printf that says so (i have to confirm that its a letter and print en error if its a non-letter. good point! forgot about that)


Does case matter when counting letters?
in the count_letter function casing dosent matter


In any case, you can use a

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.




how does that translate into code?
and what went wrong with the function that i wrote?

#5
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
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.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#6
sharonf

sharonf

    Newbie

  • Members
  • PipPip
  • 17 posts

WingedPanther said:

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!


#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..

#7
sharonf

sharonf

    Newbie

  • Members
  • PipPip
  • 17 posts

WingedPanther said:

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!


#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



for(int i=0; i<27; i++) {

   countAll[i] = Count_Letter (Str, 'A'+i);

}



but how can i develop it to what i need?

#8
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
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.

for (char letter='a'; letter <='z'; letter++)
{
  //do work
}
The key is, you don't need to know all the letters' frequencies, just the highest.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#9
Lance

Lance

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 276 posts
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.
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.
}


#10
Lance

Lance

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 276 posts
char c;
should be
unsigned char c;

#11
sharonf

sharonf

    Newbie

  • Members
  • PipPip
  • 17 posts

Lance said:

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.

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.

}


this is way beyond my level.. I cant eve understand the function and I cant realy use it anyway because its DOSENT use the first function (Count_letter) as its suppose to.

the task is more about using the first function in the second one (and after that using the second function in a third function), not creating a function that stands on its own.
thanks for trying though.. you too panther