how to get char or symbol from string in c++? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

how to get char or symbol from string in c++?

Hello, how i can get char or symbol to replace in string in c++, cuz i dont know and please help.

16th Feb 2020, 6:19 PM
Grzesiek Sikorski
Grzesiek Sikorski - avatar
8 Answers
+ 2
thx
16th Feb 2020, 6:38 PM
Grzesiek Sikorski
Grzesiek Sikorski - avatar
+ 2
https://code.sololearn.com/c9mmstgViC8K/?ref=app Heres an example of what swim said. Also i would like to tell you that string has many useful builtin functions. Sadly, those are not covered in sololearn tutorials. I would suggest you look them up on google or youtube.
16th Feb 2020, 6:41 PM
Salman Nazeer
Salman Nazeer - avatar
+ 1
Like this #include<iostream> using namespace std; int main() { string str = "hello"; cout<<str[0]; // char stored at position 0 return 0; } or maybe like this: #include<iostream> using namespace std; int main() { string str = "hello"; char c; c = str[0]; // assigning character at 0th position in string to a variable 'c'. cout<<c; return 0; } explanation: str[ i ] gives the character stored at i th location in the string. where i starts from 0 to one less than length of string. For further clarification, string stores an array of characters inside it. Each member of this array(the letters we want) can be accessed using square brackets and index values like in the examples above.
16th Feb 2020, 6:21 PM
Salman Nazeer
Salman Nazeer - avatar
+ 1
also i forgot to ask how to replace char from string into another place in the same string ex. string, trings
16th Feb 2020, 6:52 PM
Grzesiek Sikorski
Grzesiek Sikorski - avatar
0
but how to count how much letters have string? p. s.my english is bad... :|
16th Feb 2020, 6:35 PM
Grzesiek Sikorski
Grzesiek Sikorski - avatar
0
swap doesnt work
16th Feb 2020, 7:12 PM
Grzesiek Sikorski
Grzesiek Sikorski - avatar
0
it shows that swap class doesnt exist
16th Feb 2020, 7:12 PM
Grzesiek Sikorski
Grzesiek Sikorski - avatar
0
maybe I did mistake in my code :|
16th Feb 2020, 7:22 PM
Grzesiek Sikorski
Grzesiek Sikorski - avatar