Click here to Skip to main content
15,867,750 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everybody!!!
I use enum for first time & I have a problem.
I want to print the number of the letter(of enum)
See code:

What I have tried:

C++
#include <stdio.h>
#include <string.h>

enum alphabet {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} chosen_letter;

int main(void)
{
    char *my_input;
    //printf("Give a word: ");
    //scanf("%s" , &my_input);
    int numbers_array[strlen(my_input)];
    
    for(int i = 0; i < strlen(my_input); i++)
    {
        numbers_array[i] = 0;   // initialize all array's prices to 0...
    }
    printf("here -> %d", chosen_letter = j);
}
Posted
Updated 16-Sep-20 3:59am
Comments
Rick York 16-Sep-20 11:30am    
I recommend you get a beginner's book on C. I used Scott Meyer's "the C Language" to get started. I think it is out of print now but there are plenty of other books around. Anyway, get a book and work through the exercises in each chapter. Once you get through the book then push yourself a bit farther and come up with a project to do. For me, it was computer graphics stuff and still is. Good luck.

When you declare an enum, you are effectively creating a set of "names" for values within the enum - when you print the name, you get the value not the name itself.
Suppose you have an enum for employees:
C++
enum Employee {CEO, Manager, Peon, Developer};
You create an instance of an Employee, who is a Manager:
C++
enum Employee emp = Manager;
When you print the value:
C++
printf("%d\n", emp);
You don't get "Manager" you get "1" because Manager is the name and 1 is the value (because it's the second definition in the enum, and they start at zero by default) but you can specify values:
C++
typedef enum {
   CEO = 'C', 
   Manager = 'M', 
   Peon = 'P', 
   Developer = 'D'} Employee;
Employee emp = Manager;
Now when you print it, you will get a "human readable" indication:
C++
printf("%c\n", emp);
Will give you "M"
 
Share this answer
 
You are (if you uncomment those lines at the beginning) trying to read a string into a character pointer (which makes no sense at all). And then the last line is trying to print from the expression chosen_letter = j where neither chosen_letter nor j have been declared. So there seems to be quite a bit of code missing. I suggested yesterday that you study the documentation for scanf as you still seem to be making the same mistakes in your usage of it. A good book on the C language (Amazon.com: The C Programming Language, 3rd Edition (C Publishing) (9781691352326): Publishing, C: Books[^]) would also be a useful purchase.
 
Share this answer
 
Try (warning not-robust code!)
C
#include <stdio.h>
  
int main()
{
  char my_input[200];
  int count[26] = { 0 };
  char * p;
  printf("Give a word:");
  scanf("%s", my_input);

  p = my_input;

  while (*p)
  {
    ++count[*p++ - 'a'];
  }

  p = my_input;

  while (*p)
  {
    char c = *p++;
    if ( count[c-'a'] )
    {
      printf("%c occurrences: %d\n", c, count[c-'a']);
      count[c-'a'] = 0;
    }
  }
  return 0;
}
 
Share this answer
 
Comments
Nick_is_asking 16-Sep-20 9:36am    
Nice.
I do not understand what " if ( count[c-'a'] ) " this line does?
What exactly do you check?
Thanks!!!
CPallini 16-Sep-20 9:52am    
This is for correct indexing of the array count (valid indices in such array are 0..25), that is
c = 'a' indexes the array item count[0]
c = 'b' indexes the array item count[1]
...
c = 'z' indexes the array item count[25]
Thank you I found it.
Now I have another problem.
I want to give a string and then count all letters it has
For example ,for string: "aabbllld" ,I want to receive
==========================================================
a = 2
b = 2
l = 3
d = 1
I thought something ,but I got some errors.


What's happening??
Thank you...
C++
<pre>#include <stdio.h>
#include <string.h>

void count_each_letter_of_word(char *word , int **num_array);
enum alphabet {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} chosen_letter;

int main(void)
{
    char my_input[200];
    printf("Give a word: ");
    scanf("%s" , my_input);
    int length_word = strlen(my_input);
    printf("length of %s is %d\n", my_input , length_word);


    int numbers_array[length_word];
    
    for(int ii = 0; ii < length_word; ii++)
    {
        numbers_array[ii] = 0;
        printf("numbers_array[%d] = %d , " , ii , numbers_array[ii]);
    }
    printf("here -> %d|\n", chosen_letter = j);

    count_each_letter_of_word(my_input , numbers_array);
}




void count_each_letter_of_word(char *word , int **num_array)
{
    for(int ii = 0; ii < strlen(word); ii++)
    {
        num_array[ chosen_letter = word[ii] ]++;
    }

    for(int ii = 0; ii < strlen(word); ii++)
    {
        printf("%c = %ls\n" , word[ii] , num_array[ii]);
    }
}


I change something ,but know I have errors to the numbers_array I suppose...
 
Share this answer
 
v3
Comments
Richard MacCutchan 16-Sep-20 6:43am    
You have not allocated any memory space to my_input, so when you call scanf it tries to read into an invalid address, hence SEGV. Please follow my suggestions below and save yourself some time. You cannot learn programming by continually posting questions here, you need to spend some time in serious study.
In my code when I write for example "hellllllool"
it prints
h = 1
e = 1
l = 7
l = 7
l = 7
l = 7
l = 7
l = 7
o = 2
o = 2
l = 7

but I want to print each letter only once.How can I do that?
OK I thought something and it works good.
C++
<pre>#include <stdio.h>
#include <string.h>

void count_each_letter_of_word(char *word , int *num_array , int length);

int main(void)
{
    char my_input[200];
    printf("Give a word: ");
    scanf("%s" , my_input);
    int numbers_array[26] = {0};
    count_each_letter_of_word(my_input , numbers_array , strlen(my_input));
}




void count_each_letter_of_word(char *word , int *num_array , int length)
{
    for(int i = 0; i < length; i++)
    {
        num_array[ word[i] - 97 ]++;
    }

    for(int i = 0; i < length; i++)
    {
        if(num_array[ word[i] - 97] != 0)
        {
            printf("%c = %d\n" , word[i] , num_array[ word[i] - 97]);
            num_array[ word[i] - 97] = 0;
        }
    }
}
 
Share this answer
 
v2
Comments
Richard MacCutchan 16-Sep-20 10:09am    
If you want to use alpha characters as indices then use the actual character, such as 'a', 'D' etc, instead of numbers like 97. That way the intent becomes clear.
Nick_is_asking 16-Sep-20 10:52am    
Thanks!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900