Print string of odd length in ' X ' format | CPP program to print cross pattern ; Function to print given string in cross pattern ; 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 ; driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void pattern ( string str , int len ) { 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 ; } } int main ( ) { string str = " geeksforgeeks " ; int len = str . size ( ) ; pattern ( str , len ) ; return 0 ; }