How can I treat a number as an array?

Let's say I have

int i = 153

And I want to get the 5 part. I want to be able to do something like i[1], is there a way to do this in C++? Also, would there be a way to do this with a string?
You can do that with a string, because string is an array of characters.
You can't do it with an integer that simply, because it is not an array of digits. To get a digit, you'll have to play around with /10 and %10.
If you change it to a string, you can do that. But then, again you have to change the "5" back into int.
nope you cant do that...
but still

1
2
char a[10]={"153"};
int i=a[1]-48; //ascii value of 5 is 48 + 5... ascii value of any single digit number is 48+num 


But still if you want to logically seperate it..

1
2
3
4
5
6
7
8
9
10
11
12
13
int i=5,a,b[5],a1;
cin>>a;
a1=a;
while(a1!=0)
{
 b[i]=a1%10;
 a1=a1/10;
 i--;
}
while(i!=-1)
{
 a[i]=-1;
}


And then you can print the number if it is not -1 .
Topic archived. No new replies allowed.