Swift Program to Print Solid Rectangle Star Pattern

This tutorial will discuss how to write swift program to print solid 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 solid rectangle star 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 = 7 
Width = 5

Output

The desired output would be ?

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

Method 1 - Using Nested For Loop

We can create a solid 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 solid rectangle star pattern using nested for loop.

<div class="execute"></div><div class="code-mirror  language-javascript" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">import</span> Foundation
<span class="token keyword">import</span> Glibc

<span class="token comment">// Length and width of solid rectangle</span>
<span class="token comment">// star pattern</span>
<span class="token keyword">let</span> length <span class="token operator">=</span> <span class="token number">8</span>
<span class="token keyword">let</span> width <span class="token operator">=</span> <span class="token number">3</span>

<span class="token comment">// Handle the width of the pattern</span>
<span class="token keyword">for</span> _ <span class="token keyword">in</span> <span class="token number">1.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>width<span class="token punctuation">{</span>
   
   <span class="token comment">// Handle the length of the pattern</span>
   <span class="token keyword">for</span> _ <span class="token keyword">in</span> <span class="token number">1.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>length<span class="token punctuation">{</span>
      <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"*"</span><span class="token punctuation">,</span> terminator <span class="token operator">:</span> <span class="token string">" "</span><span class="token punctuation">)</span>
   <span class="token punctuation">}</span>
   
   <span class="token comment">// New line after each row</span>
   <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">" "</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

Output

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

Here, in the above code, we have length = 8 and width = 3. Now we use nested for loops to print solid rectangle star pattern. The outer most for loop(starts from 1 to 3) 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 8) is used to handle the Toal number of columns are going to print in solid rectangle star pattern using "*".

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 solid rectangle star pattern using string.init() function.

<div class="execute"></div><div class="code-mirror  language-javascript" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">import</span> Foundation
<span class="token keyword">import</span> Glibc

<span class="token comment">// Length and width of solid rectangle</span>
<span class="token comment">// star pattern</span>
<span class="token keyword">let</span> length <span class="token operator">=</span> <span class="token number">8</span>
<span class="token keyword">let</span> width <span class="token operator">=</span> <span class="token number">4</span>

<span class="token comment">// Handle the length of pattern</span>
<span class="token keyword">for</span> _ <span class="token keyword">in</span> <span class="token number">1.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>width<span class="token punctuation">{</span>
   <span class="token comment">// Printing solid rectangle star pattern</span>
   <span class="token function">print</span><span class="token punctuation">(</span>String<span class="token punctuation">.</span><span class="token function">init</span><span class="token punctuation">(</span>repeating<span class="token operator">:</span> <span class="token string">"*"</span><span class="token punctuation">,</span> count<span class="token operator">:</span> width<span class="token punctuation">)</span> <span class="token operator">+</span> String<span class="token punctuation">.</span><span class="token function">init</span><span class="token punctuation">(</span>repeating<span class="token operator">:</span><span class="token string">"*"</span><span class="token punctuation">,</span> count<span class="token operator">:</span>length<span class="token operator">-</span>width<span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

Output

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

Here, in the above code, we have length = 8 and width = 3. Now using String.init() function we create a solid rectangle star pattern. Here we uses for loop (starting from 1 to 4) which is used to print each row. In this loop, we use String.init() function to print solid rectangle star pattern ?

String.init(repeating: "*", count: width)

Prints "*" in rows according to the value of count (that is length-width) and

String.init(repeating:"*", count:length-width)

Prints "*" in columns according to the value of count (that is length-width)

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 solid rectangle star pattern using stride() function.

<div class="execute"></div><div class="code-mirror  language-javascript" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">import</span> Foundation
<span class="token keyword">import</span> Glibc

<span class="token comment">// Length and width of solid rectangle</span>
<span class="token comment">// star pattern</span>
<span class="token keyword">let</span> length <span class="token operator">=</span> <span class="token number">10</span>
<span class="token keyword">let</span> width <span class="token operator">=</span> <span class="token number">6</span>

<span class="token comment">// Handle the length of pattern</span>
<span class="token keyword">for</span> _ <span class="token keyword">in</span> <span class="token number">1.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>width<span class="token punctuation">{</span>

   <span class="token comment">// Printing solid rectangle star pattern</span>
   <span class="token comment">// Using stride() function</span>
   <span class="token keyword">for</span> _ <span class="token keyword">in</span> <span class="token function">stride</span><span class="token punctuation">(</span><span class="token parameter">from<span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span> to<span class="token operator">:</span> length<span class="token punctuation">,</span> by<span class="token operator">:</span> <span class="token number">1</span></span><span class="token punctuation">)</span><span class="token punctuation">{</span>
      <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"*"</span><span class="token punctuation">,</span> terminator<span class="token operator">:</span><span class="token string">" "</span><span class="token punctuation">)</span>
   <span class="token punctuation">}</span>
   <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">""</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

Output

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

Here, in the above code, we have length = 10 and width = 6. Now we uses nested for loops. The outermost for loop(starts from 1 to 6) 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 solid rectangle star pattern using stride() function ?

for _ in stride(from: 1, to: length, by: 1) {
   print("*", terminator:" ") 
}

Here the iteration starts from 1 to length(i.e., 10 and each iteration increased by one and print "*" in solid rectangle pattern.

Updated on: 2022-11-03T11:43:19+05:30

463 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements