Swift Program to Print Alphabetic Inverted Pattern


This tutorial will discuss how to write swift program to print alphabetic inverted 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 a 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 an alphabetic inverted pattern 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, and nested for loop is used to print alphabets starting from A to Z in columns.

Example

The following program shows how to print alphabetic inverted 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("G").value

// Handle the length of pattern 
for x in N1...N2 {

   // Printing alphabetic inverted 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  
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 an alphabetic solid square pattern starting from A to G. 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(“G").value // G = 71

Now we use nested for loops to print alphabetic inverted pattern. The outer most for loop(starts from A to G or 65 to 71) is use to handle the total number of rows are going to print and each row is start with new line. Now the nested for loop(starts from N1 to N1+(N2-x)) is used to print alphabets starting from A to G in inverted 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 alphabetic inverted pattern using string.init() function.

import Foundation 
import Glibc

// Size of the alphabetic inverted pattern 
let num = 7

// Handle the length of pattern 
for i in 0...num-1 {

   // Printing alphabetic inverted pattern 
   print(String.init(repeating:"XYZ", count:num-i)) 
}

Output

XYZXYZXYZXYZXYZXYZXYZ
XYZXYZXYZXYZXYZXYZ
XYZXYZXYZXYZXYZ
XYZXYZXYZXYZ
XYZXYZXYZ
XYZXYZ
XYZ

Here in the above code, we create an inverted star pattern of length 7 using String.init() function. Here we uses for loop(starting from 0 to num-1) which is used to print each row. In this loop, we use String.init() function. This function prints “XYZ” according to the count value(that is num-i) −

print(String.init(repeating:"XYZ", count:num-i))

So the working of the above code is −

num = 7 
In 1st iteration: i = 0 
print(String.init(repeating: "XYZ”, count: 7-0)) 
So it print 7 “XYZ”
In 2nd iteration: i = 1 
print(String.init(repeating: "XYZ”, count: 7-1)) 
So it print 6 “XYZ”
... so on till 7th iteration and print alphabetic star pattern.

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 used 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 alphabetic inverted 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("I").value

// Handle the length of pattern
for i in stride(from:N1, to:N2, by: 1){ 
   // Printing alphabetic inverted pattern 
   // Using stride() function 
   for j in stride(from:N1, to:N1+(N2-i), by: 1) {   
      print(UnicodeScalar(j)!, terminator:" ") 
   } 
   print("") 
}

Output

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 an alphabetic inverted pattern starting from A to I. 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(“I").value // I = 73

Here in the above code, we uses nested for loops along with stride() function. The outermost for loop is used to handle the total number of rows are going to print and each row starts with a new line, here we use stride() function

for i in stride(from:N1, to:N2, by: 1)

Here the iteration starts from N1 to N2 or 65 to 73 and each iteration increased by one.

The nested for loop is used to print alphabetic inverted pattern using stride() function −

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

Here the iteration starts from N1 to N1+(N2-i) and each iteration increased by one and print alphabets starting from A to I in inverted pattern.

Updated on: 30-Nov-2022

136 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements