Find the oldest string consisting of all specified characters in the string

Source: Internet
Author: User

Find the oldest string consisting of all specified characters in the string

/**! ========================================================== ===================================== *! FNAME: search. cpp *! BRIEF :*! AUTHR: RollStone *! EMAIL: jealdean@outlook.com *! VERNO: 1.0.31 *! CREAT: 23:43:04 *! CHGON: 07:53:00 *! *! Copyright (c) 2015 All Rights Reserved By Abodu Org *! ========================================================== =====================================* // Compile a C function, this function finds in a string that the longest possible sub-string is composed of specified characters # include
  
   
# Include
   
    
# Include
    
     
# Ifndef strndup // manually copy the first n characters of the string char * strndup (const char * src, int nMax) {int srcLen = strlen (src); int n = (srcLen
     
      
= 0) dest [n] = src [n]; return dest ;} # endif/*** @ brief * Find the longest possible substring in a string (the substring consists of the same character) * @ param str [] * @ param tc target character * @ param rcLen returns the length of the target substring ** @ return * tc does not appear in str, return NULL, otherwise, the start position */char * search_max_substr (char src [], char ch, int * rcLen) {char * pStart; if (! Src |! (PStart = strchr (src, ch) {return NULL;} * rcLen = 1; // at least one char * pEnd = strrchr (src, ch ); if (pEnd = pStart) {// there is only one unique character ch return pStart;} char * rcOut = pStart; char * pCur = pStart + 1; while (pCur-1 <= pEnd) {if (* (pCur-1) = * pCur) {pCur ++; continue ;} // The content of the current pointer is different from that of the previous pointer next to it. if (pCur-pStart> * rcLen) {// The number of records found is greater than rcLen rcOut = pStart; * rcLen = pCur-pStart; // record the number of old moves} // find the first ch in the remaining string if (! PCur |! (PStart = strchr (pCur, ch) {break;} pCur = pStart + 1;} return rcOut;} int main (int argc, char * argv []) {char s [] = "aaaabbbccddeffffffff"; int n = 0; char * p = search_max_substr (s, 'D', & n); if (p) {char * sbk = strndup (p, n); if (sbk) {printf ("RESULT: % s \ n", sbk); free (sbk );}} return 0;} // end main
     
    
   
  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.