Swift Program to check if a given string is Keyword or not


Keywords are reserved words used to perform some internal process or represent predefined actions. It is not allowed to use these keywords as a variable name, constant name, or identifier. Swift support approx 101 keywords. For example, ‘var’ is a keyword but ‘hey’ is not a keyword. Here, we will learn how to write a swift program to check whether a given string is a keyword.

Algorithm

  • Step 1 − Create an array of string types to store all the keywords supported by Swift.

  • Step 2 − Create a function to check if the given string is a keyword or not using contains() function.

  • Step 3 − Create a string.

  • Step 4 − Use an if-else statement to print “Yes” if the given string is the keyword, otherwise print “No”.

Example

Following the Swift program to check if a given string is a keyword or not.

import Foundation
import Glibc

// Array containing all the keywords 
let myKeywords: [String] = ["associatedtype", "init", "protocol", "class", "inout", "public", "deinit", "internal", "rethrows", "enum", "let", "static", "extension", "open", "struct", "fileprivate", "operator", "subscript", "func", "private", "typealias", "var", "import", "precedencegroup", "if", "else", "for", "while", "do", "break", "continue", "return", "in", "case", "repeat", "catch", "fallthrough", "throw", "default", "guard", "switch", "defer", "where", "Any", "nil", "as", "throws", "self", "true", "false", "Self", "try", "is", "super", "associativity", "left", "Protocol", "convenience", "mutating", "required", "didSet", "none", "right", "dynamic", "nonmutating", "set", "final", "optional", "some", "get", "override", "Type", "indirect", "postfix", "unowned", "infix", "precedence", "weak", "lazy", "prefix", "willSet", "#available", "#error", "#imageLiteral", "#colorLiteral", "#fileID", "#keyPath", "#column", "#fileLiteral", "#line", "#dsohandle", "#filePath", "#selector", "#elseif", "#file", "#sourceLocation", "#else", "#function", "#warning", "#endif", "#if"]

// Function to check if the given string is a keyword or not
func checkKeyword(str: String) -> Bool {
   return myKeywords.contains(str)
}

// test case 1
let myString1 = "var"
if (checkKeyword(str: myString1) == true) {
   print("YES! \(myString1) is a keyword")
}
else {
   print("NO! \(myString1) is not a keyword")
}

// test case 2
let myString2 = "Sky"
if (checkKeyword(str: myString2) == true) {
   print("YES! \(myString2) is a keyword")
}
else {
   print("NO! \(myString2) is not a keyword")
}

Output

YES! var is a keyword
NO! Sky is not a keyword

Here in the above code, we create an array in which we store all the keywords supported by Swift. Now we create a function in which we check if the given string is present in the array or not using contains() function. If the given string is present in the array then the string is the keyword so this function will return true. Or if the given string is not present in the array then the string is not keyword so this function will return false.

Conclusion

So this is how we can check if the given string is a keyword or not. Although we are not allowed to use keywords as an identifier but if you wish to use keyword as an identifier then you have to use backtick(`) before and after the name of the keyword. For example, `var`.

Updated on: 08-Feb-2023

183 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements