New: 214 pattern programs with tested code in C, C++, Java and Python Explore patterns

String Step Pattern (a, *b, **c)

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
  1. String Step Pattern output
  2. How the string step pattern works
  3. String Step Pattern in C
  4. String Step Pattern in C++
  5. String Step Pattern in Java
  6. String Step Pattern in Python
  7. Time and space complexity
  8. Frequently asked questions

String Step Pattern output

Output (“abcdefghijk”):

a
*b
**c
*d
e
*f
**g
*h
i
*j
**k
String Step Pattern layout diagram, row by row ("abcdefghijk")
How each row of the string step pattern is built (“abcdefghijk”). Blue cells are printed characters; grey dots are spaces.

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.

  1. Store the cycle of star counts: {0, 1, 2, 1}.
  2. For each character k of the string, print steps[k % 4] stars.
  3. Print the character and move to the next line.

Key takeaways

  • The indent follows the cycle 0, 1, 2, 1.
  • k % 4 picks 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.

This tutorial was drafted with AI assistance. Spotted a mistake? Tell us and we fix it. Read our editorial policy.

Quick check: can you predict the output?

Answer from the code, then check. Every answer comes from the real output above.

Pattern 23 of 23 in String Patterns

 

Still stuck? Ask AI to explain this code

Opens your favourite AI with this page, ready to explain every loop step by step.

Authored by

Ghanendra Yadav runs Pattern Programs from Bangalore, India. The site started in 2020 to help students practise loops and logic through pattern programs in C, C++, Java and Python. Every program published here is compiled and run before it goes live, and the output shown is the real output.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.