ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How do I get say, one character from a string in C++ or perhaps get characters 4-17?
secondly I did try making a string class of my own (called "String") which was meant to be excellent at dealing with this (it wasn't) using a piece of voodoo code that I came up with by accident:
The main prob is that the program results in a segmentation error
if you try to directly change a single character in string but you can create a temporary traditional array to change it in and then changing string to equaling the array (remember this is all in class so the programmer using String doesn't need to know).
Still I am faced with the question of why it works which would be needed to take out the kinks.
How do I get say, one character from a string in C++ or perhaps get characters 4-17?
As has been mentioned, you can use std::string::operator[]() or std::string::at() for accessing the character at a certain index. The first is faster, but the second is more “safe” (since it does bounds checking). Also notice that the terminating NUL is not accessible using either of these (so if your string is “std::string s = "hello";” and you want to try “char c = s.at(5);” the result is the throwing of an out-of-range exception and not the ASCII NUL you might expect). Additionally, you can use std::string::substr().
Quote:
Originally Posted by iwasapenguin
secondly I did try making a string class of my own (called "String") which was meant to be excellent at dealing with this (it wasn't) using a piece of voodoo code that I came up with by accident:
The main prob is that the program results in a segmentation error
This voodoo code shouldn’t result in segmentation error, but it probably doesn’t do what you thought it would. A very similar statement (if you used an array) would result in a segmentation error. You can’t assign string literals to arrays except at initialization (this is also true of C). You can’t modify string literals assigned to pointers (but you can if they are elements of an array). I.e.,
Code:
// This is *WRONG* (and might segfault)
char *s1 = "Hats are cool";
s1[0] = 'R';
// This is right
char s2[] = "Dislexia";
s2[1] = 'y';
// This is *WRONG* (and might segfault)
char s3[] = "q string";
s3 = "r string";
// This is allowed, but not advisable
char *s4 = "s string";
s4 = "t string";
Well I think that should do the trick. Thanks to all who didn't say that I should use arrays directly.
Oh and 95se, the reason that I wrote my own class for string is because I didn't know about std::string::substr(), hence mine. Thanks anyway.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.