strings and arrays

This is a discussion on strings and arrays within the C++ Programming forums, part of the General Programming Boards category; How do you put strings into arrays...

  1. #1
    Unregistered
    Guest

    Question strings and arrays

    How do you put strings into arrays

  2. #2
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    First Create a char array.. then use a for loop to put chars into it.. Thus putting a string


    Example



    # include <iostream.h>


    main()
    {


    char name[20];
    cout<<"Enter you name > ";
    for(int i=0;i<20;i++)
    {

    cin>>name[i];
    }



    }







    So what happens in the above program is when a user enters a name example name as vasanth. V is stored in name[0], a in name[1] and so on.. the program increments the value of I. And then accepts a char from the user and stores it into the array name[i]. Where i increments +1 after every charater is entered..
    Hope you got it...

  3. #3
    Unregistered
    Guest
    if your compiler supports STL the easy way is to vector containing instances string class. both the vector and string class are defined in STL.

    The "old fashioned", but still much used way, is to use a two dimensional array of char.

    char ArrayOfStrings[NumberOfStrings][MaximumPossibleLengthOfString];

  4. #4
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    Yea thats also a good idea.....

  5. #5
    Unregistered
    Guest
    another example of two different ways to read the same question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help understanding arrays of strings!
    By smoking81 in forum C Programming
    Replies: 18
    Last Post: 02-23-2008, 03:24 AM
  2. Strings and Substrings using arrays
    By dcwang3 in forum C Programming
    Replies: 12
    Last Post: 02-18-2008, 06:28 AM
  3. arrays of strings
    By mbooka in forum C Programming
    Replies: 2
    Last Post: 02-19-2006, 06:55 PM
  4. strings or arrays of characters?
    By Callith in forum C++ Programming
    Replies: 13
    Last Post: 12-26-2004, 10:28 AM
  5. strings or character arrays
    By Shadow12345 in forum C++ Programming
    Replies: 2
    Last Post: 07-21-2002, 10:55 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21