+ 3

Can arrays store strings?

In examples, string in arrays not discussed

1st Jul 2016, 7:03 PM
Abdur-Rahmaan Janhangeer
Abdur-Rahmaan Janhangeer - avatar
12 Respostas
+ 3
Hello Just include <string> and follow this steps: std::string str[2] = {"str1","str2"}; <string> it's a class template Check this sample in my profile: https://code.sololearn.com/cY87LGpWyUud/#
2nd Jan 2017, 11:59 PM
Laeston Jsp
Laeston Jsp - avatar
+ 2
okay thanks struct array{ char string [10]; }; void main(){ array A[10]; } obviously you have to write all the libraries and stuff, but that's the idea, here you see you have the array "A" and every element of the array "A" has inside a string (BTW to access to the string you'll have to use A.string[]) hope I helped , let me know if I didn't make myself clear
1st Jul 2016, 8:41 PM
Cristian Guerra
Cristian Guerra - avatar
+ 2
yes it can store strings. there is a.predefined function for it
2nd Jan 2017, 6:19 AM
Deepanshu pandey
Deepanshu pandey - avatar
+ 1
yes, it can
1st Jul 2016, 7:21 PM
Brandon Juarez
Brandon Juarez - avatar
+ 1
yes, if you know how to use structures, all you need to do is create a structure with strings inside of it and then declare an array with those structures (I don't know if that's clear enough, sorry English isn't my first language)
1st Jul 2016, 8:30 PM
Cristian Guerra
Cristian Guerra - avatar
+ 1
okay, let's see : the string is inside the structure okay? and what we are doing is declaring an array where every element is a structure,and inside that structure you'll have a string. so just by declaring array A[10] (following my example)you are stating that every element of the array A will be a structure type (a structure "array"(and that's just a random name ) to me more exact) and in that structure it's only declared a string so to access that string you'll have to use A[x].string (BTW I was wrong in the first example this is the way to use it)
1st Jul 2016, 8:53 PM
Cristian Guerra
Cristian Guerra - avatar
+ 1
yes. a string is nothing but a character array void main () { char a[10],I; cout<<" enter a string"; for(I=0;I<5;I++) cin>>a[I]; cout<<"the strings are"; for(I=0;I<5;I++) cout<<a[I]; } sample run: enter a string dhanu Manu janu gagan Ravi the strings are dhanu manu janu gagan ravi this is how the arrays works.
2nd Jan 2017, 1:41 PM
Dhananjaya K Dhanu
Dhananjaya K Dhanu - avatar
+ 1
yes array store strings
2nd Jan 2017, 3:50 PM
Maria Malik
Maria Malik - avatar
0
@juarez and how?
1st Jul 2016, 7:23 PM
Abdur-Rahmaan Janhangeer
Abdur-Rahmaan Janhangeer - avatar
0
@Guerra your english is excellent, just show me an example please
1st Jul 2016, 8:33 PM
Abdur-Rahmaan Janhangeer
Abdur-Rahmaan Janhangeer - avatar
0
@Guerra how to add a string in it or declare it with a string inside?
1st Jul 2016, 8:44 PM
Abdur-Rahmaan Janhangeer
Abdur-Rahmaan Janhangeer - avatar
0
@Guerra Thanks but if anybody has more clarifications, just jot them down here. (don't consider this closed.)
2nd Jul 2016, 5:49 AM
Abdur-Rahmaan Janhangeer
Abdur-Rahmaan Janhangeer - avatar