+ 4
Accessing individual characters of strings in c++
If I have a string in c++, is there a way for me to access/change certain letters of that string? (Such as the first letter, or fourth letter, etc)
16 Antworten
+ 8
Although you call it a string it is still is a char array.
So you just can use
test[1];
https://code.sololearn.com/cQZPukESkD9Y
+ 6
sneeze
Why didn't you include the string header in the code? Omitting the string header just makes the code to rely on the implementation of the library since some headers has already included inside other headers ( <string> inside <iostream> in this case ) and therefore, shouldn't be count on.
+ 5
NipIsTrue Any modern compiler whose standard complies with C++98/03 and above has most of them, including SL's.
+ 3
NipIsTrue lol. ;)
visit here to get the big picture: https://en.cppreference.com/w/cpp/header
+ 3
sneeze
That's A-OK, no problem. But then you see the OP didn't even aware of such identity!
And BTW, not just adding a necessary header doesn't take too much time and effort at the top of the list but also prevents further hiccups in case of executing the code in another compiler. And finally, I think you believe in promoting good programming practices, don't you?! ;)
+ 2
Oh ok. I didnt know you could do that. Thanks!
+ 1
What is the type of your string ?
Can you show use the code that you have ?
+ 1
Really any strinf
+ 1
You can use [] brackets to access individual characters of a string.
For ex:
string s = "something";
char c = s[1];
/*Outputs 0*/
+ 1
I didnt know if sololearn has libraries other than iostream
+ 1
https://code.sololearn.com/c01jUbKI0el4/?ref=app
you can use char data type in place of string so that you can access individual characters
+ 1
Wow i didnt know there were that many... Does sololearn’s compiler have all of these
+ 1
C++ Soldier (Babak) To focus on explaining. That a string is a char array. I took the first available string.
0
its simple by their index no..
string a="NiplsTrue";
for(int i=0;a[i]!='\0';++i)
cout<<a[i];
//output NiplsTrue
0
I can't understand that namespace stuff. please I need some help