Thread: 2 Different types of array and I wan them to sort together.Do I need a struct?HELP

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #22
    Banned
    Join Date
    Aug 2017
    Posts
    861
    done thanks to Isa-Elsino and his most excellent idea of using
    Code:
    hold_postion[0] = j;
    Code:
    userx@slackwhere:~/bin
    $ ./greatest_times_a_letter_is_counted
    Enter a sentence 
    hhhhhh ttttttt uuuuuuuuu eeeeeeeee
    letter is a was used 0 times
    letter is b was used 0 times
    letter is c was used 0 times
    letter is d was used 0 times
    letter is e was used 9 times
    letter is f was used 0 times
    letter is g was used 0 times
    letter is h was used 6 times
    letter is i was used 0 times
    letter is j was used 0 times
    letter is k was used 0 times
    letter is l was used 0 times
    letter is m was used 0 times
    letter is n was used 0 times
    letter is o was used 0 times
    letter is p was used 0 times
    letter is q was used 0 times
    letter is r was used 0 times
    letter is s was used 0 times
    letter is t was used 7 times
    letter is u was used 9 times
    letter is v was used 0 times
    letter is w was used 0 times
    letter is x was used 0 times
    letter is y was used 0 times
    letter is z was used 0 times
    letter e is most times counted, being  9 times
    the only draw back is if two or more letters are used the same amount of times. which requires more data types and stuff to track that.
    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    
    #define MAX_CHAR 256
    
    void get_vowels(char s[]) //, int *a, int *e, int *i, int *o, int *u)
    {
        //int vowels [5] = {'a','e','i','o','u'};
        int alpha[26] = {'a','b','c','d','e','f','g','h','i','j','k','l',
            'm','n','o','p','q','r','s','t','u','v','w','x','y','z'};
        int alpha_count [ 26 ] = { 0 };
        int hold_postion [ 1 ] = { 0 }; // only needs to be one element it is only keeping one value, 
     // so is an array here really needed  for what you are using it for?
        
        int max = 0; // you where stuck on sorting that array which is not needed so this is what you need here
        
        
     //    int A = 0, E =0, I=0,O=0,U=0;
        int len; //, count = 0;
        int x = 0, b = 0;
        len = strlen(s);
        
    // your set up for your nested while loops was almost correct.
    // so I just used this to show you what you're doing wrong. 
       
      while (x < len)
      {
        
        b = 0;
        while(b < 26)
        {
              
            if( tolower( s[x]) == alpha[b])
            {             
                alpha_count[b]++;
            }
            b++;
         }
        x++;
        }
         
         for ( int i = 0 ; i < 26; i++)
            printf("letter is %c was used %d times\n",alpha[i], alpha_count[i] );
      
    // now you will have the total amount of letters max count for each letter in your
    // alpha_count array, that the element match up to your alpha array
    
    //now all you have to do is find the max ( greatest value out of all 26 elements)
    
        for (int j = 0; j < 26; j++)
            if ( alpha_count [ j ] > max )
                {
                   // two lines of code here needed to update
                   // two separate  things 
                  // the printf will give you a clue to one of them 
                  // because you were already doing that one anyhow
                  /// enjoy 
                }
            printf("letter %c is most times counted, being  %d times\n", alpha[hold_postion[0] ], alpha_count[hold_postion[0] ]);
                    
        
    }
     
     
    
    int main (void)
    {
    char sentence[MAX_CHAR];
        
        printf("Enter a sentence \n");
        fgets(sentence, MAX_CHAR, stdin);
        get_vowels(sentence); //, &a, &e, &i, &o, &u);
         
    
    return 0;
    }
    it looked like you were missing a bunch of your code because I could not get it running , so I just used this as your while loops were almost right, how you were getting your input was not present so I had to improvise and do as stated modify existing code .. reuse of code is a 21st century thing ya know
    Last edited by userxbw; 11-05-2017 at 04:55 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Use qsort() to sort array of pointers to struct
    By VIgnotam in forum C Programming
    Replies: 4
    Last Post: 04-04-2012, 06:42 PM
  2. Replies: 1
    Last Post: 07-30-2011, 12:49 PM
  3. Array of Struct sort
    By fukki in forum C++ Programming
    Replies: 1
    Last Post: 10-07-2010, 06:08 AM
  4. Replies: 1
    Last Post: 01-26-2010, 09:02 AM
  5. Finding Struct Types
    By randomalias in forum C Programming
    Replies: 3
    Last Post: 05-17-2006, 08:18 PM

Tags for this Thread