1

Question: How do I extract a character from a string that is an array?

Explained: Normal strings

 string example=("Stack Over Flow");
 cout<<example[1];

The output will be:

 t

What I want is to extract a letter from an array of strings example:

string str[4];
str[0]="1st";
str[1]="2nd";
str[2]="3rd";
str[3]="4th";
cout<<str[2];

will print

3rd

how could i get the "t" from the str[0]?

7
  • 2
    string example=("Stack Over Flow"); is not an array. Commented Feb 11, 2016 at 13:30
  • 2
    @soon OP never said it was. Commented Feb 11, 2016 at 13:32
  • 2
    You already know how to get a single characters from a std::string object, and e.g. str[0] is a std::string object. Commented Feb 11, 2016 at 13:32
  • 1
    @rozina, so, what does "Explained: Normal array" mean? Commented Feb 11, 2016 at 13:34
  • Sorry wrong choice of words, what i mean to say is: the string consist of group of characters. Misused the word array Commented Feb 11, 2016 at 13:34

5 Answers 5

6

just by doing as follow:

str[0][2]; // third character of first string

Some more examples:

string str[4];
str[0]="1st";
str[1]="2nd";
str[2]="3rd";
str[3]="4th";
cout<<str[0][2]<<endl; // t
cout<<str[2][1]<<endl; // r
cout<<str[3][2]<<endl; // h
Sign up to request clarification or add additional context in comments.

7 Comments

Does that mean, i just need to add another []. when i use 2 dimensional array in strings? like: string str[4][4]; to access the characters I just need to: std::cout<<str[n][n][n]; (n) is a number
@PH.nischkar No. [0] is for the first string in the array. [2] is for the third character in that string. What do you think [n] on a char would do?
I am not sure what do you mean by "2 dimensional array in strings". If you have an array of string (string myArray[5]) you want to access the full string located at a specific index by doing myArray[string_index] , and then access the character of that particular string: myArray[string_index][char_index]
@PH.nischkar If you have a 2 dimensional array of strings then yes - std::string array[4][4]. Otherwise you have to explain what is a 2 dimensional string.
@PH.nischkar if you have an 2 dimensional array OF string (e.g. string myArray[4][5]). you need to access a character doing the following: myArray[string_row_index][string_col_index][char_index]
|
1
std::string str[4];
str[0]="1st";
str[1]="2nd";
str[2]="3rd";
str[3]="4th";

Here str is an array of std::string objects. As you know you access elements of an array with operator[]. So the first string in the array is accessed with str[0].

std::string offers operator[] as well. With it you can access characters of the string.

So lets take it step by step.

str - array
str[0] - std::string
str[0][0] - first character of the string str[0]

Comments

0

YOu need one more operator[] call. str[2] is using the [] of the array and returns a reference to the array element at index 2. If you want to get the second character of the first array element then you need

str[0][2]
    ^  ^
string |
       character

Comments

0

Class std::string has its own overloaded operator [] that you used in the first your code snippet

 string example=("Stack Over Flow");
 cout<<example[1];

If you have an array of objects of type std::string then at first you need to access the desired object stored in the array using the built-in subscript operator [] of arrays as for example

string str[4];
cout << str[1];

In this code snippet expression str[1] returns string stored in the second element (with index 1) of the array. Now you can apply the overloaded operator [] of the class std::string as for example

string str[4];
cout << str[1][1];

Comments

0

You get 't' instead of 's' because you are printing it like this cout<<example[1];

you sohuld do it like this:

cout<<example[0][2];

5 Comments

Why [1]? OP wanted the t; the third character.
Read the question again.
You have to read the question more precisely. OP wants 't' from str[0]
My bad then, anyway, the way how you do it is the same, you just move one slot forward. Shouldn't be too hard to figure that one out...
@mheonyae That doesn't make your answer any better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.