/* *!============================================================== *! FNAME:search.cpp *! BRIEF: *! Authr:rollstone *! Email: [Email protected] *! verno:1.0.31 *! Creat:2015-04-28 23:43:04 *! CHGON:2015-04-30 07:53:00 *! *! Copyright (c) Rights Reserved by Abodu Org *!============================================================== */< /c0>//// Write a C function that finds the longest possible of all the substrings that are composed of the specified characters in a string#include <stdio.h>#include <string.h>#include <stdlib.h>#ifndef strndup//manual implementation of the first n characters of the copied stringChar* Strndup (Const Char* SRC,intNMax) {intsrclen=strlen(SRC);intN= (Srclen<nmax) Srclen:nmax;Char* Dest= (Char*)calloc(n+1,sizeof(Char)); while(--n>=0) Dest[n]=src[n];returnDest;}#endif/** * @brief * Find the longest possible substring in a string (the substring consists of the same character) * @param str[] * @param TC Target character * @param the length of the target substring returned by Rclen * * @r Eturn * TC does not appear in STR, returns NULL, otherwise returns the starting position of the oldest string */Char* SEARCH_MAX_SUBSTR (CharSrc[],CharChint* Rclen) {Char* Pstart;if(!src| |! (pstart=STRCHR(src,ch))) {returnNULL; } *rclen=1;//There is at least one Char* pend=STRRCHR(SRC,CH);if(Pend==pstart) {//Have and only have a single character ch returnPstart; }Char* Rcout=pstart;Char* pcur=pstart+1; while(pcur-1<=pend) {if(* (pcur-1) ==*pcur) {pcur++;Continue; }//The current pointer is not the same as the contents of the previous pointer immediately preceding it if(Pcur-pstart>*rclen) {//Find number greater than RclenRcout=pstart; *rclen=pcur-pstart;//Record the number of old moves}//Find the remaining string within the first ch if(!pcur| |! (pstart=STRCHR(pcur,ch))) { Break; } pcur=pstart+1; }returnRcout;}intMain (intargcChar* argv[]) {Chars[]="AAAABBBCCDDEFFFFFFFFFF";intn=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
Find all the oldest strings in the string that are made up of the specified characters