Thread: Inputing Multiple Strings in "One" Array

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    27

    Inputing Multiple Strings in "One" Array

    Hello,
    I was wondering if I can input multiple strings in "One" array.
    Can it be done? Or not?
    If yes, then how?

    Please tell me.
    Regards

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    char stringarray[numberofstrings][sizeoflongeststring];

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    23
    Quote Originally Posted by ahmedbatty View Post
    Hello,
    I was wondering if I can input multiple strings in "One" array.
    Can it be done? Or not?
    If yes, then how?

    Please tell me.
    Regards
    If you really want to hold them into a single array you could create a larger array and concatenate each input using strcat. You could use a separator between them and use strtok when you want to divide them again.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by dheaven View Post
    If you really want to hold them into a single array you could create a larger array and concatenate each input using strcat. You could use a separator between them and use strtok when you want to divide them again.
    Why do all that when you can just make a 2 dimensional array...

    Code:
    MAX_STRINGS 100
    MAX_LENGTH 30
    
    // space for 100 strings 30 characters long
    
    char Words[MAX_STRINGS][MAX_LENGTH];
    It amounts to the same thing and you don't have to use strcat() or strtok() to get at them.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    27
    Quote Originally Posted by CommonTater View Post
    char stringarray[numberofstrings][sizeoflongeststring];
    but, how do input using scanf?

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ahmedbatty View Post
    but, how do input using scanf?
    Code:
    scanf("%s",stringarray[index]);
    Really... you need to get the documentation for your compiler's libraries and start looking this stuff up for yourself.

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    27
    Quote Originally Posted by CommonTater View Post
    Code:
    scanf("%s",stringarray[index]);
    Thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can I have an "Array of Strings"?
    By abh!shek in forum C++ Programming
    Replies: 15
    Last Post: 01-22-2008, 01:35 PM
  2. "Apply", "OK" and "CANCEL" strings
    By Joelito in forum Windows Programming
    Replies: 2
    Last Post: 05-12-2006, 04:07 AM
  3. "Array" of strings
    By Yorae in forum C Programming
    Replies: 5
    Last Post: 02-25-2006, 01:45 PM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM