Java Program to print the count of Consecutive character- Interview Question Asked in Mobikwik company
The provided Java program counts consecutive occurrences of each character in a given string and prints the count for each sequence of consecutive characters. Let’s go through the code step-by-step to understand how it works.
Program Title: "Java Program to Count Consecutive Characters in a String"
Code Explanation
Detailed Step-by-Step Explanation:
1. Initialization:
- The program starts by defining a string `name` containing the input `"DDeee eepppaapekkrkka"`.
- The string is converted to a character array `ch` using `name.toCharArray()` for easier manipulation.
- `len` stores the length of the character array.
- A `StringBuilder sb` is initialized to keep track of consecutive characters as they are identified.
2. Iterate Over Characters:
- The main loop iterates over each character in the array `ch` using the index `j` from `0` to `len - 2` (the second last character). 3. Checking for Consecutive Characters:
- The loop checks if the current character `ch[j]` is the same as the next character `ch[j + 1]`.
- If they are the same, it appends the character `ch[j]` to the `StringBuilder sb` to keep track of consecutive occurrences.
4. Handling Non-Consecutive Characters:
- If the characters `ch[j]` and `ch[j + 1]` are different, the program checks if `sb` has any accumulated characters:
- If `sb` is not empty (`sb.length() > 0`), it means there was a sequence of consecutive characters. It prints the last character in the sequence (`ch[j]`) and its count (`sb.length() + 1`), then clears the `StringBuilder` using `sb.setLength(0)` for the next sequence.
- If `sb` is empty, it means no consecutive characters were found, so it prints the current character (`ch[j]`) with a count of `1`.
5. Handling the Last Character(s):
- After the loop completes, there might be a sequence of consecutive characters that needs to be printed, so the program checks `sb` once more:
- If `sb` is not empty, it prints the last character and the count of its occurrences.
- If `sb` is empty, it prints the last character (`ch[i]`) with a count of `1`.
Example Execution
Given the input string `"DDeee eepppaapekkrkka"`, the program processes each character as follows:
- Characters `D` and `e`:
- `"DD"`: Two consecutive 'D's → `D -->> 2`
- `"eee"`: Three consecutive 'e's → `e -->> 3`
- Spaces:
- `" "`: Two consecutive spaces → ` -->> 2`
- Character `e`:
- `"ee"`: Two consecutive 'e's → `e -->> 2`
- Characters `p` and `a`:
- `"ppp"`: Three consecutive 'p's → `p -->> 3`
- `"aa"`: Two consecutive 'a's → `a -->> 2`
- Character `p`:
- `"p"`: Single 'p' → `p -->> 1`
- Characters `k` and `r`:
- `"kk"`: Two consecutive 'k's → `k -->> 2`
- `"rr"`: Two consecutive 'r's → `r -->> 2`
- Character `k`:
- `"kk"`: Two consecutive 'k's → `k -->> 2`
- `"a"`: Single 'a' → `a -->> 1`
Final Output:
Key Points:
- The program successfully counts and prints consecutive characters in a given string.
- The use of a `StringBuilder` allows efficient string manipulation and space management.
- The logic covers edge cases such as handling consecutive spaces and the last character sequence.
Program Title: "Java Program to Count Consecutive Characters in a String"
Code Explanation
Detailed Step-by-Step Explanation:
1. Initialization:
- The program starts by defining a string `name` containing the input `"DDeee eepppaapekkrkka"`.
- The string is converted to a character array `ch` using `name.toCharArray()` for easier manipulation.
- `len` stores the length of the character array.
- A `StringBuilder sb` is initialized to keep track of consecutive characters as they are identified.
2. Iterate Over Characters:
- The main loop iterates over each character in the array `ch` using the index `j` from `0` to `len - 2` (the second last character). 3. Checking for Consecutive Characters:
- The loop checks if the current character `ch[j]` is the same as the next character `ch[j + 1]`.
- If they are the same, it appends the character `ch[j]` to the `StringBuilder sb` to keep track of consecutive occurrences.
4. Handling Non-Consecutive Characters:
- If the characters `ch[j]` and `ch[j + 1]` are different, the program checks if `sb` has any accumulated characters:
- If `sb` is not empty (`sb.length() > 0`), it means there was a sequence of consecutive characters. It prints the last character in the sequence (`ch[j]`) and its count (`sb.length() + 1`), then clears the `StringBuilder` using `sb.setLength(0)` for the next sequence.
- If `sb` is empty, it means no consecutive characters were found, so it prints the current character (`ch[j]`) with a count of `1`.
5. Handling the Last Character(s):
- After the loop completes, there might be a sequence of consecutive characters that needs to be printed, so the program checks `sb` once more:
- If `sb` is not empty, it prints the last character and the count of its occurrences.
- If `sb` is empty, it prints the last character (`ch[i]`) with a count of `1`.
Example Execution
Given the input string `"DDeee eepppaapekkrkka"`, the program processes each character as follows:
- Characters `D` and `e`:
- `"DD"`: Two consecutive 'D's → `D -->> 2`
- `"eee"`: Three consecutive 'e's → `e -->> 3`
- Spaces:
- `" "`: Two consecutive spaces → ` -->> 2`
- Character `e`:
- `"ee"`: Two consecutive 'e's → `e -->> 2`
- Characters `p` and `a`:
- `"ppp"`: Three consecutive 'p's → `p -->> 3`
- `"aa"`: Two consecutive 'a's → `a -->> 2`
- Character `p`:
- `"p"`: Single 'p' → `p -->> 1`
- Characters `k` and `r`:
- `"kk"`: Two consecutive 'k's → `k -->> 2`
- `"rr"`: Two consecutive 'r's → `r -->> 2`
- Character `k`:
- `"kk"`: Two consecutive 'k's → `k -->> 2`
- `"a"`: Single 'a' → `a -->> 1`
Final Output:
Key Points:
- The program successfully counts and prints consecutive characters in a given string.
- The use of a `StringBuilder` allows efficient string manipulation and space management.
- The logic covers edge cases such as handling consecutive spaces and the last character sequence.
Comments
Post a Comment