查詢透過用給定字元替換給定字串的字首而形成的字串
在這個問題中,我們將從給定的字串中形成一個三角形。三角形的行數等於字串長度減1,在每一行中,我們將等於行號的起始字元替換為“.”字元。
我們可以使用迴圈來形成字串的每一行,或者使用字串建構函式和substr()方法。
問題陳述 - 我們給定一個字串 alpha。我們需要以三角形圖案列印該字串。我們需要從 alpha 字串開始三角形,並將前一個字串的第一個字元替換為“.”來顯示它作為三角形。
示例
輸入
alpha = 'abc'
輸出
abc .bc ..c
解釋 - 在第一行,它列印字串。在第二行,它替換第一個字元;在第三行,它替換前兩個字元。
輸入
alpha = 'c'
輸出
c
解釋 - 它列印單個字元的輸出。
輸入
alpha = “tutorials”
輸出
tutorials .utorials ..torials ...orials ....rials .....ials ......als .......ls ........s
方法 1
在這種方法中,我們將遍歷字串,在每次迭代中,我們將用“.”字元替換迴圈索引減1的字元,並將其他字元保持不變。
演算法
步驟 1 - 用字串長度初始化“str_len”。
步驟 2 - 開始遍歷字串。
步驟 3 - 初始化“temp”字串以儲存結果字串。
步驟 4 - 使用巢狀迴圈進行 0 到 p-1 次迭代,以追加總共 p-1 個“.”字元。
步驟 5 - 使用 for 迴圈將字串從第 p 個索引到 len-1 個索引的字元追加到“temp”字串。
步驟 6 - 列印 temp 字串。
示例
以下是上述方法的程式:
#include <stdio.h> #include <string.h> void printTriangle(char* alpha) { int str_len = strlen(alpha); for (int p = 0; p < str_len; p++) { char temp[100]; // Assuming a maximum string length of 100 memset(temp, 0, sizeof(temp)); // Initialize temp with null characters // Append dots to the string for (int q = 0; q < p; q++) temp[q] = '.'; // Append character to the string. for (int q = p; q < str_len; q++) temp[q] = alpha[q]; // Print the string printf("%s\n", temp); } } int main() { char alpha[] = "tutorialspoint"; printTriangle(alpha); return 0; }
輸出
tutorialspoint .utorialspoint ..torialspoint ...orialspoint ....rialspoint .....ialspoint ......alspoint .......lspoint ........spoint .........point ..........oint ...........int ............nt .............t
#include <bits/stdc++.h> using namespace std; void printTriangle(string alpha) { // str_len variable to calculate the length of the string. int str_len = alpha.length(); for (int p = 0; p < str_len; p++) { string temp = ""; // Append dots to the string for (int q = 0; q < p; q++) temp += "."; // Append character to the string. for (int q = p; q < str_len; q++) temp += alpha[q]; // Print the string cout << temp << "\n"; } } int main(){ string alpha = "tutorialspoint"; printTriangle(alpha); return 0; }
輸出
tutorialspoint .utorialspoint ..torialspoint ...orialspoint ....rialspoint .....ialspoint ......alspoint .......lspoint ........spoint .........point ..........oint ...........int ............nt .............t
public class Main { public static void printTriangle(String alpha) { int str_len = alpha.length(); for (int p = 0; p < str_len; p++) { StringBuilder temp = new StringBuilder(); // Append dots to the string for (int q = 0; q < p; q++) { temp.append("."); } // Append character to the string. for (int q = p; q < str_len; q++) { temp.append(alpha.charAt(q)); } // Print the string System.out.println(temp.toString()); } } public static void main(String[] args) { String alpha = "tutorialspoint"; printTriangle(alpha); } }
輸出
tutorialspoint .utorialspoint ..torialspoint ...orialspoint ....rialspoint .....ialspoint ......alspoint .......lspoint ........spoint .........point ..........oint ...........int ............nt .............t
def print_triangle(alpha): str_len = len(alpha) for p in range(str_len): temp = "" # Append dots to the string for q in range(p): temp += "." # Append character to the string. for q in range(p, str_len): temp += alpha[q] # Print the string print(temp) if __name__ == "__main__": alpha = "tutorialspoint" print_triangle(alpha)
輸出
tutorialspoint .utorialspoint ..torialspoint ...orialspoint ....rialspoint .....ialspoint ......alspoint .......lspoint ........spoint .........point ..........oint ...........int ............nt .............t
時間複雜度 - O(N2),因為使用了巢狀迴圈。
空間複雜度 - O(N),用於在 temp 字串中儲存結果。
方法 2
在這種方法中,我們將使用 String() 建構函式建立一個包含 p 個“.”字元的字串。之後,我們將使用 substr() 方法獲取字串中剩餘的最後一個字元。
演算法
步驟 1 - 使用迴圈開始遍歷字串。
步驟 2 - 使用 String() 建構函式建立一個包含 p 個“.”字元的 temp 字串。
步驟 3 - 獲取從第 p 個索引開始,長度等於 str_len - p 的子字串,並將其追加到 temp 字串。
步驟 4 - 列印 temp 字串。
示例
以下是解決上述演算法的程式。
#include <stdio.h> #include <string.h> void printTriangle(char* alpha) { // str_len variable to calculate the length of the string. int str_len = strlen(alpha); for (int p = 0; p < str_len; p++) { char temp[str_len + 1]; // +1 for null terminator memset(temp, '.', p); // Append substring starting from index p to len strcpy(temp + p, alpha + p); temp[str_len] = '\0'; printf("%s\n", temp); } } int main() { char alpha[] = "tutorials"; printTriangle(alpha); return 0; }
輸出
tutorials .utorials ..torials ...orials ....rials .....ials ......als .......ls ........s
#include <bits/stdc++.h> using namespace std; void printTriangle(string alpha) { // str_len variable to calculate the length of the string. int str_len = alpha.length(); for (int p = 0; p < str_len; p++) { string temp(p, '.'); // Append substring starting from index p to len temp += alpha.substr(p, str_len - p); // Print the string cout << temp << "\n"; } } int main() { string alpha = "tutorials"; printTriangle(alpha); return 0; }
輸出
tutorials .utorials ..torials ...orials ....rials .....ials ......als .......ls ........s
public class Main { public static void printTriangle(String alpha) { int str_len = alpha.length(); for (int p = 0; p < str_len; p++) { StringBuilder temp = new StringBuilder(); for (int i = 0; i < p; i++) { temp.append('.'); // Append dots (.) to temp. } temp.append(alpha.substring(p)); // Append the remaining characters from the input string to temp. System.out.println(temp.toString()); } } public static void main(String[] args) { String alpha = "tutorials"; printTriangle(alpha); // Call the function to print the triangle. } }
輸出
tutorials .utorials ..torials ...orials ....rials .....ials ......als .......ls ........s
def print_triangle(alpha): str_len = len(alpha) # Calculate the length of the input string. for p in range(str_len): temp = '.' * p + alpha[p:] # Create the output string with dots and the remaining characters. print(temp) if __name__ == "__main__": alpha = "tutorials" print_triangle(alpha) # Call the function to print the triangle.
輸出
tutorials .utorials ..torials ...orials ....rials .....ials ......als .......ls ........s
時間複雜度 - O(N2),用於遍歷字串並獲取子字串。
空間複雜度 - O(N),用於儲存臨時字串。
我們學習瞭如何使用給定的字串列印三角形圖案。程式設計師可以嘗試使用 while 迴圈來列印三角形圖案,就像我們在本教程中使用 for 迴圈一樣。程式設計師可以使用帶有 String() 建構函式的 for 迴圈。