# Thread: Using an integer like a string or converting an integer to a string?

1. ## Using an integer like a string or converting an integer to a string?

I am not exactly sure how to ask this question, but I'll do my best to explain:

(This is in relation to Project Euler #4)

I have a function that checks to see if a word is a palindrome, and it works fine for words. The only thing stopping me from solving #4 is either a way to change my function to work with integers, or to convert my integer from a string.

If I have a string with the word 'and' stored in it, it is essentially [a][n][d]['\0']

I can now work with these letters by string[0] etc...

So if I have a number like '257', is there a way where I can do something like number[0] = 3, thus changing the number to 357?

I need to be able to basically multiply one number by another, then be able to check each digit of the product individually.

Note: I'm aware there are other libraries or prewritten functions that can convert an integer to a string, but I would like to be able to do this operation on my own so I can understand the concept better. I've already figured out how to do basic functions which are readily available such as strcopy, strcompare, etc., but I wrote them all myself.

2. Originally Posted by jensbodal
Note: I'm aware there are other libraries or prewritten functions that can convert an integer to a string, but I would like to be able to do this operation on my own so I can understand the concept better. I've already figured out how to do basic functions which are readily available such as strcopy, strcompare, etc., but I wrote them all myself.
That is a little more low level an approach than most sane people would advocate, but okay. Here's some clues:

1) use modulus (%) by 10 in a loop to pull each digit out.
2) to convert a digit (0-9) to it's character representation:

Code:
```int n = 6;
char six = n+48;```
This is because characters are all actually numerical values from the "ascii table":

ASCII Table / Extended ASCII Codes

Notice, '0' is 48, '1' is 49, etc.

3. Thank you for the reply had to run after I posted that. I had thought to use the ASCII codes but hadn't figured out a way to get that to work. Most of the stuff I've done I've been able to complete without using pre-defined/built libraries, but maybe in this case I should make an exception?

I do see how I can use what you told me to do what I want, but I guess I should ask if in this case it's worth me not using something like atoi

4. Well here is what I came up with, I'm sure it is not the best solution (especially since it converts the integer to the string backwards and I had to make another function to reverse a string) but it works and I solved problem 4 on project euler.

My Integer to String function (and Reverse String function for that matter):

Code:
```void strRev(char* str1)
{
char str2[256] = "";
int i = 0;
int i2 = 0;
i = (strlen(str1)-1);
//str2[i] = str1[i];
while (str1[i2] != '\0')
{
str2[i2] = str1[i];
i2++;
i--;
}
for (i = 0; str2[i] != '\0'; i++)
str1[i] = str2[i];
} //End String Reverse

void cI2S (int number, char* string)
{
int test = 0;
int i = 10;
int a = 0;

while (number > 0)
{
test = number%i;
test = test / (i/10);
string[a] = 48+test;
number = number - test*(i/10);
i = i*10;
a++;
}
strRev(string);
} //End convert Integer to String```
Also now I realize I could have done the same type of numberMOD10 thing to compare integers rather than converting them to a string and comparing them there, but I can just save that knowledge for next time.

5. Code:
```while( num > 0 )
{
string[a++] = '0' + num % 10;
num /= 10;
}```