CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

isalpha() for strings ?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
TYSCSTOM



Joined: 03 Jun 2010
Posts: 8
Location: Houston

View user's profile Send private message

isalpha() for strings ?
PostPosted: Tue Jul 06, 2010 7:51 pm     Reply with quote

I am sure this is a simple question, am I am just looking for a simple answer.

I need to read in a string up to 4 digits long and only accept the string as long as none of the characters are alphanumeric.


Code:
         gets(string); //read character from UART//q=getc() 
         printf("\n\rYour entered %s mm\n\r",string);
         wheeldiam=string;
         wheeldiamreal= atol(string);
         if(isalpha(wheeldiamreal)){
            goto test2;
         }


isalpha() always returns 0 in my case, looks like it is meant for use with only a single character rather than a string... is this correct?

What should I use to check the string for characters and reject if found?

thanks in advance
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Wed Jul 07, 2010 1:50 am     Reply with quote

I just spent 5 seconds looking at the CCS help file to find your answer!

Just so you don't have to waste 5 seconds of your life doing the same the answer is YES it only accepts an 8 bit char.

isalpha(char)

PS, the use of goto's in C is bad programming practice. Although some people believe it is OK and some people can't program without using them I personally have never had to use goto when programming in C.

I have had to debug other peoples code that use goto's and it is a nightmare. I tend to "fix" it and remove them Smile
collink



Joined: 08 Jan 2010
Posts: 137
Location: Michigan

View user's profile Send private message Visit poster's website

Re: isalpha() for strings ?
PostPosted: Wed Jul 07, 2010 9:01 am     Reply with quote

TYSCSTOM wrote:

isalpha() always returns 0 in my case, looks like it is meant for use with only a single character rather than a string... is this correct?

What should I use to check the string for characters and reject if found?

thanks in advance


Well, you could create a function that takes a string and calls isalpha. Like so:

Code:

int1 is_alpha_string(char *input) {
   int1 result;
   result = 0;
   while (input && !result) result = isalpha(*(input++));
   return result;
}


I have compiled the above code but not tested it. Use at your own risk.
And, yes, it's horribly terse. But, you get the point.
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

Re: isalpha() for strings ?
PostPosted: Wed Jul 07, 2010 9:18 am     Reply with quote

collink wrote:

Well, you could create a function that takes a string and calls isalpha. Like so:

Code:

int1 is_alpha_string(char *input) {
   int1 result;
   result = 0;
   while (input && !result) result = isalpha(*(input++));
   return result;
}


I have compiled the above code but not tested it. Use at your own risk.
And, yes, it's horribly terse. But, you get the point.


A couple of issues/errors
Currently it will not check the first char.
You need to be checking if the char at input is not null not the address:- while (*input

So
Code:

int1 is_alpha_string(char *input) {
   int1 result;
   result = 0;
   while (*input && !result)
   {
      result = isalpha(*input++); //  (*input++) without the extra ()
    }
    return result;
}
collink



Joined: 08 Jan 2010
Posts: 137
Location: Michigan

View user's profile Send private message Visit poster's website

PostPosted: Wed Jul 07, 2010 9:32 am     Reply with quote

Thanks for catching those mistakes. That's what I get for hammering something out quick.
TYSCSTOM



Joined: 03 Jun 2010
Posts: 8
Location: Houston

View user's profile Send private message

thanks
PostPosted: Wed Jul 07, 2010 11:30 am     Reply with quote

Wayne - thanks for the feedback.

Code:
int1 is_alpha_string(char *input) {
   int1 result;
   result = 0;
   while (*input && !result)
   {
      result = isalpha(*input++); //  (*input++) without the extra ()
    }
    return result;
}


Collink -

Just to be sure I am clear on your code.
You created a pointer called input.
Input stores the adress of the chars 1 at a time ex...a,b,c,d,1,2,3 etc...
While there is an alphanumeric in the string....it continues to increment through "input" which is my string....? correct?

Code:
while (*input && !result)

How can this ever be (1) ? isn't input an adress (ascii character value)?

Please straighten me out on this.

thanks
collink



Joined: 08 Jan 2010
Posts: 137
Location: Michigan

View user's profile Send private message Visit poster's website

Re: thanks
PostPosted: Wed Jul 07, 2010 11:59 am     Reply with quote

TYSCSTOM wrote:


Collink -

Just to be sure I am clear on your code.
You created a pointer called input.
Input stores the adress of the chars 1 at a time ex...a,b,c,d,1,2,3 etc...


Yeah, more or less. It starts out pointing to the first character and is incremented to the next character, then the next, etc.

Quote:

While there is an alphanumeric in the string....it continues to increment through "input" which is my string....? correct?


The while is while there is NO alpha in the string. As soon as it finds one the while loop aborts.

Quote:

Code:
while (*input && !result)

How can this ever be (1) ? isn't input an adress (ascii character value)?


It probably won't ever be 1 but that doesn't matter. "while" will continue so long as it doesn't equal 0. 0 is considered FALSE but basically any other value can stand in for TRUE. So any character code other than 0 will count as true in the statement. TRUE && !result when result is FALSE will be TRUE && TRUE so it keeps going.
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Thu Jul 08, 2010 1:42 am     Reply with quote

As collink pointed out, his code checks for a null, 0, '\0' in the string. A string in C has a null termination char, null, 0, 0x00 and '\0' all mean the same and is used as the null termination char for a C string.

So you MUST only use this routine for strings, char arrays with the null at the end.
The null doesn't have to be the last char in the array, it just indicates the end of the string stored in the array.
TYSCSTOM



Joined: 03 Jun 2010
Posts: 8
Location: Houston

View user's profile Send private message

success
PostPosted: Thu Jul 08, 2010 2:04 pm     Reply with quote

collink - the example works great for my needs, even better than that, I now understand how it works, and have learned something.

I will be using more pointers in my coding from now on.

thanks for the help!
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group