# Thread: String To Integer Array!!!!

1. ## 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;

2. 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;
}```

3. ## 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

4. 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.

5. ## Whats that mean?

salem

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 ?

6. #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 ?

7. the program works only for positive integers though
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 ?

8. what about 'atoi'

it changes a string into an integer value. If it does not convert to a integer, it returns zero.

9. 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 .

Popular pages Recent additions