Find all the oldest strings in the string that are made up of the specified characters

Source: Internet
Author: User

/* *!============================================================== *! 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

Related Article

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.