Swift program to print hollow rectangle star pattern


This tutorial will discuss how to write swift program to print hollow rectangle star pattern.

Star pattern is a sequence of “*” which is used to develop different patterns or shapes like pyramid, rectangle, cross, etc. These star patterns are generally used to understand or practice the program flow controls, also they are good for logical thinking.

To create a hollow rectangle star pattern we can use any of the following methods −

  • Using nested for loop
  • Using stride Function

Below is a demonstration of the same −

Input

Suppose our given input is −

Length = 10
Width = 6

Output

The desired output would be −

* * * * * * * * * * 
*                 * 
*                 * 
*                 * 
*                 * 
* * * * * * * * * * 

Method 1- Using nested for loop

We can create a hollow rectangle star 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 “*” in columns.

Example

The following program shows how to print hollow rectangle star pattern using nested for loop.

import Foundation
import Glibc

// Length and width of the rectangle 
var Rlen = 10
var Rwid = 6

// Handle the rows
for x in 1..<Rwid+1{

   // Handle Columns
   for y in 1..<Rlen+1{
    
      // Here print star for first or last row
      // or for first or last column, 
      // otherwise print blank space
      if (x==1 || x==Rwid || y==1 || y == Rlen){
         print("*", terminator:" ")
      }
      else{
         print(" ", terminator:" ")
      }
   }
   print("")
}

Output

* * * * * * * * * * 
*                 * 
*                 * 
*                 * 
*                 * 
* * * * * * * * * * 

Here, in the above code, we have length = 10 and width = 6. Now we use nested for loops to print hollow rectangle star pattern. The outer most for loop(starts from 1 to 6) 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 1 to 10) is used to print “*” for first or last row or for first or last column, otherwise print blank space.

Method 2- 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 represent upward iteration or increment and negative value represent the downward iteration or decrement.

Example

The following program shows how to print hollow rectangle star pattern using stride() function.

import Foundation
import Glibc

// Length and width of the rectangle 
var Rlen = 9
var Rwid = 5

// Handle the rows
for x in stride(from:1, to:Rwid+1, by: 1){

   // Handle Columns
   for y in stride(from:1, to:Rlen+1, by: 1){
    
      // Here print star for first or last row
      // or for first or last column, 
      // otherwise print blank space
      if (x==1 || x==Rwid || y==1 || y == Rlen){
         print("*", terminator:" ")
      }
      else{
         print(" ", terminator:" ")
      }
   }
   print("")
}

Output

* * * * * * * * * 
*               * 
*               * 
*               * 
* * * * * * * * * 

Here, in the above code, we have length = 9 and width = 5. Now we uses nested for loops. The outermost for loop(starts from 1 to 9) with stride() is used to handle the total number of rows are going to print and each row starts with a new line. The nested for loop is used to print hollow rectangle star pattern using stride() function −

for y in stride(from:1, to:Rlen+1, by: 1){
   if (x==1 || x==Rwid || y==1 || y == Rlen){
      print("*", terminator:" ")
   }
   else{
      print(" ", terminator:" ")
   }
}

Here the iteration starts from 1 to Rlen+1 and each iteration increased by one. In this loop, we print star for first or last row or for first or last column, otherwise print blank space. So this is how we print hollow rectangle star pattern.

Updated on: 13-Dec-2022

395 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements