# Split given String into substrings of size K by filling elements

• Last Updated : 27 Jan, 2022

Given a string str of length N and an integer K, the task is to split the string into K sized groups and if the last group does not have K characters remaining, then a character ch is used to complete the group.

Examples:

Input: str = “Algorithms”, K = 3, ch = “@”
Output: Alg ori thm s@@
Explanation:
The first 3 characters “alg” form the first group.
The next 3 characters “ori” form the second group.
The last 3 characters “thm” form the third group.
For the last group, there is only the character ‘s’ from the string.
To complete this group, add ‘@’ twice.

Input: str = “Algorithm”, K = 3, ch = “@”
Output: Alg ori thm
Explanation:
Similar to the previous example,
The first 3 characters “alg” form the first group.
The next 3 characters “ori” form the second group.
The last 3 characters “thm” form the third group.
Since all groups can be completely filled by characters from the string, no need to use ch.

Approach: This is a simple implementation related problem. Follow the steps mentioned below:

1. Initialise res as an empty string.
2. Start traversing the string and when the size of the res string equals K, then place a res string into the result vector and empty the res string again for further division
3. And at last, if the res string is not empty and size is not equal to k a, use the Extra character to fill it, the last group.

Below is the implementation of the above approach.

## C++

 `// C++ code to implement above approach` `#include ` `using` `namespace` `std;`   `// Function to split the string` `vector dividestring(string str,` `                            ``int` `K, ``char` `ch)` `{` `    ``int` `N = str.size();` `    ``int` `j = 0, i = 0;` `    ``vector result;` `    ``string res = ``""``;` `    ``while` `(j < N) {` `        ``res += str[j];` `        ``if` `(res.size() == K) {` `            ``result.push_back(res);` `            ``res = ``""``;` `        ``}` `        ``j++;` `    ``}`   `    ``if` `(res != ``""``) {` `        ``while` `(res.size() < K) {` `            ``res += ch;` `        ``}` `        ``result.push_back(res);` `    ``}` `    ``return` `result;` `}`   `// Driver code` `int` `main()` `{`   `    ``string str = ``"Algorithms"``;` `    ``int` `K = 3;` `    ``char` `ch = ``'@'``;` `    ``vector ans` `        ``= dividestring(str, K, ch);` `    ``for` `(``auto` `i : ans) {` `        ``cout << i << ``"\n"``;` `    ``}` `    ``return` `0;` `}`

## Java

 `// Java code to implement above approach` `import` `java.util.ArrayList;`   `class` `GFG ` `{`   `  ``// Function to split the String` `  ``static` `ArrayList divideString(String str, ``int` `K, ``char` `ch) {` `    ``int` `N = str.length();` `    ``int` `j = ``0``;` `    ``ArrayList result = ``new` `ArrayList();` `    ``String res = ``""``;` `    ``while` `(j < N) {` `      ``res += str.charAt(j);` `      ``if` `(res.length() == K) {` `        ``result.add(res);` `        ``res = ``""``;` `      ``}` `      ``j++;` `    ``}`   `    ``if` `(res != ``""``) {` `      ``while` `(res.length() < K) {` `        ``res += ch;` `      ``}` `      ``result.add(res);` `    ``}` `    ``return` `result;` `  ``}`   `  ``// Driver code` `  ``public` `static` `void` `main(String args[])` `  ``{`   `    ``String str = ``"Algorithms"``;` `    ``int` `K = ``3``;` `    ``char` `ch = ``'@'``;` `    ``ArrayList ans = divideString(str, K, ch);` `    ``for` `(String i : ans) {` `      ``System.out.println(i);` `    ``}` `  ``}` `}`   `// This code is contributed by gfgking.`

## Python3

 `# python3 code to implement above approach`   `# Function to split the string`     `def` `dividestring(``str``, K, ch):`   `    ``N ``=` `len``(``str``)` `    ``j, i ``=` `0``, ``0` `    ``result ``=` `[]` `    ``res ``=` `""` `    ``while` `(j < N):` `        ``res ``+``=` `str``[j]` `        ``if` `(``len``(res) ``=``=` `K):` `            ``result.append(res)` `            ``res ``=` `""`   `        ``j ``+``=` `1`   `    ``if` `(res !``=` `""):` `        ``while` `(``len``(res) < K):` `            ``res ``+``=` `ch`   `        ``result.append(res)`   `    ``return` `result`     `# Driver code` `if` `__name__ ``=``=` `"__main__"``:`   `    ``str` `=` `"Algorithms"` `    ``K ``=` `3` `    ``ch ``=` `'@'` `    ``ans ``=` `dividestring(``str``, K, ch)` `    ``for` `i ``in` `ans:` `        ``print``(i)`   `    ``# This code is contributed by rakeshsahni`

## C#

 `// C# code to implement above approach` `using` `System;` `using` `System.Collections.Generic;` `class` `GFG {`   `  ``// Function to split the string` `  ``static` `List<``string``> dividestring(``string` `str, ``int` `K,` `                                   ``char` `ch)` `  ``{` `    ``int` `N = str.Length;` `    ``int` `j = 0;` `    ``List<``string``> result = ``new` `List<``string``>();` `    ``string` `res = ``""``;` `    ``while` `(j < N) {` `      ``res += str[j];` `      ``if` `(res.Length == K) {` `        ``result.Add(res);` `        ``res = ``""``;` `      ``}` `      ``j++;` `    ``}`   `    ``if` `(res != ``""``) {` `      ``while` `(res.Length < K) {` `        ``res += ch;` `      ``}` `      ``result.Add(res);` `    ``}` `    ``return` `result;` `  ``}`   `  ``// Driver code` `  ``public` `static` `void` `Main()` `  ``{`   `    ``string` `str = ``"Algorithms"``;` `    ``int` `K = 3;` `    ``char` `ch = ``'@'``;` `    ``List<``string``> ans = ``new` `List<``string``>();` `    ``ans = dividestring(str, K, ch);` `    ``foreach``(``var` `i ``in` `ans) { Console.WriteLine(i); }` `  ``}` `}`   `// This code is contributed by Taranpreet`

## Javascript

 ``

Output:

```Alg
ori
thm
s@@```

Time Complexity: O(N)
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up
Recommended Articles
Page :