Tech Study

Write a Swift program to return the first half of a given string of even length

Introduction

I have used Swift for windows 1.9 for debugging purpose. But you can use any Swift programming language compiler as per your availability.

func first_half_str(_ input: String) -> String {
    let chars = input.characters
    let first_half = chars.prefix(input.characters.count / 2)
 
    return String(first_half)
}
print(first_half_str("Tech"))
print(first_half_str("Study"))

Result

Write a Swift program to return the first half of a given string of even length
Write a Swift program to return the first half of a given string of even length

TaggedWrite a Swift program to return the first half of a given string of even length

Python Examples

Introduction: Python Examples are the basic programming concepts of python like python syntax,python data types,,python operators,python if else,python comments etc.. …

Read more

C String Functions

C String Functions perform certain operations, It provides many useful string functions which can come into action. The <string.h> header …

Read more