在字符串中找出连续最长数字串

import java.util.Scanner;

public class Test7 {
	/*
	华为OJ——在字符串中找出连续最长的数字串
 	题目描述
	样例输出
	输出123058789,函数返回值9
	输出54761,函数返回值5
	 
	接口说明
	函数原型:
	   unsignedint Continumax(char** pOutputstr,  char* intputstr)
	输入参数:
	   char* intputstr  输入字符串;
	输出参数:
	   char** pOutputstr: 连续最长的数字串,如果连续最长的数字串的长度为0,应该返回空字符串;
		如果输入字符串是空,也应该返回空字符串;  
	返回值:
	  连续最长的数字串的长度
	 
	输入描述:
	输入一个字符串。
	输出描述:
	输出字符串中最长的数字字符串和它的长度。如果有相同长度的串,则要一块儿输出,但是长度还是一串的长度
	输入例子:
	abcd12345ed125ss123058789
	输出例子:
	123058789,9
	将不是数字的字符全部变成‘a’,再将字符串用a来分割称数组,长度最大的数组的长度即为所求的长度,
	再输出对应数组元素即可
	*/
	public static void main(String[] args) {
		Scanner scn=new Scanner(System.in);
		while(scn.hasNext()){
			String str=scn.next();
			Continumax(str);
		}
	}
	public static int Continumax(String pOutputstr){
		if(pOutputstr==null||pOutputstr.length()==0){
			System.out.println("");
		}
		for (int i = 0; i < pOutputstr.length(); i++) {
			if(!(pOutputstr.charAt(i)>='0'&&pOutputstr.charAt(i)<='9')){
				pOutputstr=pOutputstr.replace(pOutputstr.charAt(i), 'a');
			}
		}
		String[] strs=pOutputstr.split("a");
		int max=0;
		int mark=0;
		for (int i = 0; i < strs.length; i++) {
			if(!strs[i].equals("")){
				if(strs[i].length()>=max){
					max=strs[i].length();
					//mark=i;
				}
				
			}
		}
		for (int i = 0; i < strs.length; i++) {
			if(strs[i].length()==max){
				System.out.print(strs[i]);
			}
		}
		System.out.println(","+max);
		return max;
	}
}

猜你喜欢

转载自blog.csdn.net/m0_38068868/article/details/81814583