LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-08-2007, 07:41 PM   #1
iwasapenguin
Member
 
Registered: Jul 2007
Posts: 110

Rep: Reputation: 15
C++ voodoo & getting one part of a string


Firstly the important part:
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:
char *string = "q string";
string = "very, very, very, long string";
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.
 
Old 12-08-2007, 09:10 PM   #2
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by iwasapenguin View Post
Firstly the important part:
How do I get say, one character from a string in C++ or perhaps get characters 4-17?
This is why I believe that hiding an implementation can be a hindrance. If this were straight C, it would be easy:
Code:
char blah[] = "My blah string"; // Creates a pointer to a string on the stack
char c = blah[3]; // Stores the 4th character in the string in c
The syntax for doing the same thing in C++ looks like this:
Code:
#include <string>

using namespace std;

int main(void)
{
    string blah("My blah string");
    char c = blah[3];

    printf("c = %c\n", c);

    return(0);
}
You can compile the above program using "g++ -o blah blah.cpp" and it will print out "c = b";
 
Old 12-08-2007, 09:52 PM   #3
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
Quote:
Originally Posted by iwasapenguin View Post
Firstly the important part:
How do I get say, one character from a string in C++ or perhaps get characters 4-17?
1 Character: As David said, you can get a character by just indexing it like a character array.
Substring: Use the substring function
Code:
string str = "Hello World!";
string substr = str.substr(3, 5); // substr = "lo wo"
Quote:
Originally Posted by iwasapenguin View Post
secondly I did try making a string class of my own (called "String") which was meant to be excellent at dealing with this
Luckily C++ already provides this class! Its called string

Please, check out, http://www.cplusplus.com/reference/string/string/

Last edited by 95se; 12-08-2007 at 09:54 PM.
 
Old 12-09-2007, 12:42 PM   #4
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by iwasapenguin View Post
Firstly the important part:
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 View Post

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:
char *string = "q string";
string = "very, very, very, long string";
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";
 
Old 12-09-2007, 07:40 PM   #5
iwasapenguin
Member
 
Registered: Jul 2007
Posts: 110

Original Poster
Rep: Reputation: 15
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C: Extracting part of a string trevorv Programming 3 08-29-2007 04:36 PM
remove part of string in bash script crewblunts Programming 2 03-16-2006 05:54 PM
Simple bash script help, grabbing part of a string colabus Linux - Newbie 3 04-25-2005 09:42 AM
Print a part of variable/string p0tw0r Linux - Newbie 1 04-07-2005 02:49 PM
voodoo 3 & its driver Infinite_Pizza Linux - Hardware 0 06-19-2003 06:02 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:20 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration