反转字符串模式程序
给定字符串str作为输入。任务是打印示例中所示的模式。
例子:
Input : str = “geeks”
Output :
g
e e
* s k
Explanation :
In the first line, print the first character in the string.
In the second line, print the next two characters in reverse order.
In the third line there are not enough characters to print, so append *s and print the characters in reverse order.
Input: str = “orange”
Output:
o
a r
e g n
方法:
- 用值1初始化一个空列表和一个变量x 。
- 遍历输入字符串str并将字符附加到列表中。
- 如果列表的长度等于x的值,则
- 以相反的顺序打印列表中的字符。
- 将 x 的值增加 1。
- 清空列表
- 如果最后一个打印字符的长度不为0且小于x的值,则附加x-length(x) *s并以相反的顺序打印。
C++
#include
using namespace std;
// method declared to print the pattern of
// the string passed as argument
void reverseString(string str)
{
// a temporary string to store the
// value of each row of pattern
string r = "";
int x = 1;
for (int i = 0 ; i < str.length(); i++)
{
// extracting each character and
// adding it to the string r
r = r + str[i];
if (r.length() == x)
{
// loop to print the string r in reverse order
for(int j = r.length() - 1; j >= 0; j--)
cout<= 0; j--)
cout << r.at(j) << " ";
}
}
// Driver Code
int main()
{
// sample string to check the code
string str = "geeks";
// method calling
reverseString(str);
}
// This code is contributed by Rajput-Ji
Java
class GFG
{
// method declared to print the pattern of
// the string passed as argument
public static void reverseString(String str)
{
// a temporary string to store the
// value of each row of pattern
String r = "";
int x = 1;
for (int i = 0 ; i < str.length(); i++)
{
// extracting each character and
// adding it to the string r
r = r + str.charAt(i);
if (r.length() == x)
{
// loop to print the string r in reverse order
for(int j = r.length() - 1; j >= 0; j--)
System.out.print(r.charAt(j) + " ");
x += 1;
r = "";
System.out.println();
}
}
// condition checking to add the "*" if required
if (r.length() < x && r.length() != 0)
{
// adding the number of "*" required in r
for(int k = 1; k <= x - r.length(); k++)
r = r + "*";
// printing r in reverse order
for(int j = r.length() - 1; j >= 0; j--)
System.out.print(r.charAt(j) + " ");
}
}
// Driver Code
public static void main(String args[])
{
// sample string to check the code
String str = "geeks";
// method calling
reverseString(str);
}
}
// This code is contributed by Animesh_Gupta
Python 3
# function to print the pattern
def reverseString(str):
# initialize an empty list
r = []
# initialize the value of x as 1
x = 1
# traverse through all the characters of x
for i in str:
# append all the characters to the list
r.append(i)
# if the length of the list is x
if len(r)== x:
# print the list in reverse order
print(*reversed(r))
# increment the value of x
x+= 1
# empty the list
r = []
# if the list is not empty
# length of the list is less than x
if len(r)
C#
using System;
class GFG
{
// method declared to print the pattern of
// the string passed as argument
public static void reverseString(String str)
{
// a temporary string to store the
// value of each row of pattern
String r = "";
int x = 1;
for (int i = 0 ; i < str.Length; i++)
{
// extracting each character and
// adding it to the string r
r = r + str[i];
if (r.Length == x)
{
// loop to print the string r in reverse order
for(int j = r.Length - 1; j >= 0; j--)
Console.Write(r[j] + " ");
x += 1;
r = "";
Console.WriteLine();
}
}
// condition checking to add the "*" if required
if (r.Length < x && r.Length != 0)
{
// adding the number of "*" required in r
for(int k = 1; k <= x - r.Length; k++)
r = r + "*";
// printing r in reverse order
for(int j = r.Length - 1; j >= 0; j--)
Console.Write(r[j] + " ");
}
}
// Driver Code
public static void Main(String []args)
{
// sample string to check the code
String str = "geeks";
// method calling
reverseString(str);
}
}
// This code is contributed by PrinciRaj1992
Javascript
输出:
g
e e
* s k