Swift Program to Print Reverse Pyramid Alphabetic Pattern


This tutorial will discuss how to write swift program to print reverse pyramid alphabetic pattern.

Alphabetic pattern is a sequence of alphabets starting from A to Z which is used to develop different patterns or shapes like pyramid, rectangle, cross, etc of alphabets.

To create an alphabetic inverted pattern, we can use any of the following methods −

Using nested for loop

Using init() Function

Using stride Function

Below is a demonstration of the same −

Input

Suppose our given input is −

Range = A to E

Output

The desired output would be −

A B C D E 
A B C D 
A B C 
A B 
A

METHOD 1- USING NESTED FOR LOOP

We can create a reverse pyramid of alphabets or any other pattern using nested for loops. Here each for loop handle different tasks such as outermost for loop is used for new rows, nested loops handle white spaces and alphabet pattern.

Example

The following program shows how to print reverse pyramid alphabetic pattern using nested for loop.

import Foundation 
import Glibc

// Finding the ASCII value of the alphabets 
var N1 = Unicode.Scalar("A").value 
var N2 = Unicode.Scalar("K").value

// Handle the length of pattern 
for x in N1...N2 { 
   for _ in 0...x-N1 { 
      print(terminator: " ") 
   } 
   // Printing reverse pyramid alphabetic pattern 
   for y in N1...N1+(N2-x) { 
      print(UnicodeScalar(y)!, terminator : " ") 
   }
   
   // New line after each row 
   print(" ") 
}

Output

A B C D E F G H I J K  
  A B C D E F G H I J  
   A B C D E F G H I  
    A B C D E F G H  
     A B C D E F G  
      A B C D E F  
       A B C D E  
        A B C D  
         A B C  
          A B  
           A  

Here, in the above code, we print reverse pyramid alphabetic pattern starting from A to K. So to print the pattern first we find the ASCII value of lower and upper limit using the following code −

var N1 = Unicode.Scalar(“A").value // A = 65 
var N2 = Unicode.Scalar(“K").value // K = 75

Now we uses nested for loops to print reverse pyramid. The outer most for loop(starts from N1 to N2) is use to handle the total number of rows are going to print and each row is start with new line. Now the first nested for loop(starts form 0 to x-N1) is used to print the white spaces. And the second nested for loop(starts from N1 to N1+N2-x) is used to print alphabets from A to K in reverse pyramid pattern.

METHOD 2- USING INIT() FUNCTION

Swift provide an in-built function named String.init(). Using this function we can able to create any pattern. String.init() function create a string in which the given character is repeated the specified number of times.

Syntax

Following is the syntax −

String.init(repeating:Character, count: Int)

Here, repeating represent the character which this method repeats and count represent the total number of time the given character repeat in the resultant string.

Example

The following program shows how to print reverse pyramid alphabetic pattern using string.init() function.

import Foundation 
import Glibc

// Size of the reverse pyramid alphabetic pattern 
let num = 7

var n = num

// Handle the length of pattern 
while(n >= 1) {

   // Printing reverse pyramid alphabetic pattern 
   print(String.init(repeating:" ", count:num-n) + String.init(repeating:"PQ", count:(2*n) - 1)) 
   n -= 1 
}

Output

PQPQPQPQPQPQPQPQPQPQPQPQPQ
 PQPQPQPQPQPQPQPQPQPQPQ
  PQPQPQPQPQPQPQPQPQ
   PQPQPQPQPQPQPQ
    PQPQPQPQPQ
     PQPQPQ
      PQ

Here in the above code, we create a reverse pyramid of height 7 using String.init() function. Here we use while loop(n >= 1) which starts from 7 and in each iteration the value of n is decrease by one. So String.init(repeating: " ", count: num-n) is used to print spaces, here in every iteration the init() function repeats the white spaces according to the count value(that is num-n) and in every iteration the value of count is increased by one. And String.init(repeating: "PQ", count: (2*n) - 1) is used to print “PQ” in reverse pyramid pattern, here in each iteration init() function repeats “PQ” according to the value of count(that is (2*n) - 1).

METHOD 3- USING STRIDE FUNCTION

Swift provide an in-built function named stride(). The stride() function is used to move from one value to another with increment or decrement. Or we can say stride() function return a sequence from the starting value but not include end value and each value in the given sequence is steps by the given amount.

Syntax

Following is the syntax −

stride(from:startValue, to: endValue, by:count)

Here,

from − Represent the starting value to use for the given sequence.

to − Represent the end value to limit the given sequence

by − Represent the amount to step by with each iteration, here positive value represents upward iteration or increment and negative value represent the downward iteration or decrement.

Example

The following program shows how to print reverse pyramid alphabetic pattern using stride() function.

import Foundation 
import Glibc

// Finding the ASCII value of the alphabets 
var N1 = Unicode.Scalar("A").value 
var N2 = Unicode.Scalar("E").value

for i in N1..<N2{
   // Printing white spaces 
   for _ in 0...i-N1 { 
      print(terminator: " ") 
   }
   
   // Printing reverse pyramid alphabetic pattern 
   for j in stride(from: N1, to: N1+(N2-i), by: 1) {  
      print(UnicodeScalar(j)!, terminator : " ") 
   }
   
   // New line after each row 
   print(" ") 
}

Output

A B C D  
  A B C  
   A B  
    A 

Here, in the above code, we print reverse pyramid alphabetic pattern starting from A to E. So to print the pattern first we find the ASCII value of lower and upper limit using the following code −

var N1 = Unicode.Scalar(“A").value // A = 65 
var N2 = Unicode.Scalar(“E").value // E = 69

Now we uses three nested for loops. The outermost for loop(starts from N1 to N2-1) is used to handle the total number of rows are going to print and each row starts with a new line. The first nested for-loop is used to print white spaces and in each iteration the white space is increased by one.

for _ in I…i-N1 { 
   print(terminator : " ") 
}

The second nested for loop is used to print alphabets in reverse pyramid pattern. Here stride() function is used to print alphabets. In this function, the iteration started from N1 to N1+(N2-i) and in each iteration the value is increase by one.

for j in stride(from: N1, to: N1+(N2-i), by: 1) {  
   print(UnicodeScalar(j)!, terminator : " ") 
}

Updated on: 30-Nov-2022

221 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements