View previous topic :: View next topic |
Author |
Message |
TYSCSTOM
Joined: 03 Jun 2010 Posts: 8 Location: Houston
|
isalpha() for strings ? |
Posted: Tue Jul 06, 2010 7:51 pm |
|
|
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
|
|
Posted: Wed Jul 07, 2010 1:50 am |
|
|
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 |
|
|
collink
Joined: 08 Jan 2010 Posts: 137 Location: Michigan
|
Re: isalpha() for strings ? |
Posted: Wed Jul 07, 2010 9:01 am |
|
|
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
|
Re: isalpha() for strings ? |
Posted: Wed Jul 07, 2010 9:18 am |
|
|
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
|
|
Posted: Wed Jul 07, 2010 9:32 am |
|
|
Thanks for catching those mistakes. That's what I get for hammering something out quick. |
|
|
TYSCSTOM
Joined: 03 Jun 2010 Posts: 8 Location: Houston
|
thanks |
Posted: Wed Jul 07, 2010 11:30 am |
|
|
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
|
Re: thanks |
Posted: Wed Jul 07, 2010 11:59 am |
|
|
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
|
|
Posted: Thu Jul 08, 2010 1:42 am |
|
|
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
|
success |
Posted: Thu Jul 08, 2010 2:04 pm |
|
|
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! |
|
|
|