ในบทช่วยสอนนี้ เราจะพูดถึงโปรแกรมเพื่อแปลงสตริงเป็นตารางอักขระตารางเมทริกซ์
สำหรับสิ่งนี้เราจะได้รับสตริงของอักขระ งานของเราคือพิมพ์สตริงนั้นในรูปแบบของตารางเมทริกซ์ที่มีจำนวนแถวและคอลัมน์ที่แน่นอน
ตัวอย่าง
#include <bits/stdc++.h>
using namespace std;
//converting the string in grid format
void convert_grid(string str){
int l = str.length();
int k = 0, row, column;
row = floor(sqrt(l));
column = ceil(sqrt(l));
if (row * column < l)
row = column;
char s[row][column];
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
s[i][j] = str[k];
k++;
}
}
//printing the new grid
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
if (s[i][j] == '\0')
break;
cout << s[i][j];
}
cout << endl;
}
}
int main(){
string str = "TUTORIALSPOINT";
convert_grid(str);
return 0;
} ผลลัพธ์
TUTO RIAL SPOI NT