Thread: Converting a string into an array?

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    36

    Converting a string into an array?

    ok so tossed out last little project cuz professor said it would be alittle much an he wanted me to be able to do it at end of semester instead of a program to do it for me lol so my new fun little project is yet to be determined, however i know i will need to take in a string and put each digit of it into an array not really sure how to take this one but what ive guessed to far was to try and declare each int. as it was scanned in but that doesn't seem to be working, how can i go about this??
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    float a,b[4],c;
    
    int main(void)
    {
    printf("Enter string up to 5 digits\n");
    scanf("%s", &b[4], &b[3],&b[2],&b[1],&b[0]);
    
    
    
    printf("\n1st: %d", b[4]);
    printf("\n2nd: %d", b[3]);
    printf("\n3rd: %d", b[2]);
    printf("\n4th: %d", b[1]);
    printf("\n5th: %d", b[0]);
    
    
    
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    A string is an array of chars with a zero at the end. If you want to copy this to somewhere else and maintain it as a string use strcpy or similar function or loop through it manually and assign one char at a time. The important thing to look out for it that you don't miss the zero at the end.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by litzkrieg View Post
    ok so tossed out last little project cuz professor said it would be alittle much an he wanted me to be able to do it at end of semester instead of a program to do it for me lol so my new fun little project is yet to be determined, however i know i will need to take in a string and put each digit of it into an array not really sure how to take this one but what ive guessed to far was to try and declare each int. as it was scanned in but that doesn't seem to be working, how can i go about this??
    Ummmm.... a string IS an array.

    Code:
    char string[6] = "12345\0";
    Mission accomplished.


    And you started 3 threads for this .... because????

  4. #4
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Quote Originally Posted by CommonTater View Post
    And you started 3 threads for this .... because????
    There seems to have been a bit of a technical issue with the boards that's caused this. I see another user has accidentally created two identical threads as well.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    I think you need to read your text book and some tutorials first. Go through all the lessons in order, and make sure you can do all the practice problems. When you master the array section, you are ready to start working on this program, at which point you should design a solution on paper before you even start coding. Right now, you are trying to tackle arrays and strings by just guessing at how to use them. Spend the time to learn it first. There are some tutorials here, and you can Google for lots more info. Do the same thing for scanf.

    Some tips to start:
    • An array with N elements has valid indexes 0..N-1
    • Using the name of the array gives you the address of the first element. Also, read up on scanf.
    • Each format specifier for scanf should have exactly one parameter to store the conversion in.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. converting a string to array
    By jtieu in forum C++ Programming
    Replies: 8
    Last Post: 03-28-2011, 11:27 PM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 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. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM