2

I am new to C programming. One of my assignment questions is giving me a hard time. Here it is:

Write an ANSI-C program that uses getchar() to read characters from the standard input, and uses putchar() to output only the letters, spaces (' ') and newlines in the input to the standard output. If the letters are lower case letters, your program should convert them into upper cases. For example, given the following input:

There are 6 apples and 8 oranges, also 9 bananas ...... @ Apple Store!! See you there!?
the output of your program should be:
THERE ARE APPLES AND ORANGES ALSO BANANAS APPLE STORE SEE YOU THERE

I can get the capitalization part right but am having a hard time with ignoring numbers and any other character. Any help would be much appreciated.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int c;

    while ((c=getchar())!=EOF) {
        if ((c>='a' && c<='z'))
            c -= 32;
        else
            while((c==getchar())<'a' || (c==getchar())>'z' ||(c==getchar())!='\n' ||(c==getchar())!=' ');  //This is where I am having trouble.
        putchar(c);
    }
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70

3 Answers3

2

Use isalpha, isspace and toupper in <ctype.h> like this

while ((c=getchar())!=EOF) {
    if(isalpha(c) || isspace(c))// isspace allow '\t' => c == ' ' || c == '\n'
        putchar(toupper(c));
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
  • This is exactly what would do what you want, in the most simple manner possible. It will ignore everything thats not a letter. It also pushes away the multiple branch checks that you've placed in your while. Just make sure that you include that ctype library. The functions written below this answer, at the time, would be good if you want to fully understand what is done underneath, but this is what you'd want, if this is a submission point. Also not too sure if you want to make sure that your output does not contain multiple spaces, that would require a bit of finagling. – L.P. Sep 26 '15 at 23:47
0

Just use the isdigit function to check if a character is a digit or not. If it is not a digit, process it as you seem to do now. Otherwise, just ignore it and try to read another character.

Paul92
  • 8,827
  • 1
  • 23
  • 37
0

You can use something like that:

char char_filter(char c)
{
    /* lower case letters */
    if (c >= 'a' && c <= 'z')
        return c - ('a' - 'A');

    /* upper case letters*/
    if (c >= 'A' && c <= 'Z')
        return c;

    /* space and new line */
    if (c == ' ' || c == '\n')
        return c;

    /* other characters */
    return 0;
}

Here if the function returns zero the char should be skippted otherwise it should be printed by putchar:

char c;
while ((c = getchar()) != EOF) {
    if ((c = char_filter(c)))
        putchar(c);
}

Note that there are also standard functions int islower(int c), int isupper(int c) and int isspace(int c). The function isspace() considers as a space also '\t', '\n' and some other characters.

Orest Hera
  • 6,706
  • 2
  • 21
  • 35