+ 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.
11 Answers
+ 2
thx
+ 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.
+ 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.
+ 1
also i forgot to ask how to replace char from string into another place in the same string ex. string, trings
0
but how to count how much letters have string?
p. s.my english is bad... :|
0
swap doesnt work
0
it shows that swap class doesnt exist
0
maybe I did mistake in my code :|