Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting string to numeric

Status
Not open for further replies.

DerPflug

Programmer
Mar 28, 2002
153
US
I have a part id (example: JJ1234) where the first two characters are always alphabetic and the last 4 are always numeric. However, it is defined as a string in a struct.
I need to validate that the last four characters are numeric. How do I convert the last 4 characters to numeric to check whether or not they're numeric. Or am I going about this the wrong way?

Thanks in advance.
 
DATA CONVERSION
The following functions convert between data types.

atof() converts an ascii character array to a float
atoi() converts an ascii character array to an integer
itoa() converts an integer to a character array

--------------------------------------------------------------------------------

Example

/* convert a string to an integer */
#include <stdio.h>
#include <stdlib.h>

char string[] = &quot;1234&quot;;

main()
{
int sum;
sum = atoi( string );
printf(&quot;Sum = %d\n&quot;, sum );
}



--------------------------------------------------------------------------------

/* convert an integer to a string */
#include <stdio.h>
#include <stdlib.h>

main()
{
int sum;
char buff[20];

printf(&quot;Enter in an integer &quot;);
scanf(&quot; %d&quot;, &sum );
printf( &quot;As a string it is %s\n&quot;, itoa( sum, buff, 10 ) );
}

Note that itoa() takes three parameters,

the integer to b converted
a character buffer into which the resultant string is stored
a radix value (10=decimal,16=hexadecimal)
In addition, itoa() returns a pointer to the resultant string.

 
Hi,

Be carfeful when you use atoi( ). You can use the atoi( ) function as Mithrilhall described all you want, but:

const char * partId[] = &quot;JJ12314&quot;;
int sum;

sum = atoi(partId);

will not be what you want. Sum will be 0 in this case because of the &quot;jj&quot;. I'm sure he just wanted you to figure that part out yourself.

What you need to do is consider how to &quot;get at&quot; the lst four digits of a string where the first 2 will always be characters. You can this many different ways. If you are absolutely sure that the digits start at the 3rd character, you can use:

sum=atoi(partId+2);

There are other ways as well. Look into the ctype.h library funcitons. They will help you out with this type of processing.

Hope that helps.

-Tyler
 
reading your question again, do you just require to check if the character stored in a member are numeric.

If so then look at the functions isdigit(), isalpha() in lib <stdlib.h>

if(isdigit(str.ch)) printf(&quot;character %c numeric&quot;, str.ch);
else printf(&quot;character %c alphabetical&quot;, str.ch); Hoping to get certified..in C programming.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top