
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check If String Is Pangram in Swift
Pangram string is a string that contains all the English alphabet. For example ?
String = "The quick brown fox jumps over the lazy dog"
Here the string is a pangram string which contains all the alphabetical characters from a to z in it.
String = " Hello! Swati how are you?"
Here the string is not a pangram string because it does not contain all the 26 alphabetical characters from a to z.
In this article, we will learn how to write a swift program to check if a string is pangram or not.
So to check if the string is pangram or not we use the following methods ?
Using user defined function
Using set
Method 1- Using user defined function
In this method, we create a function in which we add all the 26 alphabets in a string and then check if the given string contain all the 26 alphabets in it or not. If yes then the given string is pangram. Otherwise, not.
Algorithm
Step 1 ? Create a function.
Step 2 ? Convert the given string into lowercase.
Step 3 ? Create a variable which store all the alphabets in a string.
Step 4 ? Set count = 0.
Step 5 ? Run a for loop to iterate through each character of the alphabet string.
Step 6 ? Check if the given lowercased string contain the specified alphabet or not using contains() function.
Step 7 ? If the given lowercased string contain all the alphabets, then it return true. Otherwise return false.
Step 8 ? Create two test strings and pass them into the function.
Step 9 ? Print the output.
Example
Following Swift program to check if string is pangram or not.
import Foundation import Glibc // Function to check if the given string is pangram or not func CheckPangram(str: String) -> Bool { let lowercaseString = str.lowercased() let alphabet = "abcdefghijklmnopqrstuvwxyz" for l in alphabet { if !lowercaseString.contains(l) { return false } } return true } // Test case 1 let str1 = "The quick brown fox jumps over the lazy dog" print("Is string- '\(str1)' is pangram?:", CheckPangram(str: str1)) // Test case 2 let str2 = "my car color is red" print("Is string- '\(str2)' is pangram?:", CheckPangram(str: str2))
Output
Is string- 'The quick brown fox jumps over the lazy dog' is pangram?: true Is string- 'my car color is red' is pangram?: false
Here in the above code, we have two input strings. Now we create a function named CheckPangram() to check if the given string is pangram or not. In this function, we first convert the given string into lower case using lowercased() function. Then create a string which contain all the 26 alphabets. Now run a for loop to iterate through each character of the alphabet string and check if that character is present in the given string or not using contains() function. If any of the character is missing, then it will return false. If all the characters are present, then it will return true.
Method 2- Using Sets
We can also check if the given string is pangram or not using sets.
Algorithm
Step 1 ? Create a function.
Step 2 ? Create a set of character type which contains all the alphabets.
Step 3 ? Convert the given string into lowercase using lowercased() function and then convert the result into set.
Step 4 ? Return true if the lowercased set is super set of alphabet set. Otherwise return false.
Step 5 ? Create two test strings and pass them into the function.
Step 6 ? Print the output.
Example
Following Swift program to check if string is pangram or not
import Foundation import Glibc // Function to check if the given string is pangram or not func CheckPangram(str: String) -> Bool { let alphabet: Set<Character> = Set("abcdefghijklmnopqrstuvwxyz") let lowercased = Set(str.lowercased()) return lowercased.isSuperset(of: alphabet) } // Test case 1 let str1 = "The quick brown fox jumps over the lazy dog" print("Is string- '\(str1)' is pangram?:", CheckPangram(str: str1)) // Test case 2 let str2 = "I cook delicious food" print("Is string- '\(str2)' is pangram?:", CheckPangram(str: str2))
Output
Is string- 'The quick brown fox jumps over the lazy dog' is pangram?: true Is string- 'I cook delicious food' is pangram?: false
Here in the above code, we have two two test strings. Now we create a function named CheckPangram() to check if the given string is pangram or not. In this function, first we create a set of character which contain all the alphabets from a to z. Then we convert the given test string into lowercase using lowercased() function and then convert into set. Now it return true if the lowercased set is superset of alphabet set means lowercased set contain all the alphabets. Return false if the lowercased set is not the superset of alphabet set means some alphabets are missing in the lowercased set.
Conclusion
In this article, we have learned about two different methods that can be used for checking whether the string is a pangram or not. In the first method, we have used an external user-defined function whereas in the second one, we have used the A to Z alphabet set in the main function itself.