1

I have the following code to count the occurrences of each letter in an input string. The while loop executes correctly. Now I have arr[] containing the number of 'a's in arr[0], 'b's in arr[1] and so on.

However, the following printf does not display the complete contents of arr. (Sample o/p given below).

If I comment out the while loop altogether, printf prints all 26 arr[] values fine! What could I be missing in this simple program?

If it is of any help, I am using VS 2008.

int arr[26]={'\0'};
int i = 0, c = 0, idx = 0, count = 0;
while( (c = getchar()) != EOF ) {
  if( 'a' <= c && 'z' >= c ) {
    idx = c - 'a';
    arr[idx] = arr[idx]+1;
  }
}
for(count = 0; count < 26; count++) {
  printf("arr[%d] = %d\n", count, arr[count]);
}

The outputs that I get (keeps varying for different inputs):

abcxyz
arr[0] = 1
arr[1] = 1
^CPress any key to continue . . .

abcabc
arr[0] = 2
arr[1]
^CPress any key to continue . . .

@Nobilis: Updated code to account for Uppercase letters, also made the print more meaningful :)

int main()
{
    int arr[26]={'\0'};
    int i = 0, c = 0, idx = 0, count = 0;
    while( (c = getchar()) != EOF )
    {
        if( 'a' <= c && 'z' >= c )
        {
            idx = c - 'a';
            arr[idx] = arr[idx]+1;
        }
        if( 'A' <= c && 'Z' >= c )
        {
            idx = c - 'A';
            arr[idx] = arr[idx]+1;
        }
    }
    for(count = 0; count < 26; count++)
    {

        printf("%c or %c = %d\n", ('A'+count), ('a'+count), arr[count]);
    }
}
VivereJay
  • 71
  • 8
  • Is your problem that not all input characters are counted? I've just run your code with input `abcabc` and I got the count correctly: `arr[0] = 2 arr[1] = 2 arr[2] = 2`. Do you get any warnings (apart from the unused `i`) ? – Nobilis Jul 20 '13 at 08:37
  • 1
    Beside the: int arr[26]={'\0'}; Your code is perfectly correct. You should look for the problem on another place. For your array initialisation, you are initializing int with char. Which is perfectly legal C, and works fine, but isn't something you should get accustomed to. – Geod24 Jul 20 '13 at 08:44
  • The input characters are counted fine. If I give input as 'abcxyz', I see (using the VS debugger) arr[0], arr[1], arr[2] and arr[23], arr[24], arr[25] containing 1's. All other arr values are 0. But when I try to print it out, I get the following, incomplete, output: 'abcxzy arr[0] = 1 arr[1] = 1 ^CPress any key to continue . . .' – VivereJay Jul 20 '13 at 08:47
  • 4
    @VivereJay you get `^C` among your inputs? Are you passing EOF correctly? On Windows it is `CTRL+Z`. – Nobilis Jul 20 '13 at 08:49
  • Nobilis, you nailed it! Thanks :) – VivereJay Jul 20 '13 at 08:52
  • @VivereJay No problem I've added it as an answer (but I see Rohan has updated his too). – Nobilis Jul 20 '13 at 08:54

2 Answers2

6

Press CTRL-D on linux and CTRL-Z on windows instead of CTRL-C when you want to end the input characters.

CTRL-C terminates the program, hence no more processing will happen and you won't see the output.

Rohan
  • 52,392
  • 12
  • 90
  • 87
  • Rohan I use `CTR + C` for key – Grijesh Chauhan Jul 20 '13 at 08:48
  • CTRL-D on Unix does not work like CTRL-Z on Windows. The effect of CTRL-D is to signal the end of the input stream only if it is used at the beginning of a line or twice in a row. http://stackoverflow.com/a/1516177/139746 – Pascal Cuoq Jul 20 '13 at 10:51
2

You are not passing EOF correctly, on Windows it is Ctrl + Z. Also, keep in mind that your algorithm will not take into account uppercase letters.

Nobilis
  • 7,310
  • 1
  • 33
  • 67
  • 1
    Added code for taking into uppercase letters into consideration, just because it makes this complete, and might help someone! – VivereJay Jul 20 '13 at 10:37