Thread: converting a string to an array

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    5

    converting a string to an array

    Hey

    I need to write a program that stores a number as an array. Im pretty new to programming, been looking around for a couple hours now and have had no luck. Sorry if this has been asked before.

    this is an example of what I want...

    user inputs 14364 as a string (scanf("%s",number))

    The number then gets stored in an array like this

    int number[5];

    number[0]=1
    number[1]=4
    number[2]=3
    number[3]=6
    number[4]=4

    Any help would be greatly appreciated!

    Dave

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    First, if you do a scanf with a "%s", you need to put it in a char array, say char input[16]. Since a character is stored as number, you can then happily assign each element in the char array to an element in the integer array. Psuedo code:

    Code:
    read input
    for i = 0..index of last character inputted
        number[i] = input[i]

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    5
    Thanks for the help. It mostly makes sense.

    So right now it will look something like this...

    Code:
    #include<stdio.h>
    #include<string.h>
    
    int main(void)
    {
    
    char input[5];
    printf("enter a 5 digit number\n")\
    scanf("%s",input)
    
    
    char number[5]; 
    
    /*use for loop to convert each value of the string to a value in the array*/
    
    for(i = 0; ***;***)
    {    
    number[0] = input[0];
    }
    
    return 0;
    }
    the ***'s are where i am not sure what to do

    Dave

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Do you have any ideas about how to use a for loop or any programming experience? Just giving you the code wouldn't do much good if you can't even figure out the 3rd pard of the for loop (or the guts of it, which I basically gave you). Perhaps read up here first: Cprogramming.com Tutorial: Loops.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by anduril462 View Post
    First, if you do a scanf with a "%s", you need to put it in a char array, say char input[16]. Since a character is stored as number, you can then happily assign each element in the char array to an element in the integer array. Psuedo code:

    Code:
    read input
    for i = 0..index of last character inputted
        number[i] = input[i]
    It's not that simple.... The string will conatin ASCII values. The ASCII character "1" is actually valued at 49.

    If you simply put the 14364 characters in the example into your integer arry you're going to end up with... 49, 52, 51, 54, 52 and I'm guessing that's not quite what you are looking for...

    There is a conversion that has to take place between ASCII and binary representations... Basically you have to subtract 48 from each character and test each time that the result is between 0 and 9 to filter out possible letters or punctuation marks.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Doh! Thanks Tater, don't know how I missed that one.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by curlyfries View Post
    the ***'s are where i am not sure what to do
    Dave
    Ok Dave, lesson #1 in programming ... learn how to look stuff up.

    C, it's libraries and it's extensions contain thousands of function calls. Nobody can ever remember them all. If you don't know how to look these things up and follow the instructions you aren't going very far in the programming world.

    LOL... and just wait till you see the libraries for Windows, Linux and BSD...
    Last edited by CommonTater; 11-11-2010 at 12:54 PM.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by anduril462 View Post
    Doh! Thanks Tater, don't know how I missed that one.
    Well, it's kind of non-obvious unless you've been there and tripped over it once or twice....

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by CommonTater
    Basically you have to subtract 48 from each character and test each time that the result is between 0 and 9 to filter out possible letters or punctuation marks.
    I would rather check that the character is in the range ['0', '9'] and then subtract '0'.
    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

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by CommonTater View Post
    Well, it's kind of non-obvious unless you've been there and tripped over it once or twice....
    I have, several times. I've been at this for 10 years or so...that's the sad part.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by laserlight View Post
    I would rather check that the character is in the range ['0', '9'] and then subtract '0'.
    Yep... that'll work too.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by anduril462 View Post
    I have, several times. I've been at this for 10 years or so...that's the sad part.
    Don't feel bad... I've been at this a while too and I still trip over stuff all the time.

  13. #13
    Registered User
    Join Date
    Nov 2010
    Posts
    5
    Quote Originally Posted by anduril462 View Post
    Do you have any ideas about how to use a for loop or any programming experience? Just giving you the code wouldn't do much good if you can't even figure out the 3rd pard of the for loop (or the guts of it, which I basically gave you). Perhaps read up here first: Cprogramming.com Tutorial: Loops.
    yep, I have done a lot of while loops and do while loops. Never really had a reason to use a for though. I know how a for loop works... (starting number;condition;what happens to number when loop executes) i am just not sure what you mean by index of last character input.

    Quote Originally Posted by CommonTater View Post
    It's not that simple.... The string will conatin ASCII values. The ASCII character "1" is actually valued at 49.

    If you simply put the 14364 characters in the example into your integer arry you're going to end up with... 49, 52, 51, 54, 52 and I'm guessing that's not quite what you are looking for...

    There is a conversion that has to take place between ASCII and binary representations... Basically you have to subtract 48 from each character and test each time that the result is between 0 and 9 to filter out possible letters or punctuation marks.
    Before I posted here I read a bunch of tutorials (and looked up string.h library) and I had it working perfectly fine. Only problem is I was getting numbers like 49,52 etc... and it didnt make any sense to me! This actually helps alot! Thanks

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by curlyfries View Post
    Before I posted here I read a bunch of tutorials (and looked up string.h library) and I had it working perfectly fine. Only problem is I was getting numbers like 49,52 etc... and it didnt make any sense to me! This actually helps alot! Thanks
    No worries.... just wait till you encounter Unicode...

    BTW... Laserlight's suggestion about testing the indivual characters then subtracting '0' is probably better than my way.
    Last edited by CommonTater; 11-11-2010 at 01:33 PM.

  15. #15
    Registered User
    Join Date
    Nov 2010
    Posts
    5
    Sweet, I just got it to work. Look good?

    Code:
    #include<stdio.h>
    #include<string.h>
    
    char main(void)
    {
    char number[5];
    printf("Enter a 5 digit number\n");
    printf("XXXXX\n");
    scanf("%s",number);
    
    int num[5];
    num[0]=(number[0]-48);
    num[1]=(number[1]-48);
    num[2]=(number[2]-48);
    num[3]=(number[3]-48);
    num[4]=(number[4]-48);
    
    return 0;
    }
    As for checking to see if the numbers are between 0 and 9, I was thinking of making a simple function that will contain a while loop that checks to see if num[0]-num[4] are between 0 and 9. Will that work?

    Dave
    Last edited by curlyfries; 11-11-2010 at 02:16 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  4. Converting to expression to string of array elements
    By Sailors in forum C Programming
    Replies: 12
    Last Post: 07-26-2007, 03:01 PM
  5. help converting a char array to a string object?
    By axlton in forum C++ Programming
    Replies: 4
    Last Post: 07-25-2003, 01:34 PM