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


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 = "231"
newLength = 5 Output: 23100

Here, we pad the original string with two zeros on the right side.

Algorithm

  • Step 1 − Create a function which takes the original string and the length of the new string as arguments.

  • Step 2 − Inside the function first create a copy of the original string.

  • Step 3 − Run a while call append() function to pad zeros on the right side of the input string till the inputStr.count <strLength.

  • Step 4 − Return the new string.

  • Step 5 − Create a string.

  • Step 6 − Call the function and pass a string and the size of the new string.

  • Step 7 − Display the result.

Example 1

In the following Swift program, we will pad a string with 0’s on the right side. So, create a function which takes a string and the total length of the padded string as arguments. Then the function creates a copy of the input string and appends “0” on the right side until the length of the padded string is equal to the given length and then it will return the resultant string.

import Foundation
import Glibc

func padString(str: String, strLength: Int)-> String{
   var inputStr = str
   while inputStr.count<strLength{
      inputStr.append("0")
   }
   return inputStr
}

let s = "8758"
let resultStr = padString(str:s, strLength:10)
print("Padded String:", resultStr)

Output

Padded String: 8758000000

Example 2

In the following Swift program, we will pad a string with 0’s on the right side. So, create a function which takes a string and the total length of the padded string as arguments. Then the function creates a copy of the input string and add 0’s on the right side of string using += operator until the length of the padded string is equal to the given length and then it will return the resultant padded string with 0’s on right side.

import Foundation
import Glibc

func padString(inputStr: String, strlength: Int) -> String {
   var paddedStr = inputStr
   while paddedStr.count < strlength {
      paddedStr += "0"
   }    
   return paddedStr
}

// Example usage
let myString = "8848"
let resString = padString(inputStr:myString, strlength: 17)
print("Original String:", myString)
print("Padded string with zeros:", resString)

Output

Original String: 8848
Padded string with zeros: 88480000000000000

Method 2: Using Pre-defined Function

In Swift, we can pad a string with zeros on the right side using padding() function. This function returns a new string by removing characters from the end of the specified string or appending characters from the specified position and times in the given string.

Syntax

func padding(toLength: Int, withPad: String, startingAt: Int)

Here, the padding() function takes three parameters −

  • toLength − total length of the new string.

  • withPad − string which you want to pad in the given string.

  • startingAt − index from which the padding starts.

Example

In the following Swift program, we will pad a string with 0’s on the right side. So, create a string then use a padding() function to pad 0’s on the right side of the given string and display the final output.

import Foundation
import Glibc

let enteredString = "4526"
let paddedStr = enteredString.padding(toLength: 9, withPad: "0", startingAt: 0)
print("Original String:", enteredString)
print("Padded String:", paddedStr) 

Output

Original String: 4526
Padded String: 452600000

Conclusion

So this is how we can pad a string with 0’s on the right side. Using both methods you can pad as many as zeros or any other character you want on the right side of the string. Both methods work efficiently. You can use these methods when you want to add a specified character multiple times at the end or the start of the given string.

Updated on: 14-Jun-2023

432 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements