Returns the longest consecutive numeric string.

Source: Internet
Author: User
1. Write a function. Its prototype is intcontinumax (char * outputstr, char * intputstr). function: Find the longest consecutive numeric string in the string and return the length of the string, and pay the longest numeric string to the parameter outputstr.
// 1. write a function. The original form is int continumax (char * outputstr, char * intputstr) // function: // find the longest consecutive numeric string in the string, return the length of the string, // and pay the longest numeric string to the memory specified by one of the function parameters outputstr. // For example, after the first address of "abcd12345ed125ss123456789" is passed to intputstr, the function returns 9. The value outputstr indicates is 123456789 # include
 
  
# Includeint continumax (char * outputstr, char * inputstr) {assert (outputstr); assert (inputstr); int length = 0; int maxlength = 0; int I = 0; int j = 0; while (inputstr [I]! = '\ 0') {while (inputstr [I]> = '0' & inputstr [I] <= '9') {length ++; I ++ ;} if (length> maxlength) {maxlength = length; int k = I-maxlength; for (j = 0; j <maxlength; j ++) {outputstr [j] = inputstr [k ++];} length = 0; continue;} I ++; length = 0 ;}outputstr [j] = '\ 0 '; return maxlength;} int main () {char inputstr [] = "abcd12345eddafsd125ss123456789"; char outputstr [100]; int max_numstr_length = continumax (outputstr, inputstr ); printf ("% s \ n", outputstr); printf ("the max_numstr_length is % d \ n", max_numstr_length); return 0 ;}
 
# Include
 
  
# Include
  
   
Int continumax (char * outputstr, char * inputstr) {int len = 0; // count the length of the numeric string int max = 0; // The length of the current maximum numeric string char * pstr = NULL; // records the starting position of the maximum numeric character while (* inputstr! = '\ 0') {if (* inputstr <= '9' & * inputstr> = '0') // counts the length of the number substring {len ++; inputstr ++; continue;} else if (len> max) // if the calculated numeric string is greater than the length of the current maximum numeric substring, update {max = len; pstr = inputstr-len; len = 0;} inputstr ++;} for (int I = 0; I
   
    

 

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.