
- Swift Tutorial
- Swift - Home
- Swift - Overview
- Swift - Environment
- Swift - Basic Syntax
- Swift - Data Types
- Swift - Variables
- Swift - Optionals
- Swift - Tuples
- Swift - Constants
- Swift - Literals
- Swift - Operators
- Swift - Decision Making
- Swift - Loops
- Swift - Strings
- Swift - Characters
- Swift - Arrays
- Swift - Sets
- Swift - Dictionaries
- Swift - Functions
- Swift - Closures
- Swift - Enumerations
- Swift - Structures
- Swift - Classes
- Swift - Properties
- Swift - Methods
- Swift - Subscripts
- Swift - Inheritance
- Swift - Initialization
- Swift - Deinitialization
- Swift - ARC Overview
- Swift - Optional Chaining
- Swift - Type Casting
- Swift - Extensions
- Swift - Protocols
- Swift - Generics
- Swift - Access Control
- Swift Useful Resources
- Swift - Compile Online
- Swift - Quick Guide
- Swift - Useful Resources
- Swift - Discussion
Swift Program to check whether a given string is Heterogram or not
Heterogram string is a string in which no alphabet occurs more than once. For example, “Sky with cloud” is a heterogram because in the string each alphabet occurred only once. Whereas “colorful balloons” is not a Heterogram because in the string some alphabet like o, u, l, etc appears more than once. Here we will execute different examples that will check whether the given string is heterogram or not using swift programming language.
Algorithm
Step 1 − Create a function.
Step 2 − Remove the white space from the string.
Step 3 − Create a separate array to store all the alphabets present in the input string.
Step 4 − Convert the array into set to store unique elements.
Step 5 − If the length of the set is equal to the number of input alphabets means each alphabet occurred once then the set is heterogram. Otherwise not.
Step 6 − Create a string and pass it into the function.
Step 7 − Print the output.
Example 1
Following Swift program to check whether a given string is heterogram or not.
import Foundation import Glibc // Function to check if the given string is heterogram or not func CheckHeterogram(str: String) -> Bool { // Remove the white spaces from the string let nStr = str.replacingOccurrences(of: " ", with: "") var set = Set<Character>() for c in nStr { if set.contains(c) { return false } else { set.insert(c) } } return true } // Test case 1 let myString1 = "I love Ma" print("Is String-'\(myString1)' is a Heterogram?:", CheckHeterogram(str: myString1)) // Test case 2 let myString2 = "My sheep color is black" print("Is String-'\(myString2)' is a Heterogram?:", CheckHeterogram(str: myString2))
Output
Is String-'I love Ma' is a Heterogram?: true Is String-'My sheep color is black' is a Heterogram?: false
Here in the above code, we create a function named CheckHeterogram(). In this function, we uses set to keep track of the encountered characters. Then iterate through each character of the input string and check if the character is already present in the set or not. If this function return false, then that means the string is not heterogram. If the function iterates through the entire string without finding any duplicates, then it true which means the string is heterogram.
Example 2
Following Swift program to check whether a given string is heterogram or not.
import Foundation import Glibc // Function to check if the given string is heterogram or not func CheckHeterogram(str: String) -> Bool { let nStr = str.replacingOccurrences(of: " ", with: "") let lString = nStr.lowercased() let charArray = Array(lString) let uniqueChars = Set(charArray) return uniqueChars.count == charArray.count } // Test case 1 let myString1 = "Hey Suman" print("Is String-'\(myString1)' is a Heterogram?:", CheckHeterogram(str: myString1)) // Test case 2 let myString2 = "I love Cooking Food" print("Is String-'\(myString2)' is a Heterogram?:", CheckHeterogram(str: myString2))
Output
Is String-'Hey Suman' is a Heterogram?: true Is String-'I love Cooking Food' is a Heterogram?: false
Here in the above code, we have two input strings. Now we create a function to check if the given string are heterogram or not. In this function, first we convert the input string to lowercase and then creates an array of characters from the string and then convert the array into the set for unique characters. Now this function return true if the uniqueChar count is equal to the characterArray count that means the string is heterogram. This function will return false if the uniqueChar count is not equal to the characterArray count that means the string is not heterogram
Conclusion
So this is how we can check if the given string is heterogram or not.
- Related Articles
- Java program to check whether a given string is Heterogram or not
- Python program to check whether a given string is Heterogram or not
- C# program to check whether a given string is Heterogram or not
- Swift Program to check if a given string is Keyword or not
- Swift Program to check the given string is empty or not
- C++ program to check whether given string is bad or not
- Swift Program to check a given number is finite or not
- Swift Program to Check whether a number is a Perfect Cube or not
- Swift program to check if string is pangram or not
- C++ Program to check whether given password is strong or not
- Swift Program to Check Whether Two Matrices Are Equal or Not
- Python Program to Check Whether a String is a Palindrome or not Using Recursion
- C program to check if a given string is Keyword or not?
- Python program to check if a given string is Keyword or not
- Program to check whether given graph is bipartite or not in Python
