查找通过用给定字符替换给定字符串的前缀而形成的字符串


在这个问题中,我们将从给定的字符串中形成一个三角形。三角形的行数等于字符串长度减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 循环。

更新于:2023年10月20日

96 次浏览

启动您的职业生涯

通过完成课程获得认证

开始
广告
© . All rights reserved.