C++ Program to Divide the String Into N equal Parts
The program's goal is to split a string into N equal pieces. It can be helpful for several tasks, including dividing a large file into manageable parts, parsing a string into several fields, and producing a new string from a subset of the original string's characters.
Problem Statement:
A C++ program must be written according to the problem description to divide a string into equal halves. The user provides input indicating the number of pieces.
Here is a clearer explanation of the issue:
- The program accepts an integer and a string as input.
- The string's length and character count are both unlimited.
- The string should be divided into the specified number of pieces represented by the integer.
- A vector of strings containing a portion of the original string is the result of the program.
Here is a brief description of the C++ code's operation:
- The length of the original string is first divided by N to determine the size of each component.
- After that, the program loops through the original string from the first character to the last character of the following segment.
- The program replicates the characters from the current place to the end of the loop for each iteration.
To divide a string into N equal pieces using pseudocode in C++:
Input:
A string str and an integer n
Output:
A vector of strings, each of which contains a part of the original string
Calculate the size of each part: part_size = str.length() / n
Initialize a vector of strings to store the parts: parts = []
Iterate over the original string, starting at the beginning and ending at the position of the last character of the next part:
For each iteration:
Copy the characters from the current position to the end of the part to a new string: part = str.substr(i, part_size)
Add the new string to the vector of parts: parts.push_back(part)
Return the vector of parts: return parts
Code:
Let's take a C++ program to divide the string into N equal parts.
#include <iostream>
#include <vector>
using namespace std;
vector<string> split_string(string str, int n) {
vector<string> parts;
int part_size = str.length() / n;
int remainder = str.length() % n;
for (int i = 0; i < n; i++) {
int start = i * part_size;
int end = start + part_size;
if (i < remainder) {
end++; // Increment the end index for the remainder
}
// Ensure that the end index does not exceed the string's length
if (end > str.length()) {
end = str.length();
}
string part = str.substr(start, end - start);
parts.push_back(part);
}
return parts;
}
int main() {
string str = "This is a string.";
int n = 3;
vector<string> parts = split_string(str, n);
for (string part : parts) {
cout << part << endl;
}
return 0;
}
Output:
Another sample input:
str = "Hello, world!"
n = 2
Output:
Dynamic input example:
Let's take a C++ program to divide the string into N equal parts using dynamic input.
#include <iostream>
#include <vector>
using namespace std;
vector<string> split_string(string str, int n) {
vector<string> parts;
int part_size = str.length() / n;
int remainder = str.length() % n;
int start = 0;
for (int i = 0; i < n; i++) {
int end = start + part_size;
if (i < remainder) {
end++; // Increment the end index for the remainder
}
string part = str.substr(start, end - start);
parts.push_back(part);
start = end;
}
return parts;
}
int main() {
string str;
int n;
cout << "Enter a string: ";
getline(cin, str);
cout << "Enter the number of parts to split the string into: ";
cin >> n;
cin.ignore(); // Consume the newline character left in the input buffer
vector<string> parts = split_string(str, n);
for (string part : parts) {
cout << part << endl;
}
return 0;
}
Output:
Conclusion:
This article shows you how to create a C++ program to split a string into equal sections. The following are the primary steps:
- Divide the length of the original string by the required number of pieces to determine each component's dimensions.
- Start at the beginning of the original string and stop at the point of the last character of the next segment as you iterate over it.
- Copy each character from the current location to the part's conclusion into a new string for each iteration.
- After that, a vector of strings will include the new string.
- Return the vector of strings after the program has finished iterating over the starting string.
- This program can be used for many things, such as dividing a big file into smaller pieces, splitting a string into multiple fields, or making a new string out of a subset of the original string's characters.
Key points:
- Regardless of whether the string contains spaces or other special characters, the program should be able to split any length string into N equal pieces.
- Irrespective of whether the string has spaces or other special characters, the program should be able to split any length string into N equal pieces.
- The program should be effective and not consume excessive CPU or memory resources.
- The program should be simple to use and come with detailed instructions.