求在一个字符串中连续出现次数最多的字符??? 用java 如何实现??
2个回答
展开全部
public class SearchChar {
public char search(String str){
int lenght=str.length();
int max=0;
int index=0;
int j;
char ch = str.charAt(0);
for(int i=0;i<lenght;){
index=0;
for(j=i+1;j<lenght;j++){
if(str.charAt(i)==str.charAt(j))
index++;
else
break;
}
if(index>max){
max=index;
ch=str.charAt(i);
}
i=j;
}
return ch;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
SearchChar sc=new SearchChar();
char ch=sc.search("ssddfffffffdseeeedsa");
System.out.println(ch);
}
}
public char search(String str){
int lenght=str.length();
int max=0;
int index=0;
int j;
char ch = str.charAt(0);
for(int i=0;i<lenght;){
index=0;
for(j=i+1;j<lenght;j++){
if(str.charAt(i)==str.charAt(j))
index++;
else
break;
}
if(index>max){
max=index;
ch=str.charAt(i);
}
i=j;
}
return ch;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
SearchChar sc=new SearchChar();
char ch=sc.search("ssddfffffffdseeeedsa");
System.out.println(ch);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询