Thread: Histogram

  1. #1
    Registered User
    Join Date
    Oct 2018
    Posts
    16

    Post Histogram

    Here is the question :
    Histogram-screen-shot-2019-01-09-9-43-44-pm-jpg


    This is my code that I have made :

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    void count(char str[]);
    
    
    int main()
    {
        char text[80];
        printf("Enter a string : ");
        gets(text);
        count(text);
        return 0;
    }
    
    
    void count(char str[])
    {
        int i, j, count;
        for (i = 0; str[i] != '\0'; i++)
        {
            count = 0;
            for (j = 0; str[j] != '\0'; j++)
            {
                if (str[j] == str[i])
                {
                    count++;
                }
            }
            printf("%c : %d\n", str[i], count);
        }
    }
    My question is, how can i print the character in a type of an array based on their alphabetical arrangement ?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    First, posting "Hi, i wonder that you can help me to solve my problem. please !" as your title for every post isn't helpful.

    > char text[80];
    > printf("Enter a string : ");
    > gets(text);
    1. Your input lines are up to 1000 characters.
    2. There is no mention of prompting for input.
    3. Your first line of input is the number of test cases. These programs are tested automatically, so you need to follow the input file spec closely.
    4. NEVER EVER use gets(). It is totally unsafe. It's even been removed from the latest C standard. Use fgets() instead.


    I suppose you can start with understanding how this works.
    Code:
    int counters[26] = { 0 };
    char c = 'B';  // for example
    counters[c - 'A']++;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Histogram
    By SpecKROELLchen in forum C Programming
    Replies: 9
    Last Post: 08-02-2012, 03:10 AM
  2. Histogram help
    By Fixxxer in forum C Programming
    Replies: 4
    Last Post: 11-12-2010, 02:15 AM
  3. histogram
    By bazzano in forum C Programming
    Replies: 3
    Last Post: 04-04-2007, 12:25 PM
  4. histogram equilization alg.
    By sunoflight77 in forum C++ Programming
    Replies: 1
    Last Post: 04-03-2005, 01:05 AM
  5. Histogram
    By fjf314 in forum C++ Programming
    Replies: 1
    Last Post: 01-15-2004, 11:39 PM

Tags for this Thread