我试图写入一个 Java 程序, 它将输入字符串, 并计算字符串中字符的发生次数, 然后打印一个字符串, 其字符后面是无字符 。
E.G.
输入字符串 :
aaaabb
输出字符串 :
a4b2
输入字符串 :
aaaaabbbc
输出字符串 :
a5b3c1
I am posting my java code.
It is throwing StringOutOfBoundException
/*Write a routine that takes as input a string such as "aabbccdef" and o/p "a2b2c2def" or "a4bd2g4" for "aaaabddgggg".*/
import java.util.Scanner;
public class CountingOccurences {
public static void main(String[] args) {
Scanner inp= new Scanner(System.in);
String str;
char ch;
int count=0;
System.out.println("Enter the string:");
str=inp.nextLine();
while(str.length()>0)
{
ch=str.charAt(0);
int i=0;
while(str.charAt(i)==ch)
{
count =count+i;
i++;
}
str.substring(count);
System.out.println(ch);
System.out.println(count);
}
}
}