Closed Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: letter counter - C function !

  1. #1
    sharonf's Avatar
    sharonf is offline Newbie
    Join Date
    Jan 2009
    Posts
    17
    Rep Power
    0

    letter counter - C function !

    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:

    Code:
    #include <stdio.h>


    /*Q1 Part A: Count_Letter*/
    int Count_Letter (charStrchar letter)
    {
        if ((
    letter >= 'A' && letter <= 'Z') || (letter >= 'a' && letter <= 'z'))
        {
            
    int i=0count=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 (charStr)
    {
        
    /*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. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Lance is offline Programming Professional
    Join Date
    Dec 2008
    Posts
    276
    Rep Power
    13

    Re: letter counter - C function !

    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.

  4. #3
    Lance is offline Programming Professional
    Join Date
    Dec 2008
    Posts
    276
    Rep Power
    13

    Re: letter counter - C function !

    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.

  5. #4
    sharonf's Avatar
    sharonf is offline Newbie
    Join Date
    Jan 2009
    Posts
    17
    Rep Power
    0

    Re: letter counter - C function !

    Quote Originally Posted by Lance View Post
    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
    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.
    how does that translate into code?
    and what went wrong with the function that i wrote?

  6. #5
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: letter counter - C function !

    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

  7. #6
    sharonf's Avatar
    sharonf is offline Newbie
    Join Date
    Jan 2009
    Posts
    17
    Rep Power
    0

    Re: letter counter - C function !

    Quote Originally Posted by WingedPanther View Post
    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 (charStrchar 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=0count=0;
        
        while (
    Str[i] != '\0')
        {
            if(
    Str[i] == letter || Str[i] == oppositecount++;
            
    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..

  8. #7
    sharonf's Avatar
    sharonf is offline Newbie
    Join Date
    Jan 2009
    Posts
    17
    Rep Power
    0

    Re: letter counter - C function !

    Quote Originally Posted by WingedPanther View Post
    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 (charStrchar 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=0count=0;
        
        while (
    Str[i] != '\0')
        {
            if(
    Str[i] == letter || Str[i] == oppositecount++;
            
    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

    Code:

    for(int i=0i<27i++) {
       
    countAll[i] = Count_Letter (Str'A'+i);

    but how can i develop it to what i need?

  9. #8
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: letter counter - C function !

    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.

    Code:
    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

  10. #9
    Lance is offline Programming Professional
    Join Date
    Dec 2008
    Posts
    276
    Rep Power
    13

    Re: letter counter - C function !

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

  11. #10
    Lance is offline Programming Professional
    Join Date
    Dec 2008
    Posts
    276
    Rep Power
    13

    Re: letter counter - C function !

    char c;
    should be
    unsigned char c;

Closed Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. TASM displaying a letter
    By cesarg in forum Assembly
    Replies: 1
    Last Post: 09-06-2011, 12:22 PM
  2. how to 'test' entered letter.
    By 2710 in forum Pascal and Delphi
    Replies: 4
    Last Post: 02-07-2010, 05:33 AM
  3. One letter variables
    By SimonBoris in forum General Programming
    Replies: 5
    Last Post: 11-12-2009, 04:57 PM
  4. Cap only first letter
    By Chan in forum C# Programming
    Replies: 5
    Last Post: 08-19-2006, 03:46 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts