Read in a string str and output the longest continuous number string in the string str
a str with a length of no more than 255
Example:
enter:
abc123defg123456789hjfs123456
output:
123456789
The procedure is as follows:
#include <stdio.h> #include <string.h> intmain() { int i,k,key,sum,max,len[300]; char a[300]; scanf("%s",a); for(int j=0;j<300;j++) { len [j] = 0; } for(i=0;i<strlen(a);i++) { k=i; sum=0; while(a[i]>='0' && a[i]<='9') { i++; sum++; } len[k]=sum; //Save the length of the array string that appears in each position in len[], and 0 if it does not appear } max=0; for(i=0;i<strlen(a);i++) { if(max<len[i]) { max = len[i]; //Save the length of the longest number string key = i; //Record the longest number string, the initial address } } for(i=0;i<max;i++) //output from the key subscript, max numbers { printf("%c",a[key++]); } printf("\0"); printf("\n"); return 0; }
Screenshot of the program running:
Because python has built-in functions and libraries, it is very convenient to call
Let's take a look at the code to solve this problem in python:
import re input_val = input() split_char = re.split(r'\D*', input_val) print(max(split_char, key=len))