LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-05-2010, 04:00 PM   #1
anurupr
Member
 
Registered: Mar 2010
Posts: 71

Rep: Reputation: 16
i need to find a substring in a string and get the next 3 characters


for example

010100
this is the string

the substring is 010

i need to search for this substring and i also need to return the next 3 characters i.e 100.

i need to apply this for this string

01010000001001100000100010


is this possible somehow ?
 
Old 03-05-2010, 04:11 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,945

Rep: Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693
Quote:
Originally Posted by anurupr View Post
for example

010100
this is the string

the substring is 010

i need to search for this substring and i also need to return the next 3 characters i.e 100.

i need to apply this for this string

01010000001001100000100010
is this possible somehow ?
Yes it is, very possible. Read the man pages on grep and awk, and some basic shell-scripting tutorials.
 
Old 03-05-2010, 04:16 PM   #3
anurupr
Member
 
Registered: Mar 2010
Posts: 71

Original Poster
Rep: Reputation: 16
oh ok... ill check that out .. is it possible in c++ ?
 
Old 03-05-2010, 04:38 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Certainly
 
Old 03-05-2010, 05:23 PM   #5
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,945

Rep: Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693
Quote:
Originally Posted by anurupr View Post
oh ok... ill check that out .. is it possible in c++ ?
Yes, and in pretty much any other programming language too.
 
Old 03-06-2010, 03:11 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,254

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
You may also need to consider all your output options here too:

eg. your first string '010100' contains the characters 010 twice.

Also, what if you encounter 010 but there are not 3 characters after (ie 01011).
Do you retrun just the 2 characters, do you pad the last character (if so, with what)
or do you simply ignore this scenario as invalid as not 3 characters.

Just a thought

btw. these considerations are irrelevant of language or program used

Last edited by grail; 03-06-2010 at 03:12 AM.
 
Old 03-07-2010, 01:52 AM   #7
anurupr
Member
 
Registered: Mar 2010
Posts: 71

Original Poster
Rep: Reputation: 16
@grail i considered that .. the string that im going to input into this program is going to be of a proper form
 
Old 03-07-2010, 06:41 PM   #8
dsmyth
LQ Newbie
 
Registered: Mar 2010
Location: Glasgow, Scotland
Distribution: Fedora 12
Posts: 26
Blog Entries: 6

Rep: Reputation: 17
Hi, most languages (and I'm thinking Ruby and Python) have a String class that contains methods that let you search for substrings. The result of these methods is usually the index (the position) of the first character within the string. Using this position + the length of the searched substring you can find the start position of the following 3 characters.

the string "supercalifragilisticexpialidocious"
is just a list or characters in an array

Code:
supercalifragilisticexpialidocious
0123456789012345678901235456789012
          111111111122222222222333
so a search for 'frag' using these methods will return 9, the position of 'f'. 'frag' is 4 characters long so add 4 to 9 and you get 13 (the i character).

I'm not sure if C++ has a String class but I'm fairly confident that there will be methods that will find the position of a substring within a longer string; no idea what they are though.

Hope this was a little bit helpful.

Last edited by dsmyth; 03-07-2010 at 06:47 PM.
 
Old 03-07-2010, 06:45 PM   #9
devnull10
Member
 
Registered: Jan 2010
Location: Lancashire
Distribution: Slackware Stable
Posts: 548

Rep: Reputation: 116Reputation: 116
Code:
man string
use strstr to locate the substring

Code:
man strstr

Last edited by devnull10; 03-07-2010 at 06:46 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
extract substring from string in C baddah Programming 6 02-02-2010 05:22 AM
replace a substring with another string in C zeppelin Programming 21 11-09-2009 10:59 PM
Fast algorithm needed to find a substring in a string jlinkels Programming 13 10-11-2007 09:43 PM
how many occurances of a substring are they n a string linuxmandrake Programming 1 04-11-2006 03:02 AM
find repeated characters in a string mcshen Programming 9 02-02-2004 06:43 PM


All times are GMT -5. The time now is 08:31 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration