Javatpoint Logo
Javatpoint Logo

C++ program to divide a string into N equal parts

In this article, you will learn about how divide a string into N equal parts in C++. String manipulation and fundamental arithmetic are used in the C++ program to split a string into N equal pieces.

1. Input:

Two user inputs are required at the beginning of the program:

  • A user-supplied string (input) that they desire to split into equal pieces.
  • The user can divide a string into as many equal pieces (N) as desired by providing an integer (n) to symbolize this.

2. Length Calculation

Measurement of length using the length() function of the string class, the program measures the length of the supplied string. The length is kept in the variable len. You must first know its length to divide a string into equal pieces.

3. Check for errors:

The string's length must be divisible by n to split it into equal pieces. Len% n is tested to see if it equals zero by the program. If it's not, the program will exit after displaying an error notice and indicating that the string cannot be split into equal parts.

4. Calculation of Part Size:

If the length of the string is divisible by n, the program determines how big each part should be. To achieve this, multiply the string's length by n, which creates the variable partSize. Every component must be this size.

5. Loop for Division:

The n is the number of components that the user requested, and the loop iterates n times. Every time the loop is executed:

  • The program using the substr function of the string class to extracts a section of the input string. The substr function requires two inputs: the beginning position, which is updated after each iteration, and the length of the substring, which is partSize.
  • After that, the extracted part is included in a vector called "parts" that will hold all of the parts.

6. Printing:

The program prints each portion of the string on a separate line after successfully splitting it into equal parts. After that, it shows a message indicating that the string has been divided.

7. Conclusion:

The program finally returns 0 to show that it ran successfully.

Program:

Let's take an example to demonstrate how to divide a string into N strings in C++:

Output:

Enter a string: Ravan was killed by Rama
Enter the number of parts (N): 3
The string is divided into 3 equal parts:
Ravan wa
s killed
 by Rama

Explanation:

  1. The divideString function
    • Determines the input string's length (len).
    • Ensures len% n == 0 to determine whether it is possible to divide the string into equal sections.
    • Returns an empty vector (parts) and shows an error message if it is not feasible.
    • Determines the partSize of each component.
    • Extracts each component of the string using a loop and substr.
    • Each part is added to the parts vector.
    • Returns a vector of parts that includes the separated sections.
  2. main Function
    • Use std::getline to read the complete input line.
    • Obtains from the user the number of pieces (n).
    • Divide the string into pieces by using the divideString function.
    • Upon success, distinct lines are printed for the total number of equal parts and each portion.
  3. return 0
    • Returning 0 indicates that the program has run successfully.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA