1

I have an array of size 25 that stores capital letters in an array. It stores, for my user input test, 'A', 'B', 'C', and 'D'. My program counts the amount of those letters in the array and prints it out. For example, if AABBCCDD was entered, it would say there are 2 A's, 2 B's, 2 C's and 2 D's. Now, I am trying to get it to accept lower case letters as well and count them as long as they are the same as the uppercase. For example, if I entered, aA,Bb, cC and dD, it would still print that there are two of each. MY code is the following:

int main(void)
{
    char array[25];

    printf("Enter array: \n");
    scanf("%[^\n]", array);
    printf(Array is: ", array);

    count(array, 'A');
    return 0;
 }
 void count(char* array, char p)
 {    
 int i, count = 0;
 for (i = 0; array[i] !='\0'; i++)
   if(array[i] == p)
     count++;
 printf("Number of %c's: %d\n, p, count);
 }

I want the code to count both 'a' and 'A'. Is using toupper() the approach?

CodeFreak
  • 90
  • 1
  • 2
  • 15
  • `p = toupper((unsigned char)p);` before loop .. `if(toupper((unsigned char)array[i]) == p)` – BLUEPIXY Nov 10 '16 at 05:56
  • Exactly correct! thank you – CodeFreak Nov 10 '16 at 06:04
  • @CodeFreak If that's what you have, and it works, then what is the question? – Potatoswatter Nov 10 '16 at 06:07
  • @BLUEPIXY any idea why there is a warning for implicit declaration of function toupper? – CodeFreak Nov 10 '16 at 06:10
  • 1
    @CodeFreak have you included `ctype.h`? – babon Nov 10 '16 at 06:14
  • @babon I accidentally had it as ctye.h, so yes that was the problem. Thanks! – CodeFreak Nov 10 '16 at 06:16
  • Is `&` both upper case and lowercase or neither? – Ed Heal Nov 10 '16 at 06:40
  • this line: `for (i = 0; array[i] !='\0'; i++)` says to exit the loop when entry contains '\0' However, the array is not initialized to a known value and could have a '\0' anywhere within the array. Suggest: `char array[25];` be changed to: `int array[25] = {0};` and this: `for (i = 0; i < (sizeof(array_/ sizeof(int); i++)` be changed to: `for (i = 0; array[i] !='\0'; i++)`. There are several other problems with the posted code, Of most importance is the posted code does not compile, due to several syntax errors. – user3629249 Nov 11 '16 at 06:57
  • it is a very poor practice to make the name of a function be the same as a local variable inside the same function. – user3629249 Nov 11 '16 at 06:59
  • strongly suggest read only one character at a time, treat `array[]` as an array of integers, use the read in character as the index into the array, similar to: `array[ toupper(charvalue) - 'A']++;` Then when printing the results ignore array elements that are still 0. When inputting characters to update the array, do not process input characters that are not `isalpha()` – user3629249 Nov 11 '16 at 07:04
  • @user3629249, be careful, as you don't check bounds on `array` which is defined as `array[26]` (well, it's defined erroneously as 25, not allowing for `Z` character counts, but this doesn't affect my comment) you can receive a nonalpha char, like `'0'`, and try to access `array[ '0' - 'A' ]`, which is `array[ 48 - 65 ]` or `array[-17]` and have trouble. – Luis Colorado Nov 14 '16 at 13:36
  • @LuisColorado, did you miss the part of my comment: `When inputting characters to update the array, do not process input characters that are not isalpha()` – user3629249 Nov 14 '16 at 17:54
  • @user3629249, it's not me who must read your comment, it's the asker. If you post some code with errors, it's desirable at least to notify about them in some kind of marginal note, so the problem posted doesn't get worse. You don't say how to process chars not alpha (perhaps @codefreak doesn't know about `isalpha(3)`) – Luis Colorado Nov 15 '16 at 09:20

3 Answers3

1

you just need to replace if(array[i] == p) conditional block with

if(toupper(array[i]) == p)

And if you are not sure about the case of p(p can be any of lowercase or uppercase) the replace it with

if(toupper(array[i]) == toupper(p))

hrishi
  • 443
  • 2
  • 12
0
void count(char* array, char u) //pass only uppercase
 {    
    int i, count = 0;
    char l = tolower(u);      //get the lower
    for (i = 0; array[i] !='\0'; i++) {
        if(array[i] == u || array[i] == l)
        count++;
    }
    printf("Number of %c's: %d\n, p, count);
 }

BUT! For 26 letters of the alphabet, you are going over the string 26 times. Imagine the string would be 100+ characters long.
Try and make a solution that only goes over the string once.

Poody
  • 325
  • 3
  • 13
0

Just replace the if in for loop with below snippet and it will solve your problem.

if(toupper(array[i]) == p)

In this case everytime the comaprison will be with uppercase of character and as you are passing the char to be counted as argument, make sure you pass it in upper case

I hope it helps.

dips
  • 11
  • 4