Swift Program to Display Alphabets (A to Z) using loop


This tutorial will discuss how to write a Swift program to display alphabets (A to Z) using loop.

In Swift, we can display alphabets starting from A to Z in both lower and upper case with the help of for-loop. Here we use the following terms in the below codes −

Scalar − It represents a single value.

Unicode − It is a standard encoding for text.

UnicodeScalar − It represents a single Unicode scalar value.

Below is a demonstration of the same −

Suppose we enter the following input −

A to Z

Following is the desired output −

A B C D E F G H I J K L M N LO P Q R S T U V W X Y Z

Algorithm

The algorithm is explained below −

  • Step 1 − Declare variables with unicode scalar value using Unicode.Scalar.value −

    let initalAlphabet = Unicode.Scalar("A").value

    let endAlphabet = Unicode.Scalar(“Z").value

    Here, Unicode.Scalar.value return the unicode value of A and Z that are 65 and 90.

  • Step 2 − Run a for loop

  • Step 3 − Print the output

Example 1

The following program shows how to display alphabets (A to Z) using for loop in upper case.

import Foundation import Glibc let initalAlphabet = Unicode.Scalar("A").value let endAlphabet = Unicode.Scalar("Z").value print("Following are the alphabets from A to Z:") for k in initalAlphabet...endAlphabet { if let val = Unicode.Scalar(k) { print(val) } }

Output

Following are the alphabets from A to Z:
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z

Here in the above code, we create two variables with values named initalAlphabet = Unicode.Scalar("A").value and endAlphabet and endAlphabet = Unicode.Scalar(“Z”).value. Where the Unicode.Scalar(“A").value return the ASCII value of ‘A’ that is 65 and Unicode.Scalar(“Z”).value returns the ASCII value of ‘Z’ that is 90. Now we run a for loop from 65 to 90 and display all the Alphabet starting from A to Z using the following code −

if let val = Unicode.Scalar(k) {
   print(val)
}

Here Unicode.Scalar() convert all the ASCII value into alphabets.

Example 2

The following program shows how to display alphabets (A to Z) using for loop in lower case.

import Foundation import Glibc print("Following are the alphabets from a to z:") for char in "abcdefghijklmnopqrstuvwxyz" { print(char) }

Output

Following are the alphabets from a to z:
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z

Here in the above code, we simply create a for loop which contains all the alphabets in the string and display each alphabet separately.

Example 3

The following program shows how to display alphabets (A to Z) using for loop in lower case.

import Foundation import Glibc print("Following are the Alphabets: ") for val in UnicodeScalar("a").value...UnicodeScalar("z").value{ print(UnicodeScalar(val)!) }

Output

Following are the Alphabets: 
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z

Here in the above code, we create for loop starting from UnicodeScalar(“a").value to UnicodeScalar(“z”).value and display all the alphabets. Where UnicodeScalar().value returns the ASCII value of “a” and “z”. UnicodeScalar() function is used to convert the ASCII to the character.

Updated on: 05-Aug-2022

799 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements