A
A
Andrey Romashev2022-02-24 14:22:22
C++ / C#
Andrey Romashev, 2022-02-24 14:22:22

How to add a check for the absence of spaces in C?

Hello, I have a code that takes a serial input of characters as input and converts the characters to hexadecimal. ignoring spaces.

#include <stdio.h>

int decod (void);

int main() {
    decod();
}


int decod (void) {
    int s;
    while ((s = getchar()) != '\n') {
        if (s != ' ') {
            printf("%X ", s);
        }
    }
}
}


the code accepts WORLD as input and gives 57 4F 52 4C 44 as output
How to make a check that the program gives an error message at the WORLD input? (you cannot enter characters without spaces)
Thanks in advance

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Skusnov, 2022-02-24
@AlexSku

Set the error flag before checking. At the first space (more precisely, at each) reset the flag.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question