Thread: Converting single characters of a string into integers

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    10

    Converting single characters of a string into integers

    Hello,

    This is my first post in this forum.

    I have a string s in the following format: -1234567890

    Now I need to strip the '-' and convert every single digit into an int and place it into an array of ints:

    t[0] = 1
    t[1] = 2
    t[2] = 3
    t[3] = 4
    ...

    For the conversion I tried this inside a for loop, but this didn't work: t[i] = atoi(s[i]);

    Patrick

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Post your code, we'll help you through it.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    atoi(s[i]) will work only if the return value is captured in an int not an array of ints.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Maybe...
    Code:
    t[i] = s[i+1] - '0';
    Assuming s[0] is the '-' char and you loop i from 0 to two less than whatever the length of the string is (including the '-').
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    10
    Quote Originally Posted by Dino View Post
    Post your code, we'll help you through it.
    Here is my code:

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    typedef struct {
       int signe;
       int size;
       int *tab;
    } bignum;
    
    bignum tobignum( char a[] ) {
       bignum x;
    
       x.signe = (a[0] == '-');
       x.size = strlen(a);
       if (a[0] == '-') x.size--;
    
       x.tab = malloc(x.size*sizeof(int));
    
       /* removing the '-' if present */
       /* ???? */
    
       /* converting the digits into integers and insering into tab */
       int i;
       for (i=0; i<strlen(a); i++) {
          x.tab[i] = atoi(a[i]);
       }
    
       return (bignum);
    }
    
    int main (int argc, const char * argv[]) {
       char a[50] = "-1234567890";
       bignum n;
       n = tobignum(a);
    
       return 0;
    }
    Patrick

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    hk_mp5kpdw has shown exactly what you need and you don't need so much code for it.

  7. #7
    Registered User
    Join Date
    Nov 2008
    Posts
    10
    Quote Originally Posted by hk_mp5kpdw View Post
    Code:
    t[i] = s[i+1] - '0';
    Assuming s[0] is the '-' char and you loop i from 0 to two less than whatever the length of the string is (including the '-').
    But what does the -'0' mean? Is this used to make the conversion?
    And just to get things right, if the string is entered with a scanf command, you also have to remove the \n character at the end, right?

    Patrick

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    But what does the -'0' mean? Is this used to make the conversion?
    If you have the char '0' and wish to get the integer 0, you can subtract '0' from it. Since the digits are guaranteed to be consecutive, '1' - '0' gives 1, and so forth.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by petz_e View Post
    But what does the -'0' mean? Is this used to make the conversion?
    And just to get things right, if the string is entered with a scanf command, you also have to remove the \n character at the end, right?

    Patrick
    If c is of type char and a digit then c - '0' gives you c in its int form otherwise its stored in its decimal ASCII value.
    if c = 1 its decimal ASCII value is 49 but c - '0' gives 1.
    and scanf skips '\n' as its whitespace.
    Last edited by itCbitC; 11-12-2008 at 12:34 PM.

  10. #10
    Registered User
    Join Date
    Nov 2008
    Posts
    10
    OK, the input string doesn't have to be composed of consecutive digits, but this should not matter.

    Thanks for the replies and for the help.

    Patrick

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    When I say "the digits are guaranteed to be consecutive", I mean that this guarantee is made of the character set, not your string.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Hacker MeTh0Dz's Avatar
    Join Date
    Oct 2008
    Posts
    111
    Here is a solution...

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    void convert(char * data, int ** output)
    {
    	int i = 0;
    	for (i = 0; data[i]; i++) {
    		if (data[i] == '-') data++;
    		(*output)[i] = (int)(data[i] & ~0x30);
    	}
    	
    	return;
    }
    
    int main (int argc, const char * argv[]) {
    	char a[50] = "-1234567890";
    	
    	int * output_data = malloc(sizeof(int) * 40);
    	if (!output_data) return 0;
    	
    	convert(a, &output_data);
    	printf("&#37;d\n", output_data[1]);
    	
    	free (output_data);
    	return 0;
    }

  13. #13
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by MeTh0Dz View Post
    Code:
    	int * output_data = malloc(sizeof(int) * 40);
    that allocates 4x more storage than needed

  14. #14
    Hacker MeTh0Dz's Avatar
    Join Date
    Oct 2008
    Posts
    111
    Okay cool. Change it. Although it's not like it really matters. And it also allows for expansion.

    That wasn't even worth commenting on.

  15. #15
    Registered User
    Join Date
    Nov 2008
    Posts
    10
    No, it's ok. I got it working now Thanks again

    Patrick

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  2. Converting String to integers
    By Mister C in forum C Programming
    Replies: 21
    Last Post: 06-01-2005, 03:13 PM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM