Programming Articles - Page 309 of 3363

Swift Program to Replace a Character at a Specific Index

Ankita Saini
Updated on 14-Jun-2023 16:39:54

3K+ Views

To replace a character at a specified index with a new character Swift support the following methods − Using replaceSubrange() method. Using the append() method. Using replacingCharacters(in:with:). Using these methods you can replace any character from the given string with a new character. For Example Input: String = "Pink Bike" Index = 5 Character = "O" Output: "Pink Oike " Here, we replaced the character present on index 5 which is “B” with “O”. Method 1: Using the replaceSubrange() method To replace a character at a specified index in the given ... Read More

Swift Program to Remove leading zeros

Ankita Saini
Updated on 14-Jun-2023 16:35:37

2K+ Views

While working with strings sometimes we encounter some numeric strings whose leading numbers are zeros, e.g. 000003232, 00321, etc. To remove leading zeros from a numeric string − Using removeFirst() and hasPrefix() methods Using firstIndex() and dropFirst() methods Example Input: String = "000003231" Output: "3231 " Here, the input string contains 5 leading zeros so we remove these zeros from the input string so the resultant string is 3231. Method 1: Using removeFirst() and hasPrefix() methods So to remove leading ... Read More

Swift Program to Remove All Whitespaces from a String

Ankita Saini
Updated on 14-Jun-2023 12:23:59

8K+ Views

A whitespace character is a non-printable character which represents a space in the string. To remove all the whitespaces from a string Swift provides the following methods − Using isWhitespace Property Using component() function Using replacingOccurrences(of:with:) method Using regular expressions Method 1: Using isWhitespace Property isWhitespace property is used to check whether the given character is a whitespace character or not. In this method, we use the isWhitespace property with the filter() method to remove all the whitespaces present in the given string. Syntax var res = str.filter{!$0.isWhitespace} Here, the filter() method is called on the ... Read More

Swift Program to Print first letter of each word using regex

Ankita Saini
Updated on 14-Jun-2023 15:03:30

269 Views

In Swift, regex is known as a regular expression. It is used to create a pattern which helps in matching or extracting some specific port of the given string. We can create a regex instance with the help of a regular expression in regex literal or string. In this article, we are going to use regex to print the first letter of each word. Example Input: String = "Ram got first place" Output: "Rgfp " Here, the output string contains the first letters of each word present in the given string. In the following examples we are going to ... Read More

Swift Program to pad a string with 0's on the right side

Ankita Saini
Updated on 14-Jun-2023 11:37:27

1K+ Views

In Swift, pad a string with 0’s is to add 0 in the given string either on the left side or on the right side, for example, 234000 or 00021. Here we are going to pad a string with 0’s on the right side using the following methods − Using User-defined function Using pre-defined function Method 1: Using User-Defined Function To pad a string with 0’s on the right side we create a user-defined function which takes the input string and the total length of the resultant string as arguments and returns the resultant string. Example Input: String ... Read More

Swift Program to pad a string with 0's on left side

Ankita Saini
Updated on 14-Jun-2023 11:30:40

1K+ Views

In Swift, pad a string with 0’s is to add 0 in the given string either on the left side or on the right side, for example, 34000 or 0003232. Here we are going to pad a string with 0’s on the left side. Example Input: String = "151" newLength = 6 Output: 000151 Here, we pad the original string with three zeros on the left side. Algorithm Step 1 − Create a function which takes the original string and the length of the new string as arguments. Step 2 − Calculates the total number of ... Read More

Swift Program to Lookup enum by String value

Ankita Saini
Updated on 14-Jun-2023 11:23:35

906 Views

An enumeration or enum is a user-defined data type which holds a set of related values. It is defined by using the enum keyword. It is also known as an enum case because it uses case keywords to declare values inside it. In Swift, we are allowed to create an enum by the string value. Syntax enum nameOfEnum: Type { case value1 case value2 case value3 } Let enumVariable = value Here, nameOfEnum represents the name of the enum, value1, value2, etc are values defined in the enum and ... Read More

Swift Program to Implement the queue data structure

Ankita Saini
Updated on 14-Jun-2023 11:18:50

590 Views

A queue is a data structure which worked upon FIFO(first in first out) principle. In a queue, both ends are open, so that we can add a new element from one end called the rear or tail, this operation is known as enqueue and remove element from another end called the front or head, this operation is known as dequeue. Although Swift does not support any in-built queue data structure, still we can implement queue using various ways like link-list, structure, class, array, etc. You can use any of the methods to implement the queue data structure according to ... Read More

Swift Program to Implement switch statement on Strings

Ankita Saini
Updated on 14-Jun-2023 10:51:25

936 Views

A switch statement is a control flow statement in which it only executes the block of code as soon as the expression given in the switch statement matches a case among the given multiple cases. If no case satisfies the given expression, then the switch statement executes the default case. In Swift, we are allowed to implement a switch statement on strings. Syntax switch (expression) { case 1: // Block of code case 2: // Block of code . . . default: // Block of code } Here, the switch statement evaluates the expression and executes only the ... Read More

Swift Program to Implement LinkedList

Ankita Saini
Updated on 13-Jun-2023 17:47:04

1K+ Views

A Linked list is a data structure which is used to store and manage data. It is a sequence of nodes where each not contains two things: data and the reference to the next node in the given sequence. Using a linked list we can easily insert or remove elements from any position inside the list. Linked lists are of two types − Singly-linked list− It moves in only one direction because each node has a reference to the next node. But the next pointer of the last node points to NULL. Doubly linked list− It moves in ... Read More

Advertisements