LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   i need to find a substring in a string and get the next 3 characters (https://www.linuxquestions.org/questions/linux-newbie-8/i-need-to-find-a-substring-in-a-string-and-get-the-next-3-characters-793481/)

anurupr 03-05-2010 03:00 PM

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 ?

TB0ne 03-05-2010 03:11 PM

Quote:

Originally Posted by anurupr (Post 3887419)
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.

anurupr 03-05-2010 03:16 PM

oh ok... ill check that out .. is it possible in c++ ?

Tinkster 03-05-2010 03:38 PM

Certainly

TB0ne 03-05-2010 04:23 PM

Quote:

Originally Posted by anurupr (Post 3887445)
oh ok... ill check that out .. is it possible in c++ ?

Yes, and in pretty much any other programming language too.

grail 03-06-2010 02:11 AM

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

anurupr 03-07-2010 12:52 AM

@grail i considered that .. the string that im going to input into this program is going to be of a proper form

dsmyth 03-07-2010 05:41 PM

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.

devnull10 03-07-2010 05:45 PM

Code:

man string
use strstr to locate the substring

Code:

man strstr


All times are GMT -5. The time now is 09:47 AM.