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 seeem 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]);
    
    
    
    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
    Sep 2010
    Posts
    69
    Quote Originally Posted by litzkrieg View Post
    Code:
    float a,b[4],c;
    
    printf("Enter string up to 5 digits\n");
    scanf("%s", &b[4]);
    
    
    
    printf("\n1st: %d", b[4]);
     ...
    printf("\n5th: %d", b[0]);
    If you want an array of 5 elements, then you have to declare an array with 5 elements.
    i.e:
    Code:
      int  myArray[5];
    
          myArray[0]
          myArray[1]
          myArray[2]
          myArray[3]
          myArray[4]

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    See this duplicate thread: Converting a string into an array?.

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