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

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    6

    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.
    Last edited by jensbodal; 12-03-2009 at 01:53 PM.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by jensbodal View Post
    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.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    6
    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. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    6
    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. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    while( num > 0 )
    {
        string[a++] = '0' + num % 10;
        num /= 10;
    }
    Then do your strrev.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  4. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  5. Converting a string to an integer
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 08-31-2001, 10:01 PM