How do you put strings into arrays
How do you put strings into arrays
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...
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];
Yea thats also a good idea.....
another example of two different ways to read the same question.