Java Program to print number of Occurrence of characters in the String. Example if string is aabbccdddaa Output shown will be a2b2c2d3a3.

import java.util.*;
public class OccuranceString {
 
 public static void main(String args[])
 {
  String s,s1="";
  int i,j,temp=0,count=0,len;
  Scanner in=new Scanner(System.in);
  System.out.print("Enter the String: ");
  s=in.next();
  len=s.length();
  
  while(temp!=len)
  {
   count=0;
   for(j=temp;j<len;j++)
   {
    if(s.charAt(temp)==s.charAt(j))
    {
     count++;
    }
    else
    {
     break;
    }
   }
   s1=s1+s.charAt(temp)+""+count+" ";
   temp=j;
  }
  
  System.out.println(s1);
 
  
  
  
 }
Java Program to print number of Occurrence of characters in the String. Example if string is aabbccdddaa Output shown will be a2b2c2d3a3. Java Program to print number of Occurrence of characters in the String. Example if string is aabbccdddaa Output shown will be a2b2c2d3a3. Reviewed by on December 20, 2019 Rating: 5

No comments:

Powered by Blogger.