给定一个字符串,需要将该字符串转换为波形
null
例如:
Input : GeeksforGeeks, 2Output :G e s o G e s e k f r e k Input : GeeksforGeeks, 4Output :G o s e f r k e s G e k e
C++
// CPP program to print wave pattern // of a given string // This is a modified code of #include<bits/stdc++.h> using namespace std; // Function that takes string and zigzag offset void fun(string s, int n) { // if offset is 1 if (n==1) { // simply print the string and return cout << s; return ; } // Get length of the string int len = s.length(); // Create a 2d character array char a[len][len] = { }; // for counting the rows of the ZigZag int row = 0; bool down; for ( int i=0; i<len; i++) { // put characters in the matrix a[row][i] = s[i]; // You have reached the bottom if (row==n-1) down = false ; else if (row==0) down = true ; (down)?(row++):(row--); } // Print the Zig-Zag String for ( int i=0; i<n; i++) { for ( int j=0; j<len; j++) { cout<<a[i][j]<< " " ; } cout<<endl; } } // Driver function int main() { string s = "GeeksforGeeks" ; int n = 3; fun(s, n); } |
JAVA
// Java program to print wave // pattern of a given string import java.lang.*; import java.util.*; class GFG { // Function that takes // string and zigzag offset static void fun(String s, int n) { // if offset is 1 if (n == 1 ) { // simply print the // string and return System.out.print(s); return ; } // Get length of the string int len = s.length(); // Create a 2d character array char [][]a = new char [len][len]; char []c = s.toCharArray(); // for counting the // rows of the ZigZag int row = 0 ; boolean down = true ; for ( int i = 0 ; i < len; i++) { // put characters in // the matrix a[row][i] = c[i]; // You have reached // the bottom if (row == n - 1 ) down = false ; else if (row == 0 ) down = true ; if (down) row++; else row--; } // Print the Zig-Zag String for ( int i = 0 ; i < n; i++) { for ( int j = 0 ; j < len; j++) { System.out.print(a[i][j] + " " ); } System.out.println(); } } // Driver Code public static void main(String[] args) { String s = "GeeksforGeeks" ; int n = 3 ; fun(s, n); } } // This code is contributed // by ChitraNayal |
Python 3
# Function that takes string # and zigzag offset def fun(s, n): # if offset is 1 if (n = = 1 ): # simply print the # string and return print (s) return # Get length of the string l = len (s) # Create a 2d character array a = [[ " " for x in range (l)] for y in range (l)] # for counting the # rows of the ZigZag row = 0 for i in range (l): # put characters in the matrix a[row][i] = s[i]; # You have reached the bottom if row = = n - 1 : down = False elif row = = 0 : down = True if down = = True : row = row + 1 else : row = row - 1 # Print the Zig-Zag String for i in range (n): for j in range (l): print ( str (a[i][j]), end = " " ) print () # Driver Code s = "GeeksforGeeks" n = 3 fun(s, n) # This code is contributed # by ChitraNayal |
C#
// C# program to print wave // pattern of a given string using System; class GFG { // Function that takes // string and zigzag offset static void fun( string s, int n) { // if offset is 1 if (n == 1) { // simply print the // string and return Console.Write(s); return ; } // Get length of the string int len = s.Length; // Create a 2d character array char [,] a = new char [len,len]; char [] c = s.ToCharArray(); // for counting the // rows of the ZigZag int row = 0; bool down = true ; for ( int i = 0; i < len; i++) { // put characters // in the matrix a[row, i] = c[i]; // You have reached the bottom if (row == n - 1) down = false ; else if (row == 0) down = true ; if (down) row++; else row--; } // Print the Zig-Zag String for ( int i = 0; i < n; i++) { for ( int j = 0; j < len; j++) { Console.Write(a[i, j] + " " ); } Console.Write( "" ); } } // Driver Code public static void Main() { string s = "GeeksforGeeks" ; int n = 3; fun(s, n); } } // This code is contributed // by ChitraNayal |
PHP
<?php // php program to print wave // pattern of a given string // Function that takes string // and zigzag offset function fun( $s , $n ) { // if offset is 1 if ( $n == 1) { // simply print the // string and return echo $s ; return ; } // Get length of the string $len = strlen ( $s ); // for counting the rows // of the ZigZag $row = 0; $down ; for ( $i = 0; $i < $len ; $i ++) for ( $j = 0; $j < $len ; $j ++) $a [ $i ][ $j ]= " " ; for ( $i = 0; $i < $len ; $i ++) { // put characters // in the matrix $a [ $row ][ $i ] = $s [ $i ]; // You have reached // the bottom if ( $row == $n - 1) $down = false; else if ( $row == 0) $down = true; ( $down )? ( $row ++): ( $row --); } // Print the Zig-Zag String for ( $i = 0; $i < $n ; $i ++) { for ( $j = 0; $j < $len ; $j ++) { echo $a [ $i ][ $j ]. " " ; } echo "" ; } } // Driver code $s = "GeeksforGeeks" ; $n = 3; fun( $s , $n ); //This code is contributed by mits ?> |
Javascript
<script> // Javascript program to print wave // pattern of a given string // Function that takes // string and zigzag offset function fun(s,n) { // if offset is 1 if (n == 1) { // simply print the // string and return document.write(s); return ; } // Get length of the string let len = s.length; // Create a 2d character array let a = new Array(len); for (let i=0;i<len;i++) { a[i]= new Array(len); for (let j=0;j<len;j++) { a[i][j]= ' ' ; } } let c = s.split( "" ); // for counting the // rows of the ZigZag let row = 0; let down = true ; for (let i = 0; i < len; i++) { // put characters in // the matrix a[row][i] = s[i]; // You have reached // the bottom if (row == n - 1) down = false ; else if (row == 0) down = true ; if (down) row++; else row--; } // Print the Zig-Zag String for (let i = 0; i < n; i++) { for (let j = 0; j < len; j++) { document.write(a[i][j] + " " ); } document.write( "<br>" ); } } // Driver Code let s = "GeeksforGeeks" ; let n = 3; fun(s, n); // This code is contributed by avanitrachhadiya2155 </script> |
输出:
G s G s e k f r e k e o e
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END