+ 10
Index of a character in a string in an array
How can we get a character from a string kept in an array? arr = ["Bob", "Hello", "Sup"] For example, I want to get letter 'e' from arr[1] ("Hello") and I want the output to be '1' because the index of 'e' in 'Hello' is '1', Idk if that makes any sense but does anyone know?
33 Respostas
+ 10
Refer to this code:
https://code.sololearn.com/cDk92HnAZaOy/?ref=app
Basically, arr[1] is equal to "Hello"
That means arr[1].index('e') is equal to "Hello".index('e')
hope you understood
+ 18
If it's in C++, where strings are character arrays themselves, I'd be doing:
cout << (arr[1])[1];
I haven't tested yet. Will update shortly.
EDIT: Yep, it works this way in C++.
EDIT2: Yep, it is also working in Ruby.
puts((arr[1])[1])
Looks like I should find some time to master Ruby. I've always had a liking to the language and looks like it likes me too. :>
EDIT3: Thanks to @Supersebi for pointing out my flaws. The correct code should be:
puts(arr1.index('e'))
+ 17
puts(arr[1].index('e'))
Just off the top of my head.
Maybe put it in a for loop.
+ 16
@SuperSebi3 It's alright. @Hatsy Rei won't get it wrong any more
+ 16
@supersebi3 Oops, my bad
+ 15
@Supersebi3 Sorry for getting your name wrong. I'm personally a victim too (Hasty Rei, Hatsey Rei) so I understand.
+ 15
@MrCoder looks like someone hasn't updated their SoloLearn app :P
+ 11
I have to choose one of you to be the best answer! :O
+ 10
Oh alright @Jafca @Hatsy Rei @Luka @Supersebi3 Thank you very much :D PS: Dont fight here lol
+ 10
@Hatsy Rei Ohh... Woops didn't get a notification about itš Hold on buddies
+ 9
@Hatsy Rei why do I have to tell this every community I enter...
IM Supersebi3 NOT SuperSebi3!!!!!! THE SECOND s IS SMALL
+ 9
@MrCoder mark best answer pls
+ 9
@Hatsy Rei :)
+ 9
Anyone thinking of a random generator(ā Ķ”ā ĶŹ Ķ”ā )
+ 9
@SATYAJEET JHA thats how it works in ruby too, but he wanted to get the index number of 'e' in "Hello", not the character
and this is solved anyways
+ 8
@Jafca but you did š”š”š”š”
+ 8
@jaFca small first s is okay
+ 8
@Supersebi3 I dont even know how but I wanted to
+ 8
@MrCode press the checkmark below the like/dislike buttons
+ 8
also I updated my code to have a fourth way without any variable