+ 1

Program that prints an histogram

So I was doing a program that creates a histogram whose columns are made by "#" symbols, each one for each character of each word. I was able to count how many words are in input, so I was able to print the number of columns made by this symbol "|" and even the characters, the problem is that the characters counter just count every character in the string (space, tabs, and newlines too of course). I'd like to count the characters of the words separately, any suggestions? https://code.sololearn.com/cAApY46K6M7Q/?ref=app

7th Nov 2019, 3:33 PM
Gandalf the Grey
Gandalf the Grey - avatar
11 Antworten
+ 2
~ swim ~ I'm studying C at university, I'm a beginner student so I don't know much functions, these you told me for example I didn't know the existence. I'm gonna give it a try
7th Nov 2019, 4:03 PM
Gandalf the Grey
Gandalf the Grey - avatar
+ 1
If you take a look at the ascii table, you see that the lower and upper alphabet are in a bunch each. You can make use of that. To manually check, if your symbol is from the alphabet, you can test for (c>='a' && c<= 'z') (and the same for capitals, if you need it).
7th Nov 2019, 3:49 PM
HonFu
HonFu - avatar
+ 1
I found the solution of the exercise; googling I discovered that there is a version of my textbook with the resolution of all the exercises. The fun fact is that now I'm more confused than before because the solution isn't what I expected. Here is the solution code: http://imgur.com/a/32JwE94 I wrote it down if anyone want to give it a try: https://code.sololearn.com/cwBHEoRCttfl/?ref=app Anyway thanks to ~ swim ~ I was able to make my *first* code smoother using the isalpha(c) isspace(c) functions
7th Nov 2019, 10:19 PM
Gandalf the Grey
Gandalf the Grey - avatar
0
Maybe you can skip counting the spaces by doing something like- if(c == ' '){ continue; } I'm not sure whether this will work or not but you can try.
7th Nov 2019, 3:51 PM
Avinesh
Avinesh - avatar
0
@HonFu Did you mean to use an array? @Avinesh The problem is that skipping the spaces doesn't interrupt the counter, in the end it'll print anyway the numbers of all characters (spaces excluded)
7th Nov 2019, 3:54 PM
Gandalf the Grey
Gandalf the Grey - avatar
0
First of all, your program misses the histogram. An histogram, in C, is an array of counters. In your case histo[i] is the number of words with length 'i' characters. Then, you do not need a 'state' variable. To avoid problems, we must make things as simplest as possible. You only need to know when you are exiting a word, for resetting the number of characters and updating the histogram. And here is your rewritten program https://code.sololearn.com/cHlfr7pm8G73/?ref=app
8th Nov 2019, 5:13 AM
Bilbo Baggins
Bilbo Baggins - avatar
0
Bilbo Baggins First of all thanks for your reply secondly, the state variable was inserted by my book, all I did was copy down the whole text in a code to see if it works. I also wrote it again by myself using isspace(c) and isalpha(c) to know if I'm inside or outside a word and the code gets smoother without state variable. Anyway, you're program is great but is it possible to arrange the columns in the order in which the words are arranged? Ex Input: Hello from C Output: |##### (1 word "Hello" length 5 chars) |#### (2 word "from" length 4 chars) |# (3 word "C" length 1 char)
8th Nov 2019, 6:53 AM
Gandalf the Grey
Gandalf the Grey - avatar
0
Your program 1.0.c has a bug: it does not take into account the last word. Arrange in the order in which the words are inserted? It does not make sense in an histogram, where all the words with equal length must be grouped together in a single entry. Finally: the solution proposed in your book, which uses 3 states (beginning of word, end of word, inside word) is too redundant. Only two states are enough: beginning of word, inside word.
8th Nov 2019, 7:02 AM
Bilbo Baggins
Bilbo Baggins - avatar
0
Bilbo Baggins The 1.0 program is the solution
8th Nov 2019, 7:11 AM
Gandalf the Grey
Gandalf the Grey - avatar