Swift Program to Print Alphabetic Solid Square Pattern


This tutorial will discuss how to write swift program to print alphabetic solid square 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 solid square 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 −

Length = A to D

Output

The desired output would be −

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

Method 1- USING NESTED FOR LOOP

We can create an alphabetic solid square 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 solid square pattern using nested for loop.

import Foundation 
import Glibc

// Finding ASCII value of letters 
// Here A is the initial letter and G is the end letter 
var N1 = Unicode.Scalar("A").value 
var N2 = Unicode.Scalar("G").value

// Handle the length of pattern 
for _ in 1...5 {
   // Printing alphabetic solid square pattern 
   for x in N1...N2 {
      print(UnicodeScalar(x)!, terminator : " ") 
   }
   
   // New line after each row 
   print(" ") 
}

Output

A B C D E F G  
A B C D E F G  
A B C D E F G  
A B C D E F G  
A B C D E F G 

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. The outer most for loop(starts from 1 to 5) 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 65 to 71) is used to print alphabets in solid square 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 solid square pattern using string.init() function.

import Foundation 
import Glibc

// Size of the alphabetic solid square pattern 
let num = 5

// Handle the length of the pattern 
for _ in 1...num{
   // Printing alphabetic solid square pattern 
   print(String.init(repeating:"AB", count:num)) 
}

Output

ABABABABAB
ABABABABAB
ABABABABAB
ABABABABAB
ABABABABAB

Here in the above code, we create an alphabetic solid square pattern of length 5 using String.init() function. Here we uses for loop(starting from 1 to num) which is used to print each row. In this loop, we uses String.init() function. This function prints “ABC” according to the count value(that is num) −

print(String.init(repeating:"ABC", count:num))

So the working of the above code is −

num = 5 
In 1st iteration: 
print(String.init(repeating: "ABC”, count: 5)) 
So it print 5 “ABC” 
In 2nd iteration: 
print(String.init(repeating: "*”, count: 5)) 
So it print 5 “ABCABC”
... so on till 5th iteration and print alphabetic solid square 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 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 alphabetic solid square 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("D").value

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

Output

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

Here, in the above code, we print an alphabetic solid square pattern starting from A to D. 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(“D").value // D = 68

Now we uses nested for loops along with stride() function. The outermost for loop(start from 1 to 4) is used to handle the total number of rows are going to print and each row starts with a new line. And the nested for loop is used to print alphabetic solid square pattern using stride() function −

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

Here the iteration starts from N1 to N2+1 and each iteration is increased by one and print Square from A to D pattern.

Updated on: 30-Nov-2022

150 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements