0

Do help me on this. I'm trying to create a function where the user can input 10 alphabets and the characters will be saved in a array iterated with a for loop. But, I'm having problems with passing the arrays.

#include <stdio.h>
#include <conio.h>

void listAlpha( char ch)
{
   printf(" %c", ch);
   
}
int readAlpha(){
    char arr[10];
    int count = 1, iterator = 0, result;
    for(int iterator=0; iterator<10; iterator++){
        printf("\nAlphabet %d:", count);
        scanf(" %c", &arr[iterator]);
        count++;
    }
    printf("\n-----------------------------------------");
    printf("\nList of alphabets: ");
   for (int x=0; x<10; x++)
   {
       
       /* I’m passing each element one by one using subscript*/
       listAlpha(arr[x]);
   }
    result = findTotal(arr);
   return 0;
}

int findTotal(char arr[]){
    int alpha_a , alpha_b , alpha_c;
    for(int a = 0; a < 10; a++){
        if(arr[a] == "A" || "a"){
            alpha_a + 1;
        }
        else if(arr[a] == "B"){
            alpha_b + 1;
        }
        else if(arr[a] == "C"){
            alpha_c + 1;
        }
    }
    printf("\nTotal alphabet A: %d", alpha_a);
    printf("\nTotal alphabet B: %d", alpha_b);
    printf("\nTotal alphabet C: %d", alpha_c);
    
    
}
int main(){
    readAlpha();
}

I'm trying to pass the array arr[iterator] into findTotal() to count and display the total number of alphabet inputted. Moreover, I shall also have to find the result of the highest number of alphabets entered in the main() function.

The output should look something like this.

Alphabet 1: A
Alphabet 2: B
Alphabet 3: C
Alphabet 4: A
Alphabet 5: B
Alphabet 6: C
Alphabet 7: A
Alphabet 8: B
Alphabet 9: C
Alphabet 10: A
---------------------------------------------------------------------------------
List of alphabets: A B C A B C A B C A
Total alphabet A: 4
Total alphabet B: 3
Total alphabet C: 3

The highest number of alphabets is A.

1 Answers1

0

I use an array to count the number of the existence of each character in the array but the display is repeated a lot of times

int main()
{
char arr[100];
printf("Give a text :");
gets(arr);
int k=strlen(arr);
for(int iterator=0; iterator<k; iterator++)
{
    printf("[%c]",arr[iterator]);
}
int T[k];
for(int i=0;i<k;i++)
{
    T[i]=arr[i];
}
int cpt1=0;
char d;
for(int i=0;i<k;i++)
{int cpt=0;
     for(int j=0;j<k;j++)
     {
          if(T[i]==T[j])
          {
               cpt++;
          }
     }
     if(cpt>cpt1)
     {
          cpt1=cpt;
          d=T[i];
     }
     printf("\nTotal alphabet %c : %d \n",T[i],cpt);
}
printf("\nAlphabet with highest hit is : %c\n",d,cpt1);
}
MED LDN
  • 684
  • 1
  • 5
  • 10
  • 1
    [Why is the gets function so dangerous that it should not be used?](https://stackoverflow.com/q/1694036) – 001 Nov 30 '20 at 14:40
  • @MED LDN thank you for the answer. But it would I would really appreciate if you did fit the code to work perfectly in the code above. – Jitesh Mogana Raja Nov 30 '20 at 15:05