将一个字符串分成N等份
难度级别:新手
题:
编写一个程序来打印给定字符串的 N 个相等部分。
解决方案:
1) 使用字符串函数strlen() 获取字符串的大小(存在于字符串.h 中)
2)获取零件的尺寸。
part_size = string_length/n
3) 循环输入字符串。在循环中,如果 index 变为 part_size 的倍数,则放置一个部分分隔符(“\n”)
执行:
C++
// C++ program to divide a string
// in n equal parts
#include
#include
using namespace std;
class gfg {
// Function to print n equal parts of str
public:
void divideString(char str[], int n)
{
int str_size = strlen(str);
int i;
int part_size;
// Check if string can be divided in
// n equal parts
if (str_size % n != 0) {
cout << "Invalid Input: String size";
cout << " is not divisible by n";
return;
}
// Calculate the size of parts to
// find the division points
part_size = str_size / n;
for (i = 0; i < str_size; i++) {
if (i % part_size == 0)
cout << endl;
cout << str[i];
}
}
};
// Driver code
int main()
{
gfg g;
// length od string is 28
char str[] = "a_simple_divide_string_quest";
// Print 4 equal parts of the string
g.divideString(str, 4);
return 0;
}
// This code is contributed by SoM15242
C
// C program to divide a string
// in n equal parts
#include
#include
// Function to print n equal parts of str
void divideString(char* str, int n)
{
int str_size = strlen(str);
int i;
int part_size;
// Check if string can be divided in
// n equal parts
if (str_size % n != 0) {
printf("Invalid Input: String size");
printf(" is not divisible by n");
return;
}
// Calculate the size of parts to
// find the division points
part_size = str_size / n;
for (i = 0; i < str_size; i++) {
if (i % part_size == 0)
printf("\n");
printf("%c", str[i]);
}
}
int main()
{
// length od string is 28
char* str = "a_simple_divide_string_quest";
// Print 4 equal parts of the string
divideString(str, 4);
getchar();
return 0;
}
Java
// Java program to divide a string
// in n equal parts
class GFG {
// Method to print n equal parts of str
static void divideString(String str, int n)
{
int str_size = str.length();
int part_size;
// Check if string can be divided in
// n equal parts
if (str_size % n != 0) {
System.out.println("Invalid Input: String size"
+ "is not divisible by n");
return;
}
// Calculate the size of parts to find
// the division points
part_size = str_size / n;
for (int i = 0; i < str_size; i++) {
if (i % part_size == 0)
System.out.println();
System.out.print(str.charAt(i));
}
}
// Driver Code
public static void main(String[] args)
{
// length od string is 28
String str = "a_simple_divide_string_quest";
// Print 4 equal parts of the string
divideString(str, 4);
}
}
Python3
# Python program to print n equal parts of string
# Function to print n equal parts of string
def divideString(string, n):
str_size = len(string)
# Check if string can be divided in n equal parts
if str_size % n != 0:
print ("Invalid Input: String size is not divisible by n")
return
# Calculate the size of parts to find the division points
part_size = str_size/n
k = 0
for i in string:
if k % part_size == 0:
print ()
print (i,end='')
k += 1
# Driver program to test the above function
# Length of string is 28
string = "a_simple_divide_string_quest"
# Print 4 equal parts of the string
divideString(string, 4)
# This code is contributed by Bhavya Jain
C#
// C# program to divide a string
// in n equal parts
using System;
class GFG {
// Method to print n
// equal parts of str
static void divideString(String str, int n)
{
int str_size = str.Length;
int part_size;
// Check if string
// can be divided in
// n equal parts
if (str_size % n != 0) {
Console.Write("Invalid Input: String size"
+ "is not divisible by n");
return;
}
// Calculate the size
// of parts to find
// the division points
part_size = str_size / n;
for (int i = 0; i < str_size; i++) {
if (i % part_size == 0)
Console.WriteLine();
Console.Write(str[i]);
}
}
// Driver Code
static void Main()
{
// length od string is 28
String str = "a_simple_divide_string_quest";
// Print 4 equal parts of the string
divideString(str, 4);
}
}
// This code is contributed by Anuj_67
PHP
Javascript
C++
#include
using namespace std;
void divide(string str, int n)
{
if (str.length() % n != 0) {
cout << "Invalid Input: String size";
cout << " is not divisible by n";
return;
}
int parts = str.length() / n;
int start = 0;
while (start < str.length()) {
cout << str.substr(start, parts) << endl;
start += parts;
// if(start < str.length()) cout << endl; to ignore
// final new line
}
}
int main()
{
string str = "a_simple_divide_string_quest";
divide(str, 4);
}
//Contributed By Jagadeesh
Java
import java.util.*;
class GFG {
static void divide(String str, int n) {
if (str.length() % n != 0) {
System.out.print("Invalid Input: String size");
System.out.print(" is not divisible by n");
return;
}
int parts = str.length() / n;
int start = 0;
int t = parts;
while (start < str.length()) {
String temp = new String(str);
System.out.print(temp.substring(start, parts) + "\n");
start = parts;
parts += t;
// if(start < str.length()) System.out.println(); to ignore
// final new line
}
}
// Driver code
public static void main(String[] args) {
String str = "a_simple_divide_String_quest";
divide(str, 4);
}
}
// This code is contributed by umadevi9616
输出
a_simpl
e_divid
e_strin
g_quest
在上述解决方案中,仅打印字符串的 n 个相等部分。如果我们想要存储单个部分,我们需要为所有 N 个部分分配 part_size + 1 内存(字符串终止字符'\0' 额外分配 1 个)并将这些部分的地址存储在字符指针数组中。
另一种方法:在 C++ 中使用 STL
C++
#include
using namespace std;
void divide(string str, int n)
{
if (str.length() % n != 0) {
cout << "Invalid Input: String size";
cout << " is not divisible by n";
return;
}
int parts = str.length() / n;
int start = 0;
while (start < str.length()) {
cout << str.substr(start, parts) << endl;
start += parts;
// if(start < str.length()) cout << endl; to ignore
// final new line
}
}
int main()
{
string str = "a_simple_divide_string_quest";
divide(str, 4);
}
//Contributed By Jagadeesh
Java
import java.util.*;
class GFG {
static void divide(String str, int n) {
if (str.length() % n != 0) {
System.out.print("Invalid Input: String size");
System.out.print(" is not divisible by n");
return;
}
int parts = str.length() / n;
int start = 0;
int t = parts;
while (start < str.length()) {
String temp = new String(str);
System.out.print(temp.substring(start, parts) + "\n");
start = parts;
parts += t;
// if(start < str.length()) System.out.println(); to ignore
// final new line
}
}
// Driver code
public static void main(String[] args) {
String str = "a_simple_divide_String_quest";
divide(str, 4);
}
}
// This code is contributed by umadevi9616
输出
a_simpl
e_divid
e_strin
g_quest
如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。