Thread: how to return specific string pattern from input string if searched pattern Found.

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    50

    how to return specific string pattern from input string if searched pattern Found.

    I want to implement in C/C++ function that its input is an array of chars like char string[]="010101010001110001110001110001110001110001110001 11000111000111000111000111000111000111" and to search on specific string pattern, then the function returns an array of chars or string (same concept) according to last index of my searched pattern + 6 steps, for example according to the same input that I attached above, lets assume I want to search on string pattern '01010101' , so if the search pattern found in my input *if it's found *, return the string that its index in my input is : last index of my searched pattern in my input + 15 => return the string that starts from last index of my searched pattern in my input and ends last index of my searched pattern in my input + 6 , in my example it should return : returns the extraction of required string in another char array[]: "000111" why? because I searched for '01010101' and the last index of this in my input is at index=7 ,so the returned string starts from index=8, and I add +6 to my index=7 so it ends at 7+6 =13 , so the returned string (extracted string from my input) will be from index =8 till index=13, so the returned array of chars in my example is: "000111" for more clarification, the searched string pattern in my input is "01010101" , if not found then return null .. , if found (like my example ) then I take the last index in my input array of my searched pattern that in my example last index is at index=7, so my returned string will start at index=8 of my input string , and will be longed till (last index of my searched pattern=7 ) + 6 steps through my input string so at index =13 my returned string(required string) will end.
    so the output of my example is : "000111".

    if the searched pattern not found, then return NULL or Enum type EROR.

    Any help please how can I do that in C/C++ ? thanks alot for any help!!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,178
    Lookup the strstr() function.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2019
    Posts
    50
    Thanks for your hint!
    this is the code that I have done for finding the first index of every appearance of my searched pattern.
    Code:
    #include
    usingnamespace std;
    main(){
       string str1 ="1100001"; // this example just for finding out if my function gives me the first index of every the specific pattern appearance
       string str2 ="11";
       int pos =0;
       int index;
       while((index = strstr(str1,str2))!=NULL){
          cout <<"Match found at position: "<< index << endl;
          cout<< "Required String starts from index + 1 till the end is" <<str1[str1.length()-(index)] ; % in my case it should print "100001" because the first index of 11 is index=zero so the string should starts from index+1  
    
          pos = index +1;//new position is from next element of index
       }
       
    }
    


    but unfortunately Im not getting the correct output, Im not getting in my case the string "100001" , thanks for any help!

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,944
    Quote Originally Posted by Brian_Teir
    I want to implement in C/C++ function
    Refer to Stroustrup's answer to the FAQ: What do you think of C/C++?
    Quote Originally Posted by Bjarne Stroustrup
    No that's not really a question I often get. In that sense, it is the only "fake FAQ" in this FAQ. However, it ought to be a FAQ because people use "C/C++" as if it meant something specific and as if they knew what it meant, leading to much confusion and misery. People should ask "What is C/C++?" and then on reflection stop using the term. It does harm.

    There is no language called "C/C++". The phrase is usually used by people who don't have a clue about programming (e.g. HR personnel and poor managers). Alternatively, it's used by people who simple do not know C++ (and often not C either). When used by programmers, it typically indicates a "C++ is C with a few useful and a lot of useless complicated features added" attitude. Often, that is the point of view of people who like to write their own strings and hash tables with little knowledge of the standard library beyond printf and memcpy. There are people who stick to a restricted subset of C++ for perfectly good reasons, but they (as far as I have noticed) are not the people who say "C/C++".
    So, you have posted (corrupted) C++ code in the C programming forum, using Salem's good advice for C that becomes poor advice when applied to C++ using std::string. Shall I move this to the C++ programming forum?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    740
    Quote Originally Posted by Brian_Teir View Post
    Thanks for your hint!
    this is the code that I have done for finding the first index of every appearance of my searched pattern.
    Code:
    #include
    usingnamespace std;
    main(){
       string str1 ="1100001"; // this example just for finding out if my function gives me the first index of every the specific pattern appearance
       string str2 ="11";
       int pos =0;
       int index;
       while((index = strstr(str1,str2))!=NULL){
          cout <<"Match found at position: "<< index << endl;
          cout<< "Required String starts from index + 1 till the end is" <<str1[str1.length()-(index)] ; % in my case it should print "100001" because the first index of 11 is index=zero so the string should starts from index+1  
    
          pos = index +1;//new position is from next element of index
       }
       
    }
    


    but unfortunately Im not getting the correct output, Im not getting in my case the string "100001" , thanks for any help!
    You are also in the wrong forum to ask about a C++ code question

    This is the forum to discuss the C Programming Language only.

  6. #6
    Registered User
    Join Date
    Feb 2019
    Posts
    722
    This is WRONG:

    Code:
    int index;
    
    while ( ( index = strstr( str1, str2 ) ) != NULL ) ...

  7. #7
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    174
    Quote Originally Posted by Brian_Teir View Post
    but unfortunately Im not getting the correct output
    How could you? It doesn't even compile!
    The power of null.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to find strings that match specific pattern
    By RyanC in forum C Programming
    Replies: 5
    Last Post: 12-02-2018, 10:23 AM
  2. Search a pattern " ) , (" in string array
    By aj74 in forum C Programming
    Replies: 2
    Last Post: 08-22-2011, 11:23 AM
  3. pattern checking in a string question..
    By transgalactic2 in forum C Programming
    Replies: 31
    Last Post: 12-26-2008, 04:54 PM
  4. string pattern recognition?
    By barneygumble742 in forum C++ Programming
    Replies: 4
    Last Post: 07-17-2005, 06:07 PM
  5. string pattern search problem
    By goron350 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 08:50 AM

Tags for this Thread