# String To Integer Array!!!!

• 10-28-2001
Unregistered
String To Integer Array!!!!
Ok I finally got my program to read in a line as a character array for a string but now I want to convert that character array into an integer array.

Its like this:

if I input 1 2 3 4 5

I want to put that into an integer array with a[0]=1, a[1]=2, etc.

Or if I have this: 12 45 6 23 100

I want to convert that into the integer array like a[0]=12, a[1]=45, a[2]=6, a[3]=23, etc.
If I can do this then my problem is solved so please help me!

I guess I need to find the spaces and concatenate anything before that into an array location such as a[4] or something.

thanks,
dragoon

Here is my code so far!

#include <stdio.h>
int n;
char a[200];

int caten()
{
// int *numchar;
// *numchar = a;

printf("%s", a);
// printf("%d", *numchar);
return 0;
}

int main()
{
n = 0;
printf("Enter some integers (maximum of 20):\n");
while((a[n++]=getchar( )) != '\n' );
a[n] = '\0';
if (a[n] == '\0')
caten();
// printf("%s", a);
return 0;
• 10-28-2001
Salem
Code:

```#include <stdio.h> void foo ( char *line ) {     int num, i = 0, len;     while ( sscanf( line, "%d%n", &num, &len) == 1 ) {         printf( "Number %d is %d\n", i, num );         line += len;    // step past the number we found         i++;            // increment our count of the number of values found     } } int main ( ) {     char test[] = "12 45 6 23 100";     foo( test );     return 0; }```
• 10-28-2001
Unregistered
im sorry
I need to put it into a int array[] and I need to use an int *n for my number to count. So please help me put the string array into the integer array using the *n.

thnx
dragoon
• 10-28-2001
Salem
Come off it, you mean adding
&nbsp; array[i] = num;
to the middle of the code is too hard?

At least have a go at it - do some reading, try some stuff out.
• 10-28-2001
C_Coder
Whats that mean?
salem

Quote:

while ( sscanf( line, "%d%n", &num, &len) == 1 )
What does the format specifier %n do? Haven't come across that.

and if sscanf is doing 2 conversions shouldnt it return 2 ?
• 10-28-2001
pinko_liberal
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

void writenum(const char *s)
{
int k=0;
int c;
do
{
while( (c=*s++) && (c==' ') );
if(isdigit(c))
{
int i=0;
while(isdigit(c))
{
i=10*i+(c-'0');
c=*s++;
}
printf("index:%d value:%d\n",++k,i);
}
else if(c)
{
printf("invalid character with ascii value %d found\n",c);
return;
}
}while(c);
}

int main(void)
{
writenum(" 1 2 3 344 1 44");
return 0;
}

when I copy paste from my editor why does all the formatting get lost ?
• 10-28-2001
pinko_liberal
the program works only for positive integers though
Quote:

Originally posted by pinko_liberal
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

void writenum(const char *s)
{
int k=0;
int c;
do
{
while( (c=*s++) && (c==' ') );
if(isdigit(c))
{
int i=0;
while(isdigit(c))
{
i=10*i+(c-'0');
c=*s++;
}
printf("index:%d value:%d\n",++k,i);
}
else if(c)
{
printf("invalid character with ascii value %d found\n",c);
return;
}
}while(c);
}

int main(void)
{
writenum(" 1 2 3 344 1 44");
return 0;
}

when I copy paste from my editor why does all the formatting get lost ?

• 10-29-2001
Unregistered

it changes a string into an integer value. If it does not convert to a integer, it returns zero.
• 10-29-2001
pinko_liberal
Quote:

Originally posted by Salem
> when I copy paste from my editor why does all the formatting get lost ?
Because you're not using the code tags - those magic words between square brackets.

http://www.cprogramming.com/cboard/m...bbcode#buttons

&#91;code]
// paste code here
&#91;/code]

thanks a lot .