0

I am given a task to write a code that check whether the user input is a white-space character or not. If the input character is a white-space: Print "white-space character" If the input character is not a white-space: Print "not a white-space character" We are currently learning the "ctype.h" library, and learned about it many functions for characters. So I decided to use "isspace()" function in my code since it asking me to check whether the user input is a white-space character or not. Here is my code:

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

int main(){
    char a;
    int output;

    printf("Enter a character: ");
    scanf("%c", &a);
    printf("%c",a);

    output = isspace(a);

    if (output == 0)
    {
        printf("Not a white-space character.");
    }
    else
    {
        printf("White-space character.");
    }
    return 0;
}

However, when I execute the code, I keep recieving that the white-space character such as "\t and "\n" are "not white-character". I noticed that the scanf, when I input "\n" it is only reading the "\" and not "\" and "n" together. I have not learn bool or string part of the code yet, our professor hasn't teached it to us yet so I can't not use those code. So if there is any other way, by not using that, please let me know! Thank you o/

Ryn A
  • 1
  • 5
    You don't enter `\n` by typing \ and `n`. `\n` is a newline and is input with the "ENTER" key. `\t` is a tab and is input with the "TAB" key. – kaylum Apr 13 '20 at 05:57
  • @kaylum Oh, that the kind of input it was looking for to be considered "white-space character"? Yes, when I tried "ENTER" and "TAB" it said they were white-space characters. I have a questions, what about these other white space characters: /f (formfeed), /v (vertical tab), /r (return)? – Ryn A Apr 13 '20 at 06:10
  • 1
    Does this help? https://stackoverflow.com/questions/3091524/what-are-carriage-return-linefeed-and-form-feed – Jerry Jeremiah Apr 13 '20 at 06:17
  • I only know how to do it in a linux shell. I assume there is something similar for windows command line. First look up the [ascii chart](http://man7.org/linux/man-pages/man7/ascii.7.html) for the character you want. For example `\r` has hex value `0x0d`. You can then pass that to your program as `echo -e '\x0d' | ./your_executable` – kaylum Apr 13 '20 at 06:19
  • 2
    You can generate every ASCII character by pressing ALT+. For example form feed is ALT+12. – Roberto Caboni Apr 13 '20 at 06:30

0 Answers0