From baf69125b553814bbd08748d94af2a329fe5ab5b Mon Sep 17 00:00:00 2001 From: Hassam Siddiqui <62291215+iamhassam143@users.noreply.github.com> Date: Tue, 15 Oct 2024 07:59:14 -0700 Subject: [PATCH] UpdatedCharRepeat.java MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I have updated the program to ensure it accurately provides the expected output, as the previous version was not functioning as intended. The revised implementation now produces the correct results. For example: Input: aabbbcc → Output: a2b3c2 Input: hassam → Output: h1a2s2m1 This version handles the counting of consecutive characters efficiently, ensuring the desired format of output. Signed-off-by: Hassam Siddiqui <62291215+iamhassam143@users.noreply.github.com> --- CharRepeat.java | 56 +++++++++++++++++++++---------------------------- 1 file changed, 24 insertions(+), 32 deletions(-) diff --git a/CharRepeat.java b/CharRepeat.java index e758b7f..7663d48 100644 --- a/CharRepeat.java +++ b/CharRepeat.java @@ -1,34 +1,26 @@ -// aabbbcc = a2b3c2 -// abbccc = a1b2c3 +//aabbbcc a2b3c2 +//hassam h1a2s2m1 import java.util.*; -public class CharRepeat { - public static void main(String[]args) - { - Scanner sc= new Scanner(System.in); - String s = sc.nextLine(); - int i=0; - int count = 0; - char ch = s.charAt(i); - for (int k = 0; k < s.length(); k++) - { - if(ch==s.charAt(k)) - { - count++; - } - else{ - System.out.print(ch+""+count); - ch = s.charAt(k); - count =0; - k--; - } - - if(ch == s.charAt(s.length()-1) && k==s.length()-1) - { - System.out.println(ch+""+count); - } - - } - - } -} \ No newline at end of file +public class MyClass { + public static void main(String args[]) { + Scanner sc=new Scanner(System.in); + String str=sc.nextLine(); + char ch[]=str.toCharArray(); + int count; + for(int i=0;i