- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Print a String in wave pattern in C++
In this problem, we are given a string and an integer n. Our task is to print the given string in a wave pattern of n lines.
Let’s take an example to understand the problem,
Input: Tutorial n = 3 Output: T r U o i s t l
Wave patterns are printed by printing each character of the string one by one in the next line and tab space away from the next element till the nth line. And the printing tab spaces to the upper line till the first line and follow the same pattern until the string has characters.
Example
The below code gives the implementation of our solution,
#include<bits/stdc++.h> using namespace std; void printWavePattern(string s, int n) { if (n==1) { cout<<s; return; } int len=s.length(); char a[len][len]={ }; int row=0; bool down; for (int i=0; i<len; i++) { a[row][i]=s[i]; if (row==n-1) down=false; else if (row==0) down=true; (down)?(row++):(row--); } for (int i=0; i<n; i++) { for (int j=0; j<len; j++) { cout<<a[i][j]<<" "; } cout<<endl; } } int main() { string str = "TutorialsPoint"; int n = 4; cout<<n<<" Line wave pattern '"<<str<<"' is:\n"; printWavePattern(str, n); }
Output
4 Line wave pattern 'TutorialsPoint' is − T a n u i l i t t r s o o P
- Related Articles
- Print the Mirror Image of Sine-Wave Pattern in C
- Print a matrix in Reverse Wave Form in C++
- Program to print a rectangle pattern in C++
- Program to print a pattern of numbers in C++
- Print a pattern without using any loop in C++
- Print concentric rectangular pattern in a 2d matrix in C++
- Program to print Interesting pattern in C++
- Program to print Kite Pattern in C++
- Program to print number pattern in C
- Program to print Diamond Pattern in C
- Program to print numeric pattern in C
- Program to print pyramid pattern in C
- Print matrix in snake pattern in C Programming.
- Print symmetric double triangle pattern in C language
- Print numbers in matrix diagonal pattern in C Program.

Advertisements