# 以X格式打印奇数长度的字符串

2021年3月13日17:00:37 发表评论 755 次浏览

## 本文概述

``````Input: 12345
Output:
1       5
2   4
3
2   4
1       5

Input: lsbin
Output:
g                         s
e                     k
e                 e
k             e
s         g
f      r
o
f     r
s         g
k             e
e                 e
e                      k
g                          s``````

## C

``````// C++ program to print Cross pattern
#include<iostream>
using namespace std;

// Function to print given string in cross pattern
// Length of string must be odd
void printPattern(string str)
{
int len = str.length();

// i goes from 0 to len and j goes from len-1 to 0
for ( int i=0, j=len-1; i<=len, j>=0; i++, j--)
{
// To print the upper part. This loop runs
// til middle point of string (i and j become
// same
if (i<j)
{
// Print i spaces
for ( int x=0; x<i; x++)
cout << " " ;

// Print i'th character
cout << str[i];

// Print j-i-1 spaces
for ( int x=0; x<j-i-1; x++)
cout << " " ;

// Print j'th character
cout << str[j] << endl;
}

// To print center point
if (i==j)
{
// Print i spaces
for ( int x=0; x<i; x++)
cout << " " ;

// Print middle character
cout << str[i] << endl;
}

// To print lower part
else if (i>j)
{
// Print j spances
for ( int x = j-1; x>=0; x--)
cout << " " ;

// Print j'th character
cout << str[j];

// Print i-j-1 spaces
for ( int x=0; x<i-j-1; x++)
cout << " " ;

// Print i'h character
cout << str[i] << endl;
}
}
}

// Driver program
int main()
{
printPattern( "lsbin" );
return 0;
}``````

## Java

``````// Java program to
// print cross pattern
class GFG
{

// Function to print given
// string in cross pattern
static void pattern(String str, int len)
{

// i and j are the indexes
// of characters to be
// displayed in the ith
// iteration i = 0 initially
// and go upto length of string
// j = length of string initially
// in each iteration of i, // we increment i and decrement j, // we print character only
// of k==i or k==j
for ( int i = 0 ; i < len; i++)
{
int j = len - 1 - i;
for ( int k = 0 ; k < len; k++)
{
if (k == i || k == j)
System.out.print(str.charAt(k));
else
System.out.print( " " );
}
System.out.println( "" );
}
}

// Driver code
public static void main (String[] args)
{
String str = "lsbin" ;
int len = str.length();
pattern(str, len);

}
}

// This code is contributed
// by Smitha``````

## Python3

``````# Python 3 program to
# print cross pattern

# Function to print given
# string in cross pattern
def pattern( str , len ):

# i and j are the indexes
# of characters to be
# displayed in the ith
# iteration i = 0 initially
# and go upto length of string
# j = length of string initially
# in each iteration of i, we
# increment i and decrement j, # we print character only of
# k==i or k==j
for i in range ( 0 , len ):

j = len - 1 - i
for k in range ( 0 , len ):

if (k = = i or k = = j):
print ( str [k], end = "")
else :
print (end = " " )

print ( " " )

# Driver code
str = "lsbin"
len = len ( str )
pattern( str , len )

# This code is contributed
# by Smitha``````

## C#

``````// C# program to print
// cross pattern
using System;

class GFG
{

// Function to print given
// string in cross pattern
static void pattern(String str, int len)
{

// i and j are the indexes
// of characters to be
// displayed in the ith
// iteration i = 0 initially
// and go upto length of string
// j = length of string initially
// in each iteration of i, we
// increment i and decrement j, // we print character only of
// k==i or k==j
for ( int i = 0; i < len; i++)
{
int j = len - 1 - i;
for ( int k = 0; k < len; k++)
{
if (k == i || k == j)
Console.Write(str[k]);
else
Console.Write( " " );
}
Console.Write( "\n" );
}
}

// Driver code
public static void Main ()
{
String str = "lsbin" ;
int len = str.Length;
pattern(str, len);
}
}

// This code is contributed by Smitha``````

## 的PHP

``````<?php
// PHP program to print
// Cross pattern

// Function to print given
// string in cross pattern, // Length of string must be odd
function printPattern( \$str )
{
\$len = strlen ( \$str );

// i goes from 0 to len and
// j goes from len-1 to 0
for ( \$i = 0, \$j = \$len - 1;
\$i <= \$len , \$j >= 0;
\$i ++, \$j --)
{
// To print the upper part.
// This loop runs til middle point
// of string i and j become same
if ( \$i < \$j )
{
// Print i spaces
for ( \$x = 0; \$x < \$i ; \$x ++)
echo " " ;

// Print i'th character
echo \$str [ \$i ];

// Print j-i-1 spaces
for ( \$x = 0; \$x < \$j - \$i - 1;
\$x ++)
echo " " ;

// Print j'th character
echo \$str [ \$j ]. "\n" ;
}

// To print center point
if ( \$i == \$j )
{
// Print i spaces
for ( \$x = 0; \$x < \$i ; \$x ++)
echo " " ;

// Print middle character
echo \$str [ \$i ]. "\n" ;
}

// To print lower part
else if ( \$i > \$j )
{
// Print j spances
for ( \$x = \$j - 1; \$x >= 0;
\$x --)
echo " " ;

// Print j'th character
echo \$str [ \$j ];

// Print i-j-1 spaces
for ( \$x = 0; \$x < \$i - \$j - 1;
\$x ++)
echo " " ;

// Print i'h character
echo \$str [ \$i ]. "\n" ;
}
}
}

// Driver code
printPattern( "lsbin" );

// This code is contributed by mits
?>``````

``````g           s
e         k
e       e
k     e
s   g
f r
o
f r
s   g
k     e
e       e
e         k
g           s``````

## C ++

``````// CPP program to print cross pattern
#include<bits/stdc++.h>
using namespace std;

// Function to print given string in
// cross pattern
void pattern(string str, int len){

// i and j are the indexes of characters
// to be displayed in the ith iteration
// i = 0 initially and go upto length of
// string
// j = length of string initially
// in each iteration of i, we increment
// i and decrement j, we print character
// only of k==i or k==j
for ( int i = 0; i < len; i++)
{
int j = len -1 - i;
for ( int k = 0; k < len; k++)
{
if (k == i || k == j)
cout << str[k];
else
cout << " " ;
}
cout << endl;
}
}

// driver code
int main ()
{
string str = "lsbin" ;
int len = str.size();
pattern(str, len);

return 0;
}
// This code is contributed by Satinder Kaur``````

## Java

``````// Java program to print cross pattern

class GFG
{

// Function to print given
// string in cross pattern
static void pattern(String str, int len)
{

// i and j are the indexes of
// characters  to be displayed
// in the ith iteration i = 0
// initially and go upto length
// of string j = length of string
// initially in each iteration
// of i, we increment i and decrement
// j, we print character only
// of k==i or k==j
for ( int i = 0 ; i < len; i++)
{
int j = len - 1 - i;
for ( int k = 0 ; k < len; k++)
{
if (k == i || k == j)
System.out.print(str.charAt(k));

else
System.out.print( " " );
}
System.out.println( "" );
}
}

// driver code
public static void main(String[] args)
{
String str = "lsbin" ;
int len = str.length();
pattern(str, len);
}
}

// This code is contributed by 29AjayKumar``````

## Python3

``````# Python 3 program to print cross pattern

# Function to print given string in
# cross pattern
def pattern(st, length):

# i and j are the indexes of characters
# to be displayed in the ith iteration
# i = 0 initially and go upto length of
# string
# j = length of string initially
# in each iteration of i, we increment
# i and decrement j, we print character
# only of k==i or k==j
for i in range (length):
j = length - 1 - i
for k in range (length):
if (k = = i or k = = j):
print (st[k], end = "")
else :
print ( " " , end = "")
print ()

# driver code
if __name__ = = "__main__" :

st = "lsbin"
length = len (st)
pattern(st, length)``````

## C#

``````// C# program to print cross pattern
using System;

class GFG
{

// Function to print given
// string in cross pattern
static void pattern(String str, int len)
{

// i and j are the indexes of
// characters to be displayed
// in the ith iteration i = 0
// initially and go upto length
// of string j = length of string
// initially in each iteration
// of i, we increment i and decrement
// j, we print character only
// of k==i or k==j
for ( int i = 0; i < len; i++)
{
int j = len -1 - i;
for ( int k = 0; k < len; k++)
{
if (k == i || k == j)
Console.Write(str[k]);

else
Console.Write( " " );
}
Console.WriteLine( "" );
}
}

// Driver code
public static void Main(String[] args)
{
String str = "lsbin" ;
int len = str.Length;
pattern(str, len);
}
}

// This code is contributed by Rajput-Ji``````

## 的PHP

``````<?php
// PHP program to print
// cross pattern

// Function to print given
// string in cross pattern
function pattern( \$str , \$len )
{

// i and j are the indexes of
// characters  to be displayed
// in the ith iteration i = 0
// initially and go upto length of
// string
// j = length of string initially
// in each iteration of i, we
// increment i and decrement j, we
// print character only of k==i or k==j
for ( \$i = 0; \$i < \$len ; \$i ++)
{
\$j = \$len -1 - \$i ;
for ( \$k = 0; \$k < \$len ; \$k ++)
{
if ( \$k == \$i || \$k == \$j )
echo \$str [ \$k ];
else
echo " " ;
}
echo "\n" ;
}
}

// Driver code
\$str = "lsbin" ;
\$len = strlen ( \$str );
pattern( \$str , \$len );

// This code is contributed by mits
?>``````

``````g           s
e         k
e       e
k     e
s   g
f r
o
f r
s   g
k     e
e       e
e         k
g           s``````

：也可以通过观察将字符包围在矩阵中来观察字符沿左右对角线的打印方式来解决。现在, 如果字符串的长度是

.

• 可以通过条件访问沿左对角线的元素(i == j)其中i和j分别是行号和列号。
• 可以通过条件访问右对角线上的元素(i + j == len-1).

## CPP

``````// C++ program to print the given pattern

#include<bits/stdc++.h>
using namespace std;

// Function to print the given
// string in respective pattern
void printPattern(string str, int len)
{
for ( int i = 0; i < len; i++)
{
for ( int j = 0; j < len; j++)
{
// Print characters at corresponding
// places satisfying the two conditions
if ((i == j) || (i + j == len-1))
cout<<str[i];
// Print blank space at rest of places
else
cout<< " " ;
}

cout<<endl;
}
}

// Driver Code
int main()
{
string str = "lsbin" ;

int len = str.size();

printPattern(str, len);

return 0;
}

// This code and Approach is contributed by
// Aravind Kimonn``````

``````g           s
e         k
e       e
k     e
s   g
f r
o
f r
s   g
k     e
e       e
e         k
g           s``````

Dinesh T.P.D

。如果发现任何不正确的地方, 或者想分享有关上述主题的更多信息, 请发表评论。