Zach Smith
Zach Smith

Reputation: 5674

How to Check for Certain Characters in C

I am writing a script where I need to make sure they only put in certain characters. These include "x", "/", "+", "-", "%" (basic math operators), every letter from a -z and every number. I have the following below that only checks for alpha and number. How can I check that only certain one are used, and everything else, such as "&" or ">", are correctly error handled?

//check to see if user has input an incorrect symbol or letter
    if (isalpha(symbol) || isalnum(symbol)) 
    {
        printf("You must enter a math operator, not a letter or number. \n \n");
    }
    else {//move along nothing to see here
    }

Upvotes: 1

Views: 6560

Answers (6)

Roland Rabien
Roland Rabien

Reputation: 8836

Make a string with all the allowed characters and then check the string.

char* ok = "/+-*%";

if (isalpha(symbol) || isalnum(symbol) || strchr(ok, symbol) == NULL) 
{
    printf("You must enter a math operator, not a letter or number. \n \n");
}
else {//move along nothing to see here
}

Upvotes: 9

Jonathan Leffler
Jonathan Leffler

Reputation: 753725

Implementing the idea of TheUndeadFish:

int isMathOperator(int c)
{
    static char symbols[257] =
    {
        ['+'] = 1, ['-'] = 1, ['/'] = 1, ['x'] = 1,
        ['='] = 1, ['%'] = 1, ...
    };
    assert(c == EOF || (c & 0xFF) == c);
    return((c == EOF) ? 0 : symbols[c]);
}

Note that like the isxxxx() macros/functions in <ctype.h>, this function accepts any valid 8-bit character or EOF. It uses the C99 mechanism for initializing specific elements of an array.

Upvotes: 0

DigitalRoss
DigitalRoss

Reputation: 146073

The general answer to this kind of question in C is that you do what would be done behind the scenes in a language with elaborate string handling: you examine each character and process it in open code.

Having said that, there are now two ways to process each character:

  • use a multi-way if or index a string of valid characters, probably with strchr(3)
  • use a lookup table, i.e., x['a'] = 1, if(x[i]) ...

And having said that, there is a hybrid approach which uses a preconstructed lookup table that is part of every C library since before C89, called ctype.h. The man pages for this are found under isalpha(3), use man 3 isalpha on unix and google or msdn if under windows.

Upvotes: 1

pmg
pmg

Reputation: 108988

I think you have to check every input character by itself. strchr can help

/* code untested. I don't have a compiler available at the moment */
/* input = "123 / 14 + x - 5"; */
char *pinput = input;
while (*pinput) {
    if (!strchr("+-*/% abcdefghijklmnopqrstuvwxyz0123456789", *pinput)) {
        /* unacceptable character */
        break;
    }
    ++pinput;
}
if (*pinput != '\0') {
    fprintf(stderr, "Invalid input\n");
}

Upvotes: 1

TheUndeadFish
TheUndeadFish

Reputation: 8171

Write your own isMathOperator function that returns true for that symbols you want to allow.

Upvotes: 3

&#211;lafur Waage
&#211;lafur Waage

Reputation: 70001

If it's a char, then you do something like this

if(charVariable == '+')

These need to be in single quotes.

Upvotes: 0

Related Questions