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