Zig-Zag Triangle – String Pattern
Print a zig-zag triangular pattern using characters from a string with alternating forward and reverse order
Understand the Problem
Problem Statement
Zig-Zag Triangle – String Pattern: The program must accept a string S and an integer N as the input. The program must print N lines of output based on the following conditions.
- The 1st line contains N-1 asterisks and the first character of S.
- The 2nd line contains N-2 asterisks and the next 3 characters of S.
- The 3rd line contains N-3 asterisks and the next 5 characters of S in reverse order.
- The 4th line contains N-4 asterisks and the next 7 characters of S.
- The 5th line contains N-5 asterisks and the next 9 characters of S in reverse order.
- Similarly, the remaining lines are printed in the zig-zag order.
- If there are no more characters in S when printing lines, the program must print # for those characters.
Constraints
- 1 ≤ Length of S ≤ 1000
- 3 ≤ N ≤ 50
- Characters can be any printable ASCII characters
- Output lines will have decreasing asterisks from N-1 to 0
- Character extraction follows odd number sequence: 1, 3, 5, 7, 9...
- Odd-numbered lines (1st, 3rd, 5th...) print characters in reverse order
- Even-numbered lines (2nd, 4th, 6th...) print characters in normal order
Examples
skillrack
3**s
*kil
kcarlFor N=3: - Line 1: 2 asterisks + 1st character 's' → '**s' - Line 2: 1 asterisk + next 3 characters 'kil' → '*kil' - Line 3: 0 asterisks + next 5 characters 'lrack' in reverse → 'kcarl'
Telegram
4***T
**ele
*#marg
#######For N=4: - Line 1: 3 asterisks + 1st character 'T' → '***T' - Line 2: 2 asterisks + next 3 characters 'ele' → '**ele' - Line 3: 1 asterisk + next 5 characters, but only 'gram' available, so padded with '#' → '*#marg' - Line 4: 0 asterisks + next 7 characters, all padded with '#' → '#######'
Acknowledgement
3**A
*ckn
delwoFor N=3: - Line 1: 2 asterisks + 1st character 'A' → '**A' - Line 2: 1 asterisk + next 3 characters 'ckn' → '*ckn' - Line 3: 0 asterisks + next 5 characters 'delwo' in reverse → 'delwo'
Solution
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
char s[10000];
int n;
// Read input
fgets(s, sizeof(s), stdin);
s[strcspn(s, "\n")] = 0; // Remove newline
scanf("%d", &n);
// Pad string with '#' characters
int len = strlen(s);
for (int i = len; i < 10000; i++) {
s[i] = '#';
}
s[9999] = '\0';
int index = 0;
// Generate each line
for (int line = 1; line <= n; line++) {
// Print leading asterisks
for (int j = 0; j < n - line; j++) {
printf("*");
}
// Calculate number of characters to extract (1, 3, 5, 7...)
int chars_to_extract = 2 * line - 1;
// Extract characters
char temp[100];
for (int j = 0; j < chars_to_extract; j++) {
temp[j] = s[index + j];
}
temp[chars_to_extract] = '\0';
// Print in reverse order for odd-numbered lines
if (line % 2 == 1) {
for (int j = chars_to_extract - 1; j >= 0; j--) {
printf("%c", temp[j]);
}
} else {
printf("%s", temp);
}
printf("\n");
index += chars_to_extract;
}
return 0;
}The C solution follows these steps:
- Input Reading: Uses
fgets()to read the string andscanf()for the integer N. - String Padding: Extends the string with '#' characters to prevent index out-of-bounds errors during character extraction.
- Line Generation Loop: Iterates from 1 to N to generate each line.
- Asterisk Printing: Prints N-line asterisks at the beginning of each line.
- Character Extraction: Calculates the number of characters to extract using the formula 2*line-1, which gives the sequence 1, 3, 5, 7...
- Reverse Logic: For odd-numbered lines (line % 2 == 1), prints characters in reverse order. For even-numbered lines, prints in normal order.
- Index Management: Advances the character index by the number of characters extracted for the next iteration.
The solution handles edge cases where the string runs out of characters by padding with '#' characters.