How would it be done to make an array containing several Strings in each position in c++? I find myself with an inconvenience, I am not very clear how to establish an array with Strings and how to access each String and how to access each letter of one of the String. The concept would be the following:
words=[10]={"Londres","Paris","Dublin","Madrid","Roma","Atenas","Lisboa","Berlin","Amsterdam","Cardiff"}
My intention is through a variable that contains a random number that will be the pointer that will indicate a position of the words array , so I would need to know the length of the array, but I understand that the following expression is not useful in this case:
strlen(words)
and once one of the strings in the array has been selected, I need to go through the characters of the selected string and then modify those characters. How would it be programmed? I'm not sure how to approach it.
He
namespace std
provides you with the typestring
. You can create type arraysstring
and initialize them with strings.It is a normal array, you can perform iterations on its elements and access its elements by indices.
Cycle
for
that shows on the screen each element of the arraycout
of an array indexActually, strings are character arrays, that is, strings have the same properties that an array type would have
char
. Therefore you can access each letter by its index or by iterationsfor ... in
orfor
.cout
of the first letter of the last element of the arrayThe prototype of
strlen()
according to this documentation isA pointer
char
is not the same as astring
. If you try to use astrlen()
with an element typestring
you get an error.In any case, the example you give would not be valid, since it
words
is an array.I hope I've helped.