Can anyone help with this program. Both the functions work fine separately but when for example, I tried to use "if" for simplicity the islower came out as 0 when all the characters were printed out, because it's second in the flow of control. It would also help if I could put this program for counting upper and lowercase into a function with pointers using the function prototype below. I want to be able to return the character that occurs the least but at least once, and the character that occurs the most, taking upper and lowercase separately.

I was redirecting the string from a text file, not sure if that is important.

Thanking you in advance.

void search(char s[], char *p_least, char *p_most, int *p_least_count, int *p_most_cnt);
while ((c = getchar())!=EOF)
      if(isupper(c)){
      	++letter2[c-'A'];}
	if(islower(c)){
      	++letter1[c-'a'];}
#include <stdio.h>
#include <ctype.h>

int main(void)

{

int c,i,j,letter[26],letter2[26];


for(j=0;j<26;++j)
	letter2[j]=0;

   while ((c = getchar())!=EOF)
      if(isupper(c)){
      	++letter2[c-'A'];}

for(j=0;j<26;++j){
	if(j%6 ==0)
    printf("\n");{

   printf("%2c:%2d", 'A' + j,letter2[j]);
}}
printf("\n\n");


for(i=0;i<26;++i)
	letter[i]=0;

      while ((c = getchar())!=EOF);
      if(islower(c)){
      	++letter[c-'a'];}


for(i=0;i<26;++i){
	if(i%6 ==0)
    printf("\n");{


   printf("%2c:%2d", 'a' + i,letter[i]);

}}
printf("\n\n");

return 0;
}

I assume you are trying to count the number of lowercase and uppercase letters entered via stdin and load the counts into two separate arrays.

You need to load the two arrays within the while loop whilst reading characters in from stdin. I would also change the loop condition to check for a newline character as well.

One more thing, using expressions such as c- 'A' and 'A' + j is generally not a good idea. It will work for the ASCII character set but it would break for other character sets. Since this is more than likely an assignment and that's the not so good way teachers and books tend to code these character counting type of programs, we'll leave that discussion for another time.

Since I'm providing code below that corrects your initial issue, I'll leave you with sorting out the details of the search function. At least put some code together and post back if you're having difficulties with it.

#include <stdio.h>
#include <ctype.h>

int main(void) {

    int c, i, j, letter[26], letter2[26];

    /* initialise arrays that hold counts */
    for (j = 0; j < 26; ++j)
        letter2[j] = 0;

    for (i = 0; i < 26; ++i)
        letter[i] = 0;

    /* read characters from stdin and count */
    while ((c = getchar()) != '\n') {
        if (isupper(c)) {
            ++letter2[c - 'A'];
        }

        if (islower(c)) {
            ++letter[c - 'a'];
        }
    }

    /* print tables displaying counts of each letter entered */
    for (j = 0; j < 26; ++j) {
        if (j % 6 == 0) {
            printf("\n");
        }

        printf("%2c:%2d", 'A' + j, letter2[j]);
    }
    printf("\n\n");

    for (i = 0; i < 26; ++i) {
        if (i % 6 == 0) {
            printf("\n");
        }

        printf("%2c:%2d", 'a' + i, letter[i]);
    }
    printf("\n\n");

    return 0;
}

Cheers,
JD

You have a ; you don't want at the end of the while statement:

while ((c = getchar())!=EOF);
      if(islower(c)){
      	++letter[c-'a'];}

Great, thanks a lot. Much appricated! I can work on the function from here.

This question has already been answered. Start a new discussion instead.