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?
 
AlexVirochovskyConnect With a Mentor Commented:
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
 
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
All Courses

From novice to tech pro — start learning today.