Newbie question

E

Erik

I want to convert a string of 5 numbers into 5 integers, like:

string=12345
int1=1
int2=2
int3=3
int4=4
int5=5

What is the best way to do this?
 
G

Guenther Sohler

I want to convert a string of 5 numbers into 5 integers, like:

string=12345
int1=1
int2=2
int3=3
int4=4
int5=5

What is the best way to do this?

char string[]="12345";
int int1,int2,int3,int4,int5;

int1=string[0]-'0';
int2=string[1]-'0';
int3=string[2]-'0';
int4=string[3]-'0';
int5=string[4]-'0';

rds
 
M

Malcolm

Erik said:
I want to convert a string of 5 numbers into 5 integers, like:

string=12345
int1=1
int2=2
int3=3
int4=4
int5=5

What is the best way to do this?

char *string = "12345"
int1 = string[0] - '0';
int2 = string[1] - '0';

usually however you want intgers in arrays
int myx[5];

then you can manipulate them in loops.
 
M

Martin Ambuhl

Erik said:
I want to convert a string of 5 numbers into 5 integers, like:

string=12345
int1=1
int2=2
int3=3
int4=4
int5=5

What is the best way to do this?

#include <stdio.h>

int main(void)
{
char String[] = "12345";
size_t n = sizeof String - 1;
unsigned i;
printf("The original string was \"%s\"\n", String);
printf("The individual digits are\n");
for (i = 0; i < n; i++)
printf
("As a char (String[%u]): '%c', "
"as an int (String[%u]-'0'): %d\n",
i, String, i, String - '0');
return 0;
}


The original string was "12345"
The individual digits are
As a char (String[0]): '1', as an int (String[0]-'0'): 1
As a char (String[1]): '2', as an int (String[1]-'0'): 2
As a char (String[2]): '3', as an int (String[2]-'0'): 3
As a char (String[3]): '4', as an int (String[3]-'0'): 4
As a char (String[4]): '5', as an int (String[4]-'0'): 5
 
P

Paul Mesken

I want to convert a string of 5 numbers into 5 integers, like:

string=12345
int1=1
int2=2
int3=3
int4=4
int5=5

What is the best way to do this?

Something like this could do the job :

int i, Integer[5];
char MyString[6] = "12345";

for (i = 0; i < 5; ++ i)
Integer = MyString - '0';

A bit obscure looking perhaps, you could also use the function "atoi".
 
B

bjrnove

Hi.

This pretty much depends on what you are going to use it for. If the
lenght of the string is unknown and you want to have a function that
returns the values you could do it like this:

#include <ctype.h>
#include <stdio.h>

/* szStr = the string
* pnArray = An array of integers that has the size of at least nSize.
* nSize = The size of the string + array. This could be calculated
from the string, but
* I like to do it this way..
* Return value = The number of numbers in the array.
*/
int split_numbers(const char* szStr, int* pnArray, int nSize)
{
int i;

for(i = 0; i < nSize; i++){
if(isdigit(szStr)){
pnArray = szStr - '0';
}
else {
/* You might want to break here. If not just remove the
entire else */
return i;
}
}

return i;
}

int main(void)
{
char szWhatever[] = "123456";
int nArray[sizeof(szWhatever) - 1] = {0};
int i,j;

/* Split the numbers */
j = split_numbers(szWhatever, nArray, sizeof(szWhatever) - 1);

/* Print the splitted numbers */
for(i = 0; i < j; i++){
printf("nArray[%i] = %i\n", i, nArray);
}

}
 
A

Ari Lukumies

Erik said:
I want to convert a string of 5 numbers into 5 integers, like:

string=12345
int1=1
int2=2
int3=3
int4=4
int5=5

What is the best way to do this?

Use an array:

#include <string.h>
int main(void)
{
char string[] = "12345";
int ints[5];
for (int i = strlen(string) - 1; i >= 0; i--)
ints = string-'0';
return 0;
}

The reason why the I didn't write the loop as
for (int i = 0; i < strlen(string); i++)
is left as an excercise for the OP.

-atl-
 
J

Jonas Geiregat

int1=string[0]-'0';
int2=string[1]-'0';
int3=string[2]-'0';
int4=string[3]-'0';
int5=string[4]-'0';


Why - '0' ?
string[0] points to some memory containing a char, so you decrease the
char by '0' , guessing it has something to do with the ascii table else
it would just print the ascii value for the char ..
 
A

Arafangion

Jonas said:
int1=string[0]-'0';
int2=string[1]-'0';
int3=string[2]-'0';
int4=string[3]-'0';
int5=string[4]-'0';



Why - '0' ?
string[0] points to some memory containing a char, so you decrease the
char by '0' , guessing it has something to do with the ascii table else
it would just print the ascii value for the char ..

THis assumes an ascii charmap, this could be undefined elsewhere,
wouldn't it be better to use something like:

int1 = char_to_int(string[0]);
....
int 5 = char_to_int(string[0]);

And define char_to_int(string[0]) as: (Sorry for my possibly incorrect C
code, I don't normally do C code)

int inline char_to_int(char c) {
/* Returns the int representation of the char.
* Returns -1 on fail, negative numbers require
* two int's, and so -1 will never occur
*/
switch(c):
case '0':
return 0;
case '1':
return 1;
...
case '9':
return 9;
default:
return -1;
}

Even so, a library function "str_to_int" would likely be much better.
 
D

Dik T. Winter

> >>int1=string[0]-'0';
....
> > Why - '0' ?
> > string[0] points to some memory containing a char, so you decrease the
> > char by '0' , guessing it has something to do with the ascii table else
> > it would just print the ascii value for the char ..
>
> THis assumes an ascii charmap, this could be undefined elsewhere,

No. This assumes that the requirements of the standard are met. According
to the standard the 10 digits must be consecutive characters.
 
P

pete

Arafangion said:
Jonas said:
int1=string[0]-'0';
int2=string[1]-'0';
int3=string[2]-'0';
int4=string[3]-'0';
int5=string[4]-'0';



Why - '0' ?
string[0] points to some memory containing a char,
so you decrease the
char by '0' ,
guessing it has something to do with the ascii table else
it would just print the ascii value for the char ..

THis assumes an ascii charmap, this could be undefined elsewhere,

No.
int values '0' through '9' are sequential
in all C implementations everywhere.
The alphabet is different.
 
M

Martin Ambuhl

Arafangion said:
Jonas said:
int1=string[0]-'0';
int2=string[1]-'0';
int3=string[2]-'0';
int4=string[3]-'0';
int5=string[4]-'0';




Why - '0' ?
string[0] points to some memory containing a char, so you decrease the
char by '0' , guessing it has something to do with the ascii table else
it would just print the ascii value for the char ..


THis assumes an ascii charmap,

No, it does not. It depends only on these words in the standard:
In both the source and execution basic character sets, the value of
each character after 0 in the above list of decimal digits shall be
one greater than the value of the previous.
this could be undefined elsewhere,

No, it couldn't.
wouldn't it be better to use something like:

No, it wouldn't.
 
C

c_learner

/* coded by c_learner, released under GPL v2 */
#include <stdio.h>
#include <stdlib.h>

int main()
{
char i=0;
char a[6] ="12345";
char c[2]= {0};
char *p = a;
int b[5] = {0};

while(i < 5)
{
c[0] = a;
c[1] = '\0';
b = atoi(c);
printf("%d ",b);
i++;
};
putchar('\n');

return 0;
}
 
C

c_learner

Erik said:
I want to convert a string of 5 numbers into 5 integers, like:

string=12345
int1=1
int2=2
int3=3
int4=4
int5=5

What is the best way to do this?

/*coded by c_learner, released under GPL v2 */
#include <stdio.h>
#include <stdlib.h>

int main()
{
char i=0;
char a[6] ="12345";
char c[2]= {0};
int b[5] = {0};

while(i < 5)
{
c[0] = a;
b = atoi(c);
printf("%d ",b);
i++;
};
putchar('\n');

return 0;
}
 
M

Michael Mair

c_learner said:
/* coded by c_learner, released under GPL v2 */
<OT>If you want to release something under a certain
license, provide either the license text or at least
the URI of a copy of the license said:
#include <stdio.h>
#include <stdlib.h>

int main()
int main (void)
is more expressive
{
char i=0;

Note: Most people expect int or size_t integer loop variables.
If you have a good reason to use char, do so, of course.
char a[6] ="12345";
char c[2]= {0};
char *p = a;
int b[5] = {0};

AFAICS, p is used nowhere.
The 6 is not necessary. In fact, it may be a better
idea to use a symbolic constant, e.g. NUMDIGITS to express
the size of a (char[NUMDIGITS+1]) and b (int[NUMDIGITS])
as well as the range of the loop.
while(i < 5)

The loop variable i is initialised at the very start of main().
If someone (maybe even you) has later on the bright idea to use
i for another loop before this one, you may be in for a surprise.
So, either set i=0; before while (i < 5) or go for a for loop
which IMO here would express in a clearer way what you are doing
with the loop.
{
c[0] = a;
c[1] = '\0';
b = atoi(c);


You are not performing any error checking here. Either have
a look at c[0] with isdigit((unsigned char)c[0]) or use
strtol() instead of atoi() to know if something went amiss.
printf("%d ",b);
i++;
};
putchar('\n');

return 0;
}


Note: You do not need c if you
- go for the canonical a - '0'; or
- run the other way through a and set a[i+1] to zero before
passing &a to atoi()/strtol() (this effectively destroys
the information in a);

Another approach could make use of a reference string literal
char digits[] = "0123456789"; and p = strchr(digits, a);
where, for non-NULL p, b becomes p - digits.


Cheers
Michael
 
J

Jonas Geiregat

Martin said:
Arafangion said:
Jonas said:
int1=string[0]-'0';
int2=string[1]-'0';
int3=string[2]-'0';
int4=string[3]-'0';
int5=string[4]-'0';





Why - '0' ?
string[0] points to some memory containing a char, so you decrease the
char by '0' , guessing it has something to do with the ascii table else
it would just print the ascii value for the char ..



THis assumes an ascii charmap,


No, it does not. It depends only on these words in the standard:
In both the source and execution basic character sets, the value of
each character after 0 in the above list of decimal digits shall be
one greater than the value of the previous.
this could be undefined elsewhere,


No, it couldn't.
wouldn't it be better to use something like:


No, it wouldn't.

I never quite got the answer ...
 
A

Al Bowers

Jonas Geiregat wrote:

char string[] = "12345";
int1=string[0]-'0';
int2=string[1]-'0';
int3=string[2]-'0';
int4=string[3]-'0';
int5=string[4]-'0';





Why - '0' ?
I never quite got the answer ...

You got an answer to the question. Just review the posts of
Martin Anbuhl on this topic. Martin has shown that the Standard
describes the behavior of the '-' operator in the statements
above. And, the behavior satisfies the OP's requirements.
 
P

Paul Mesken

Why - '0' ?
string[0] points to some memory containing a char, so you decrease the
char by '0' , guessing it has something to do with the ascii table else
it would just print the ascii value for the char ..
I never quite got the answer ...

As was pointed out : C requires that the decimal digits '0' - '9' have
consecutive values (the value of '2' is the value of '1' - 1).

This doesn't have anything to do whether the character set is ASCII or
not, it's just a requirement of C itself and thus totally portable.

Let's say the value of '0' is X. The value of '1' is then X + 1, the
value of '2' X + 2, etc. This is what C requires.

So, if we subtract 'X' from the value of a decimal digit then we'll
get 0 for '0', 1 for '1', 2 for '2', etc.
 
C

c_learner

/* Thanks */
/* coded by c_learner, released under GPL v2
(http://www.gnu.org/copyleft/gpl.html) */
#include <stdlib.h>
#define NUM 5

int main(void)
{
int i = 5;
char a[NUM+1] = "12345";
int b[5] = {0};

while(--i > -1)
{
b = atoi(&a);
a='\0';
}

return 0;
}


Michael said:
c_learner said:
/* coded by c_learner, released under GPL v2 */
<OT>If you want to release something under a certain
license, provide either the license text or at least
the URI of a copy of the license said:
#include <stdio.h>
#include <stdlib.h>

int main()
int main (void)
is more expressive
{
char i=0;

Note: Most people expect int or size_t integer loop variables.
If you have a good reason to use char, do so, of course.
char a[6] ="12345";
char c[2]= {0};
char *p = a;
int b[5] = {0};

AFAICS, p is used nowhere.
The 6 is not necessary. In fact, it may be a better
idea to use a symbolic constant, e.g. NUMDIGITS to express
the size of a (char[NUMDIGITS+1]) and b (int[NUMDIGITS])
as well as the range of the loop.
while(i < 5)

The loop variable i is initialised at the very start of main().
If someone (maybe even you) has later on the bright idea to use
i for another loop before this one, you may be in for a surprise.
So, either set i=0; before while (i < 5) or go for a for loop
which IMO here would express in a clearer way what you are doing
with the loop.
{
c[0] = a;
c[1] = '\0';
b = atoi(c);


You are not performing any error checking here. Either have
a look at c[0] with isdigit((unsigned char)c[0]) or use
strtol() instead of atoi() to know if something went amiss.
printf("%d ",b);
i++;
};
putchar('\n');

return 0;
}


Note: You do not need c if you
- go for the canonical a - '0'; or
- run the other way through a and set a[i+1] to zero before
passing &a to atoi()/strtol() (this effectively destroys
the information in a);

Another approach could make use of a reference string literal
char digits[] = "0123456789"; and p = strchr(digits, a);
where, for non-NULL p, b becomes p - digits.


Cheers
Michael
 
G

goose

c_learner said:
Michael said:
c_learner said:
/* coded by c_learner, released under GPL v2 */

<OT>If you want to release something under a certain
license, provide either the license text or at least
the URI of a copy of the license said:
#include <stdio.h>
#include <stdlib.h>

int main()

int main (void)
is more expressive
{
char i=0;

Note: Most people expect int or size_t integer loop variables.
If you have a good reason to use char, do so, of course.

char a[6] ="12345";
char c[2]= {0};
char *p = a;
int b[5] = {0};

AFAICS, p is used nowhere.
The 6 is not necessary. In fact, it may be a better
idea to use a symbolic constant, e.g. NUMDIGITS to express
the size of a (char[NUMDIGITS+1]) and b (int[NUMDIGITS])
as well as the range of the loop.

while(i < 5)

The loop variable i is initialised at the very start of main().
If someone (maybe even you) has later on the bright idea to use
i for another loop before this one, you may be in for a surprise.
So, either set i=0; before while (i < 5) or go for a for loop
which IMO here would express in a clearer way what you are doing
with the loop.

{
c[0] = a;
c[1] = '\0';
b = atoi(c);


You are not performing any error checking here. Either have
a look at c[0] with isdigit((unsigned char)c[0]) or use
strtol() instead of atoi() to know if something went amiss.

printf("%d ",b);
i++;
};
putchar('\n');

return 0;
}


Note: You do not need c if you
- go for the canonical a - '0'; or
- run the other way through a and set a[i+1] to zero before
passing &a to atoi()/strtol() (this effectively destroys
the information in a);

Another approach could make use of a reference string literal
char digits[] = "0123456789"; and p = strchr(digits, a);
where, for non-NULL p, b becomes p - digits.


Cheers
Michael



Firstly, top-posting on clc earns you a spanking!
*spank*
*spank*
*spank*

There

Now see that it doesn't happen again :)
> /* Thanks */
> /* coded by c_learner, released under GPL v2
> (http://www.gnu.org/copyleft/gpl.html) */
> #include <stdlib.h>
> #define NUM 5
>
> int main(void)
> {
> int i = 5;

Since you have #define NUM, why not use NUM here? When the
array size changes to 7, you will then only have to change
the #define, and not hunt down all the places where you used
the magic number 5.
> char a[NUM+1] = "12345";
> int b[5] = {0};
>
> while(--i > -1)
> {
> b = atoi(&a);


Once again, you've used the dreaded atoi(). Why not
change this as suggested above by MM?
> a='\0';
> }
>
> return 0;
> }
>
>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top