Here is a possible solution that is close to your original code:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define MAXBUF 10000
int main()
{
printf("Enter: \n");
char sifra[MAXBUF];
int pole[26]={0};
char pismeno, znak;
int i;
int l, max, temp;
int m, x, y;
char abeceda[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'};
fgets(sifra, MAXBUF, stdin);
int n=strlen(sifra);
for(i=0; i<n; i++)
{
pismeno= tolower(sifra[i]);
if(pismeno>=97 && pismeno<=122)
{
pole[pismeno-97]++;
}
}
for(i=97; i<=122; i++)
{
printf("%c --- %d -times\n",toupper(i),pole[i-97]);
}
// Sorting it here
l= 25;
do
{
max=0;
for (x=0; x<=l; x++)
{
if (pole[x]>pole[max])
max=x;
else if (pole[x] == pole[max])
if (abeceda[x] < abeceda[max])
max = x;
}
temp=pole[l];
pole[l]=pole[max];
pole[max]=temp;
znak=abeceda[l];
abeceda[l]=abeceda[max];
abeceda[max]=znak;
l--;
}while (l!=0);
for(y=25; y>-1; y--)
{
printf("%c = %d, ",toupper(abeceda[y]), pole[y]);
}
printf(" \n ");
return 0;
}
The fixed issues and comments:
- As mentioned in the comments you were missing the
ctype.h
library that provides the functions tolower
and toupper
- though I'm assuming this was a typo.
- Similarly as you said, there was a typo in initialization of
abeceda
- it should have 26 characters rather than 25 and the closing bracket was supposed to be square. Note that you can also write char abeceda[] = {your list}
and the compiler will deduce the array size on its own. Stating it explicitly seems like a better idea to me in this case though since we're dealing with a known set - the alphabet, of a known size. Turning it into a separate variable and reusing that variable in the loops also seems like a good choice.
- Like @JonathanLeffler wrote - use
fgets
instead of gets
(and read the posts from the link he provided) - this is implemented along with an upfront defined max buffer size MAXBUF
.
- Instead of printing the letters of alphabet in order in the last loop, you want to print the letters in
abeceda
as sorted.
- Finally, as it was in the OP the sorting effectively mixed up the letters with equal count. What it does now is that it sorts those entries based on the letters alphabetical order which is what I understood was the goal.
main()
should return an int
rather than void
(in fact you return an int
and that would not compile on my machine).
- Some variable names could use improvement, at least in my opinion, while
znak
("sign") makes some sense, pomm
could be simply replaced by temp
. Also careful with one letter variables like x
and and y
(and n
, m
...) - it's better to be a bit more verbose or at least stick to i,j,k which are commonly used in loops. n
would be more readable to me if it was something like nchar
.
- Use indentation to improve readability. I'd also use less spaces in between parts of code.
The output for the word Hello
would now be:
L = 2, E = 1, H = 1, O = 1, A = 0, B = 0, C = 0, D = 0, F = 0, G = 0, I = 0, J = 0, K = 0, M = 0, N = 0, P = 0, Q = 0, R = 0, S = 0, T = 0, U = 0, V = 0, W = 0, X = 0, Y = 0, Z = 0,
If you want to get rid of the last comma, you can also do:
for(y=25; y>0; y--)
{
printf("%c = %d, ",toupper(abeceda[y]), pole[y]);
}
printf("%c = %d ",toupper(abeceda[y]), pole[y]);
printf(" \n ");
One thing to note is that normally you would want to use a more modular design. In this case at least a function for sorting rather than directly typing everything in main
. When you progress in your studies I recommend you start writing functions, the sooner the better. They improve programs readability and ease the debugging.