# Printing frequency of each character just after its consecutive occurrences

• Difficulty Level : Basic
• Last Updated : 18 May, 2021

Given a string in such a way that every character occurs in a repeated manner. Your task is to print the string by inserting the frequency of each unique character after it and also eliminating all repeated characters.
Examples:

```Input : GeeeEEKKKss
Output : G1e3E2K3s2

Input : ccccOddEEE
Output : c4O1d2E3```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

One approach to solve the above problem is to start a loop till the end of the string and for every iteration, increment a count till the character at ith position matches the following character.
Below is the implementation to the above given problem.

## C++

 `// CPP program to print run``// length encoding of a string``#include ``using` `namespace` `std;` `void` `printRLE(string s)``{``    ``for` `(``int` `i = 0; s[i] != ``'\0'``; i++) {` `        ``// Counting occurrences of s[i]``        ``int` `count = 1;``        ``while` `(s[i] == s[i + 1]) {``            ``i++;``            ``count++;``        ``}``        ``cout << s[i] << count << ``" "``;``    ``}` `    ``cout << endl;``}` `// Driver code``int` `main()``{``    ``printRLE(``"GeeeEEKKKss"``);``    ``printRLE(``"ccccOddEEE"``);``    ``return` `0;``}`

## Java

 `// Java program to print run``// length encoding of a string``class` `GFG {` `    ``static` `void` `printRLE(String s)``    ``{``        ``for` `(``int` `i = ``0``; i < s.length(); i++) {` `            ``// Counting occurrences of s[i]``            ``int` `count = ``1``;``            ``while` `(i + ``1` `< s.length()``                   ``&& s.charAt(i)``                          ``== s.charAt(i + ``1``)) {``                ``i++;``                ``count++;``            ``}``            ``System.out.print(s.charAt(i)``                             ``+ ``""` `+ count + ``" "``);``        ``}` `        ``System.out.println();``    ``}` `    ``// Driver code``    ``public` `static` `void` `main(String args[])``    ``{``        ``printRLE(``"GeeeEEKKKss"``);``        ``printRLE(``"ccccOddEEE"``);``    ``}``}` `// This code is contributed``// by Arnab Kundu`

## Python 3

 `# Python 3 program to print run``# length encoding of a string` `def` `printRLE(s) :` `    ``i ``=` `0``    ``while``( i < ``len``(s) ``-` `1``) :` `        ``# Counting occurrences of s[i]``        ``count ``=` `1` `        ``while` `s[i] ``=``=` `s[i ``+` `1``] :` `            ``i ``+``=` `1``            ``count ``+``=` `1``            ` `            ``if` `i ``+` `1` `=``=` `len``(s):``                ``break``        ` `        ``print``(``str``(s[i]) ``+` `str``(count),  ``                          ``end ``=` `" "``)``        ``i ``+``=` `1``    ` `    ``print``()` `# Driver Code``if` `__name__ ``=``=` `"__main__"` `:` `    ``# function calling``    ``printRLE(``"GeeeEEKKKss"``)``    ``printRLE(``"cccc0ddEEE"``)` `# This code is contributed by ANKITRAI1`

## C#

 `// C# program to print run``// length encoding of a string``using` `System;` `class` `GFG {` `    ``static` `void` `printRLE(String s)``    ``{``        ``for` `(``int` `i = 0;``             ``i < s.Length - 1; i++) {` `            ``// Counting occurrences of s[i]``            ``int` `count = 1;``            ``while` `(s[i] == s[i + 1]) {``                ``i++;``                ``count++;``                ``if` `(i + 1 == s.Length)``                    ``break``;``            ``}``            ``Console.Write(s[i] + ``""` `+ count + ``" "``);``        ``}` `        ``Console.WriteLine();``    ``}` `    ``// Driver code``    ``public` `static` `void` `Main(String[] args)``    ``{``        ``printRLE(``"GeeeEEKKKss"``);``        ``printRLE(``"ccccOddEEE"``);``    ``}``}` `// This code contributed by Rajput-Ji`

## Javascript

 ``
Output:
```G1 e3 E2 K3 s2
c4 O1 d2 E3 ```

My Personal Notes arrow_drop_up