
- 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 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`.
- Related Articles
- C program to check if a given string is Keyword or not?
- Python program to check if a given string is Keyword or not
- Swift program to check if string is pangram or not
- Swift Program to check whether a given string is Heterogram or not
- Swift Program to check the given string is empty or not
- Swift Program to check a given number is finite or not
- How to check if a given word is a Python keyword or not?
- Swift Program to Check if the given number is Perfect number or not
- Python - Check if a given string is binary string or not
- Swift Program to Check If a Number is Spy number or not
- Swift Program to check if an Array is Palindrome or not
- Java Program to check if a string is empty or not
- C# program to check if a string is palindrome or not
- Python program to check if a string is palindrome or not
- Java program to check whether a given string is Heterogram or not
