-5

this is the question what asked in my exam is: "write cource code of a character taken from user is alphanumeric or not."

alphanumeric means--> A-Z | a-z | 0-9 (alphabetic or numeric) if it is return true or someting. help me to solve this question please..

in summary we will build isalnum() function ourselves.(with #define macros)

Sillyon
  • 45
  • 1
  • 2
  • 11
  • Google `man isalnum` - hey presto – Ed Heal Jan 10 '18 at 05:56
  • using isalnum() function right ? – Krunal Shah Jan 10 '18 at 05:56
  • no. we need to define isalnum ourselves.(with using #define macro or macros, i don't know.) – Sillyon Jan 10 '18 at 06:00
  • Description and title doesn't match. I'm bit confused, can you edit the question @Sillyon ? – ntshetty Jan 10 '18 at 06:06
  • sorry for that @ThiruShetty. i will try – Sillyon Jan 10 '18 at 06:12
  • 2
    Sure, take the time to learn all you can about macros. It's a great educational endeavor. Then, a year from now, if you have continued to develop as a C programmer, revisit the wisdom of using macros for such purposes and see how much your desire to use them has changed. – David C. Rankin Jan 10 '18 at 06:43
  • This question is described as an exam question and was answered so fast that it could have actually been used in an exam. But you have not done so, have you? It is just your preparation for trying a second time to pass that exam, isn't it? Or maybe you passed that exam and just want to brush up on those questions you could not answer. This is probably an interesting read (for you and for those people who answered fast): https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions – Yunnosch Jan 10 '18 at 08:49

2 Answers2

2

Here is the macro:

#define IS_ALNUM(x) (((x)>='a' && (x) <= 'z')) ||((x)>='A' && (x) <= 'Z')) || (((x)>='0' && (x) <= '9')))

It tests if it is

  • Between a and z
  • Between A and Z
  • Between 0 and 9

Quite simple

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
0

i solved this i think, thanks to all of you. that one is working:

#include <stdio.h>

#define IS_LOWER(x)     ((x) <='z' && (x) >= 'a')  //then returns 1, else returns 0.
#define IS_UPPER(x)     ((x) <='Z' && (x) >= 'A')  //then returns 1, else returns 0.
#define IS_NUMERIC(x)   ((x) <= 9  && (x) >=  0 )  //then returns 1, else returns 0.

#define IS_ALPHANUM(x)  (IS_LOWER(x) || IS_UPPER(x) || IS_NUMERIC(x) ? (x) : (-1))
//then returns x, else returns -1.

int main()
{
    int a;
    a=IS_ALPHANUM('h'); //try h character one for example.
    printf("%d",a);
    return 0;
}

have a great coding days

Sillyon
  • 45
  • 1
  • 2
  • 11