input redirection

This is a discussion on input redirection within the C Programming forums, part of the General Programming Boards category; Hi, Please find below the code for an exercise I had to do as part of a course book I ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Location
    Dnipropetrovsk, Ukraine
    Posts
    25

    input redirection

    Hi,
    Please find below the code for an exercise I had to do as part of a course book I use to learn C.
    The program is to accept input from an ASCII text file using redirection, count the number of occurrences of each character and output this info on to the screen.
    I would appreciate any suggestions or comments about the code.

    Look forward to hearing from anyone who would care or have the time for that sort of thing.

    Code:
    /* ex14-7.c: Write a program that uses redirection to accept input from a disk file, counts the number of times each letter occurs in the file, and then displays the results on-screen */
    #include <stdio.h>
    
    int main(void)
    {
    	/* declare and initilise an array of type int to hold number of 
    	 * occurences of each character being input. */
    	int input[25] = {0};
    	char input_c[25];
    	/* declare and initialise a variable of type int to hold ASCII 
    	 * value of each character being input. */
    	int c = 0;
    	int i;
    	/* while loop to accept input */
    	while ((c=getchar()) != EOF)
    	{
    		/* switch block to sort out input */
    		switch(c)
    		{
    			case 'a':
    				input_c[0] = c;
    				++input[0];
    				break;
    			case 'b':
    				input_c[1] = c;
    				++input[1];
    				break;
    			case 'c':
    				input_c[2] = c;
    				++input[2];
    				break;
    			case 'd':
    				input_c[3] = c;
    				++input[3];
    				break;
    			case 'e':
    				input_c[4] = c;
    				++input[4];
    				break;
    			case 'f':
    				input_c[5] = c;
    				++input[5];
    				break;
    			case 'g':
    				input_c[6] = c;
    				++input[6];
    				break;
    			case 'h':
    				input_c[7] = c;
    				++input[7];
    				break;
    			case 'i':
    				input_c[8] = c;
    				++input[8];
    				break;
    			case 'j':
    				input_c[9] = c;
    				++input[9];
    				break;
    			case 'k':
    				input_c[10] = c;
    				++input[10];
    				break;
    			case 'l':
    				input_c[11] = c;
    				++input[11];
    				break;
    			case 'm':
    				input_c[12] = c;
    				++input[12];
    				break;
    			case 'n':
    				input_c[13] = c;
    				++input[13];
    				break;
    			case 'o':
    				input_c[14] = c;
    				++input[14];
    				break;
    			case 'p':
    				input_c[15] = c;
    				++input[15];
    				break;
    			case 'r':
    				input_c[16] = c;
    				++input[16];
    				break;
    			case 's':
    				input_c[17] = c;
    				++input[17];
    				break;
    			case 't':
    				input_c[18] = c;
    				++input[18];
    				break;
    			case 'u':
    				input_c[19] = c;
    				++input[19];
    				break;
    			case 'v':
    				input_c[20] = c;
    				++input[20];
    				break;
    			case 'w':
    				input_c[21] = c;
    				++input[21];
    				break;
    			case 'x':
    				input_c[22] = c;
    				++input[22];
    				break;
    			case 'y':
    				input_c[23] = c;
    				++input[23];
    				break;
    			case 'z':
    				input_c[24] = c;
    				++input[24];
    				break;
    			default:
    				break;
    		}
    	}
    	puts("Here's statistics on number of times each characer was encountered in your input:");
    	for (i=0; i<=25; i++)
    		printf("%c was encountered %d times\n", input_c[i], input[i]);
    
    	return 0;
    }

  2. #2
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    you forgot q, also your entire switch statement can be replaced with 2 lines, are you assuming there will be no capital letters?
    Last edited by ಠ_ಠ; 06-23-2009 at 01:41 AM.
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  3. #3
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Each character is a single byte "number", it would be a lot more efficient and clear to just use that as an array index, and then loop through 'a' to 'z' when printing.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Location
    Dnipropetrovsk, Ukraine
    Posts
    25

    thanks for help with question about input redirection

    Quote Originally Posted by valaris View Post
    Each character is a single byte "number", it would be a lot more efficient and clear to just use that as an array index, and then loop through 'a' to 'z' when printing.
    thanks a lot to all those who have responded to my query about input redirection. sorry for the delay in replying to your messages.
    here's the new version of my program. is this what you had in mind? thanks.

    Code:
    /* ex14-7new.c: Write a program that uses redirection to accept input from a disk file, counts the number of times each letter occurs in the file, and then displays the results on-screen */
    #include <stdio.h>
    #include <ctype.h>
    
    int main(void)
    {
    	/* array to hold number of occurances of letters being input */
    	int array[26] = {0};
    	/* i to be used as counter in for loop. c as storage for character input */
    	int i, c;
    
    	while ((c=getchar())!=EOF)
    	{
    		c = tolower(c);
    		if (isalpha(c))
    			++array[c - 'a'];
    	}
    	
    	for (i='a'; i<='z'; i++)
    	{
    		/* print data if letter used in this iteration was encountered */
    		if (array[i]>0)
    			printf("Letter %c = %d times\n", i, array[i - 'a']);
    	}
    	return 0;
    }

  5. #5
    Registered User
    Join Date
    Mar 2009
    Location
    Dnipropetrovsk, Ukraine
    Posts
    25
    Hi. Thanks for replying to my post about input redirection. i've posted the new version of the program based on the input given by yourself as well as another member. Would you mind having a look at it? Just like to be sure this is what people had in mind. thanks.

  6. #6
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by sashaKap View Post
    Hi. Thanks for replying to my post about input redirection. i've posted the new version of the program based on the input given by yourself as well as another member. Would you mind having a look at it? Just like to be sure this is what people had in mind. thanks.
    you might want to consider having the program end when it reaches a new line

    Other than that it looks and works fine, oh

    on the line
    Code:
    if (array[i] > 0);
    I think you meant
    Code:
    if (array[i-'a'] > 0);
    looks much better that way (and you're not blowing the bounds of your array)
    Last edited by ಠ_ಠ; 06-25-2009 at 01:50 AM.
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  7. #7
    Registered User
    Join Date
    Mar 2009
    Location
    Dnipropetrovsk, Ukraine
    Posts
    25
    thanks for your input once again!
    about having the program end when it reaches a new line...
    why is it better? is it because the program assumes that the input will come from the keyboard? i've been feeding input into the program from a text file using this format, executable file < text file.
    thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Input statement problem
    By une in forum C Programming
    Replies: 3
    Last Post: 05-30-2007, 12:16 AM
  2. For loop problems, input please.
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-22-2007, 04:54 AM
  3. I would love some input on my BST tree.
    By StevenGarcia in forum C++ Programming
    Replies: 4
    Last Post: 01-15-2007, 01:22 AM
  4. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 04:42 PM
  5. Help with Input Checking
    By Derek in forum C Programming
    Replies: 7
    Last Post: 06-17-2003, 04:07 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21