# splitting int values?!

#### essi1553

##### Limp Gawd
Splitting up an int value?

I am trying to find out of a number is a palindrome, my problem is I have no idea what-so-ever to on how to split a number up.

for example if i had the user input 12345,

I am trying to figure out how to read 1 into num 1, 2 into num2, 3 into num3, etc.

Is there a special command to split up a numbeer without spaces between them?

#### CodeX

##### 2[H]4U
Divide by 10, take remainder

12345/10 = 1234 remainder 5 -> ones position
1234/10 = 123 remainder 4 -> tens position
123/10 = 12 remainder 3 -> hundreds position
12/10 = 1 remainder 2 -> thousands position
1/10 = 0 remainder 1 -> ten thousands position

I would use a stack to implement this, push each remainder onto the stack
To find a remainder in C++ use the modulus operator, %

while(numToBeSplit > 0)
{
stack.push(numToBeSplit % 10);
numToBeSplit /= 10;
}

You would then check the size of the stack to see how many digits were seperated, and store each digit in its own int variable.

This can of course be done without a stack, you could also use an array. Or, if you know the size of the number you are splitting (number of digits) will always be the same you could just hardcode it without even looping...

##### 2[H]4U
I'd just convert it to a string ;o)

#### CodeX

##### 2[H]4U
I'd just convert it to a string ;o)

Shhh! Don't give away everything, LOL.

#### essi1553

##### Limp Gawd
Awesome guys thanks, ill be working this out for the rest of the evening.

#### gotkilled

##### Limp Gawd
keep dividing by 10 this will result in numbers being reversed. Then you take the reversed numbers and reverse them back.

#### essi1553

##### Limp Gawd
how do you store the remainder?

#### CodeX

##### 2[H]4U
int remainder = numToBeSplit % 10;

#### mikeblas

##### [H]ard|DCer of the Month - May 2006
I'd just convert it to a string ;o)
Why did you convert it to an integer in the first place?

#### Fark_Maniac

##### 2[H]4U
the percent is called a 'mod'....I'm pretty sure.

#### CodeX

##### 2[H]4U
Modulus, as I stated in the first reply...

#### Fark_Maniac

##### 2[H]4U
CodeX said:
Modulus, as I stated in the first reply...
as homer would say..."doh!!!"

##### 2[H]4U
mikeblas said:
Why did you convert it to an integer in the first place?

I just think its more elegant to convert a string into an int and then back again, dont you?

#### mikeblas

##### [H]ard|DCer of the Month - May 2006
I just think its more elegant to convert a string into an int and then back again, dont you?
Not for this problem.

#### CodeX

##### 2[H]4U
mikeblas said:
Why did you convert it to an integer in the first place?

Why are you assuming it was a string in the first place? The OP asked how to split up an integer value. Sure, he said it was user input, but that could still be in the form of an integer and not a string:

int num;
cin >> num;

Using a string would be the more efficient way to do this, but thats not what the OP asked and for all we know (knowing little about what he is trying to accomplish) it might not make any sense at all to use a string.

#### ameoba

##### Supreme [H]ardness
GAH!

using a stack for this?

Just use * and + to build a second number & compare the two.

#### CodeX

##### 2[H]4U
ameoba said:
GAH!

using a stack for this?

Just use * and + to build a second number & compare the two.

yeah... that really helps alot

If the number he is splitting has an unkown number of digits and he wants each digit stored as its own int you are going to have to hold them in an array or a stack or some other dynamically allocated data structure...

#### mikeblas

##### [H]ard|DCer of the Month - May 2006
CodeX said:
Why are you assuming it was a string in the first place?
How would the user provide their integer to the program without it being a string? It can be done, but it's pretty tough -- on modern computers, anyway -- and I assumed those convoluted paths were too unlikely to be the problem.

There are many ways to represent "an integer value"; one way is as a string. That is, in fact, the most convenient representation for this problem. (Why are you assuming we're testing for palindromic numbers only in base ten? FAAF is a palindromic number in base 16. And in base 36, too.)

CodeX said:
The OP asked how to split up an integer value. Sure, he said it was user input, but that could still be in the form of an integer and not a string:

int num;
cin >> num;

That code converts it from a string (since cin is a character-mode stream) to an integer.

Code:
``````string str;
cin >> str;``````

doesn't do such a conversion. Then, you don't need any temporary storage to do the test, and you don't need to do all this expensive math, and you don't need to do any conversions (which just hide the expensive math). The code easily works on different bases, and can work with numbers of pretty much unlimited size.

#### ameoba

##### Supreme [H]ardness
CodeX said:
yeah... that really helps alot

If the number he is splitting has an unkown number of digits and he wants each digit stored as its own int you are going to have to hold them in an array or a stack or some other dynamically allocated data structure...

Since this is pretty clearly a homework problem that's supposed to show the use of integer division, the 'best' solution is going to be integer based.

Don't read too much into it.

Don't overcomplicate the matter.

#### mikeblas

##### [H]ard|DCer of the Month - May 2006
ameoba said:
Since this is pretty clearly a homework problem that's supposed to show the use of integer division, the 'best' solution is going to be integer based.
Well, an adequate solution could be integer based. If I was a CS professor and had a student hand in a solution that took into account additional, unspecified requirements, did a good job of thinking the problem through, and ended up at an elegant solution anyway, I'd give them a grade that was better than "adequate".

#### ameoba

##### Supreme [H]ardness
...but an overengineered solution beyond the normal/expected skill/knowledge level of the student would be suspicious.

#### CodeX

##### 2[H]4U
lol... yeah you are probably going to get him in trouble

...you are right about the string thing mike, I submit to your superior knowledge...

##### Limp Gawd
string str;
cin >> str;
doesn't do such a conversion

doesn't that convert from a char[] to a string?

#### mikeblas

##### [H]ard|DCer of the Month - May 2006
doesn't that convert from a char[] to a string?
It's not converting anything to an integer type. That requires division, and lots of it.

cin is buffered, so I guess you could say that operation is creating a string from a range of characters; but that doesn't really involve a conversion. The string represents the characters the same way the buffer over the file stream represents them.

ameoba said:
...but an overengineered solution beyond the normal/expected skill/knowledge level of the student would be suspicious.
It's really simple:

Code:
``````int main()
{
std::string str;
std::cin >> str;

size_t nLength = str.length();
bool bPalindrome = true;
for (int n = 0; n < nLength/2; n++)
{
if (str[n] != str[nLength-n-1])
{
bPalindrome = false;
break;
}
}

std::cout << "It is ";
if (!bPalindrome)
std::cout << "not ";
std::cout << "a palindrome" << std::endl;

return 0;
}``````

I don't see that as overengineered. I'd expect someone in an intro class to be able to write that, no problem.

#### ameoba

##### Supreme [H]ardness
Nope - that's not overengineered. Using a stack as another poster suggested, OTOH, might've raised suspicions.

As I said, however, the fact that the problem description involved integer palindromes and not finding string palindromes -very- strongly indicates that the div/mod solution is the desired one (and the point of the whole exercise). Not being in the class myself, I can't say for certain, but it wouldn't suprise me in the least if the lectures or readings preceding the assignment covered integer arithmetic.

#### CodeX

##### 2[H]4U
ameoba said:
Using a stack as another poster suggested, OTOH, might've raised suspicions.
*edit*
Nevermind, its cool, LOL.

#### CodeX

##### 2[H]4U
For the record, using a stack (which is not, in any way, complicated or convoluted) would have allowed him to simply pop the values out in reverse order, giving him the proper digit ordering (since using the method I mentioned in the first reply to the thread results in the original number in reverse digit order)

And yes I know I resurrected a several year old thread... lol

(Actually, this is a test, someone respond to this if you see it, someone PM'd me today saying that they can't see any of my posts for some reason)