• C

alpha numeric field

Hi,

I am looking for a function to check the first four fields of a variable(to ensure they are alpha).  I have handled this in Unix/ksh by comparing the char(s) against '[A-Z][a-z]'.  I understand there are the functions strncmp, strstr, etc.  Yes,  I can use these functions in a loop and get the task done, but I was looking for a slick way to do this task.
LVL 3
knel1234Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AlexVirochovskyCommented:
For test some field you can use next code:

#include <ctype.h>
int IsAlphaString(char *sz)
{
  while (*sz)
    {
      if (!isalpha(*sz))
        return 0; //false
      sz++;
    }
  return 1;//true
}
I hope, it helps. Alex
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
inprasCommented:
Hi
I know what comments will come but still

      char sz[] = "-**MyString";
      char szCpdStr[5];
      strncpy( szCpdStr, sz, 4);
      int n = atoi(szCpdStr);
      if (n == 0 && szCpdStr != '0')
            return TRUE;
      else
            return FALSE
0
knel1234Author Commented:
Hi,

That helps....
With your solution do you know of any compiler/platforms issues. By the way I just need to #define isalpha ((toupper(c) >= 'A' && (toupper((c)) <='Z'))
right?

FYI...
I came up with
BOOL Alpha(const char * inBuffer, int length)
{
   BOOL GoodInput = TRUE;
 
   for (int i=0; i < length; i++)
   {
     char cCurChar = inBuffer[i];
     if (cCurChar == '\0') break;
        if (!(cCurChar >= 'a' && cCurChar <= 'z') && !(cCurChar >= 'A' && cCurChar <= 'Z'))
        GoodInput = FALSE;
    }
    return GoodInput;
}

I can still give you the points.  As it provides me with a different approach to the solution. I really just want to hear your thoughts on any potential compiler/platform issues.
0
ozoCommented:
isalpha is a standard function in
#include <ctype.h>
in the C locale, they are A-Za-z
but did you want isalnum?
0
knel1234Author Commented:
hi,

Actually, I need to check both alpha and numerics for different parts of the variable.  I think have all the info I need.  Thanks guys for the prompt help.

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.