Thread: How to use getchar/gets to store chars in an array, all upper case, in a set amount?

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    7

    How to use getchar/gets to store chars in an array, all upper case, in a set amount?

    Hello there. This is just part of error checking in a much larger program, where I have to use only getchar (if even) / gets to manipulate the array to only store uppercase A-Z characters, with min number of characters being 2, max 10. I guess I'm still not comfortable using these functions. No string library functions may be use. What I have so far is below. Any help would be highly appreciated, thanks in advance.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main()
    {
    	char string[11]; /* gets will replace newline with null, so don't have to initialize
    						string[10] to null*/
    	int iochar;
    
    
    	printf("Input string with uppercase A-Z characters, min 2 max 10:\n");
    	
    	while(iochar = getchar())
    	{
    		if(iochar >= 'A' && iochar <= 'Z')
    		{
    		gets(string);
    		}
    	}
    
    
    	puts(string); /* testing string */
    
    }
    What I have above doesn't do anything, I was just messing around with getchar and such. I tried to set the while loop as while(iochar = getchar() != '\n'), but that gave me some erroneous results. I've also tried setting an if statement to check for lowercase characters, and a printf call to tell the user that the string can't be lowercase, but it would be print that statement (can't be lowercase) every time a single lower case is found. This is just one part of my program that I can't seem to figure out...

  2. #2
    Registered User cph's Avatar
    Join Date
    Sep 2008
    Location
    Indonesia
    Posts
    86
    suggestion
    Code:
    #include <stdio.h>
    
    int main (void)
    {
        int       cnt = 0;
        const int max = 10;
        char      buf[11];
        int       ch;
    
        do {
            ch = getchar();
            if (ch >= 'A' && ch <= 'Z') {
                buf[cnt] = (char)ch;
                ++cnt;
            }
            if (ch == '\n') {
                break;
            }
        } while (cnt < max);
        buf[cnt] = '\0';
        puts(buf);
    
        return(0);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Format Precision & Scale
    By mattnewtoc in forum C Programming
    Replies: 1
    Last Post: 09-16-2008, 10:34 AM
  2. Array Help
    By silverback011 in forum C Programming
    Replies: 7
    Last Post: 08-19-2006, 09:58 PM
  3. Changing bkgrnd color of Child windows
    By cMADsc in forum Windows Programming
    Replies: 11
    Last Post: 09-10-2002, 11:21 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. rand()
    By serious in forum C Programming
    Replies: 8
    Last Post: 02-15-2002, 02:07 AM