+ 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)

27th Oct 2018, 7:50 PM
NipIsTrue
16 Respostas
+ 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
27th Oct 2018, 8:19 PM
sneeze
sneeze - avatar
+ 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.
28th Oct 2018, 6:38 AM
Babak
Babak - avatar
+ 5
NipIsTrue Any modern compiler whose standard complies with C++98/03 and above has most of them, including SL's.
28th Oct 2018, 7:36 AM
Babak
Babak - avatar
+ 3
NipIsTrue lol. ;) visit here to get the big picture: https://en.cppreference.com/w/cpp/header
28th Oct 2018, 7:13 AM
Babak
Babak - avatar
+ 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?! ;)
28th Oct 2018, 10:19 AM
Babak
Babak - avatar
27th Oct 2018, 8:09 PM
NipIsTrue
+ 2
Oh ok. I didnt know you could do that. Thanks!
27th Oct 2018, 8:30 PM
NipIsTrue
+ 1
What is the type of your string ? Can you show use the code that you have ?
27th Oct 2018, 7:55 PM
sneeze
sneeze - avatar
+ 1
Really any strinf
27th Oct 2018, 8:09 PM
NipIsTrue
+ 1
You can use [] brackets to access individual characters of a string. For ex: string s = "something"; char c = s[1]; /*Outputs 0*/
28th Oct 2018, 6:32 AM
Kavish Bamboli
Kavish Bamboli - avatar
+ 1
I didnt know if sololearn has libraries other than iostream
28th Oct 2018, 6:56 AM
NipIsTrue
+ 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
28th Oct 2018, 7:09 AM
kaligotla Ravi Tej
kaligotla Ravi Tej - avatar
+ 1
Wow i didnt know there were that many... Does sololearnā€™s compiler have all of these
28th Oct 2018, 7:16 AM
NipIsTrue
+ 1
C++ Soldier (Babak) To focus on explaining. That a string is a char array. I took the first available string.
28th Oct 2018, 9:38 AM
sneeze
sneeze - avatar
0
its simple by their index no.. string a="NiplsTrue"; for(int i=0;a[i]!='\0';++i) cout<<a[i]; //output NiplsTrue
29th Oct 2018, 12:08 AM
priyesh
priyesh - avatar
0
I can't understand that namespace stuff. please I need some help
29th Oct 2018, 1:33 PM
Vie Nikky
Vie Nikky - avatar