The string step pattern prints one letter per line and pushes it in by 0, 1, 2, 1, 0 stars in turn, so the letters step out to the right and back again, like a staircase that keeps turning round.
Quick answer: Print the string one character per line. Before character k, print a number of stars taken from the repeating list 0, 1, 2, 1, using steps[k % 4]. The remainder by 4 restarts the list after every fourth letter.
This program is part of the String Patterns collection.
Table of contents8 sections
String Step Pattern output
Output (“abcdefghijk”):
a *b **c *d e *f **g *h i *j **k

How the string step pattern works
The indent follows a short cycle: none, one, two, one, and then the same again. Storing that cycle in a small array keeps the code simple: the remainder k % 4 picks the right entry for every letter, however long the string is. Changing the array changes the shape, for example 0, 1, 2, 3, 2, 1 for a deeper step.
- Store the cycle of star counts:
{0, 1, 2, 1}. - For each character
kof the string, printsteps[k % 4]stars. - Print the character and move to the next line.
Key takeaways
- The indent follows the cycle 0, 1, 2, 1.
k % 4picks the place in the cycle for any position.- A small array of counts is easier to change than nested conditions.
- It is a vertical cousin of the rail fence zig-zag.
String Step Pattern in C
#include <stdio.h>
#include <string.h>
int main(void) {
char s[] = "abcdefghijk";
int n = strlen(s);
int steps[] = {0, 1, 2, 1}; /* stars before each letter: 0, 1, 2, 1, then again */
for (int k = 0; k < n; k++) {
for (int t = 0; t < steps[k % 4]; t++)
printf("*");
printf("%c\n", s[k]);
}
return 0;
}String Step Pattern in C++
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "abcdefghijk";
int steps[] = {0, 1, 2, 1}; // stars before each letter: 0, 1, 2, 1, then again
for (int k = 0; k < (int) s.length(); k++)
cout << string(steps[k % 4], '*') << s[k] << endl;
return 0;
}String Step Pattern in Java
public class StringStepPattern {
public static void main(String[] args) {
String s = "abcdefghijk";
int[] steps = {0, 1, 2, 1}; // stars before each letter: 0, 1, 2, 1, then again
for (int k = 0; k < s.length(); k++)
System.out.println("*".repeat(steps[k % 4]) + s.charAt(k));
}
}String Step Pattern in Python
s = "abcdefghijk"
steps = [0, 1, 2, 1] # stars before each letter: 0, 1, 2, 1, then again
for k, ch in enumerate(s):
print("*" * steps[k % 4] + ch)Time and space complexity
The program prints each character once with at most two stars in front, so it runs in O(n) time for a string of length n and uses O(1) extra memory.
Frequently asked questions
How do I make the steps deeper?
Use a longer cycle, such as {0, 1, 2, 3, 2, 1}, and take the remainder by its length, 6.
Can I use spaces instead of stars?
Yes. Print spaces before each letter and the letters form a gentle wave down the screen.
Why does the pattern use a remainder?
The cycle has four steps, so letter k is at step k % 4: letters 0, 4 and 8 start at the edge, letters 2, 6 and 10 are the furthest in. Without the remainder you would need an ever-growing list.
How do I use my own word?
Change the string at the top. The loop uses its length, so nothing else changes.






