import Foundation
import Glibc
print("hello world!")
//---------------String--------------------
//câu 1
/*var str : String = "Nguyen Hong Toi"
func strLength(_ s:String)->Int{
return str.characters.count
}
print("độ dài chuỗi là \(strLength(str))")
//câu 2
var str1 :String = "Nguyễn "
var str2 :String = "Hồng Tới"
func strAdd(_ str1:String,_ str2:String)->String{
return str1.appending(str2)
}
print("chuỗi mới sau khi nối là \(strAdd(str1,str2))")
//câu 3
var s = strAdd(str1,str2)
func strUpper(_ s:String)->String{
return s.uppercased()
}
print("Chuỗi in hoa là \(strUpper(s))")
//câu 4
//câu 5
//câu 6
func insertStartString(_ s1:String)->String{
return "nlhdung" + s1
}
print("Chuỗi sau khi chèn nhldung vào đầu chuỗi là \(insertStartString(s))")
//câu 7
func insertEndString(_ s2:String)->String{
return s2 + "nlhdung"
}
print("Chuỗi sau khi chèn nhldung vào cuối chuỗi là \(insertEndString(s))")
//câu 8
func insertPosString(_ s3: inout String,_ pos:Int)->String{
s3.insertContentsOf("nhldung".characters ,at: pos)
return s3
}
print("Chuỗi chèn tại vị trí x là \(insertPosString(s,3))")*/
//câu 9
//câu 10
//-------------For and Array-------------------
//câu 1
var arr:[Int] = [1,3,2,4,5]
func reverseArr(_ a:inout [Int]) -> [Int]{
let temp = a.count - 1
for i in 0...temp/2{
a.swapAt(temp - i,i)
}
return a
}
print("\(reverseArr(&arr))")
//câu 2
func soUocChung(_ n:Int)->(slUocSo:Int, tongUocSo:Int){
var dem = 0
var sum = 0
for i in 1...n{
if(n % i == 0)
{
dem = dem + 1
sum = sum + i
}
}
return (dem,sum)
}
var n = 12
print("\(soUocChung(n))")
//câu 3
func demChuSo(_ n:Int)->(slChuSo:Int,tongChuSo:Int){
var dem = 0
var sum = 0
var k = n
repeat{
dem = dem + 1
sum = sum + (k%10)
k=k/10
}while(k > 0)
return (dem,sum)
}
var m = 12345
print("\(demChuSo(m))")
//câu 4
func demChanLe(_ n:Int)->(slSoChan:Int,slSoLe:Int){
var demChan = 0
var demLe = 0
var k = n
while(k > 0){
if((k/10) % 2 != 0){
demChan = demChan + 1
}
else {
demLe = demLe + 1
}
k=k/10
}
return (demChan,demLe)
}
print("\(demChanLe(m))")
//câu 5
var a = 3
func tinhGiaTri(_ n:Int)->Int{
var sum = 0
for i in 1...n{
sum = sum + i*i
}
return sum
}
print("S = \(tinhGiaTri(a))")
//câu 6
var chuoi = "nlhdung"
func taoChuoi(_ s: String)->[String]{
var str = [String](repeating: s , count:100)
for i in 0..<100{
str[i]=str[i] + "-" + String(i + 1)
}
return str
}
print("Chuỗi 100 kí tự là \(taoChuoi(chuoi))")
//câu 7
func sortIncreasing(_ a:inout [Int])->[Int]{
return a.sorted()
}
print("\(sortIncreasing(&arr))")
//câu 8
func sortDecreasing(_ a:inout [Int])->[Int]{
let b = a.sorted(by: {
(_ i:Int,_ j:Int)->Bool in
return i > j
})
return b
}
print("\(sortDecreasing(&arr))")
//câu 9
func sortByK(_ a:inout [Int],_ k:Int)->[Int]{
if(k != 0){
return a.sorted()
}
else{
return a.sorted(by: {
(_ i:Int,_ j:Int)->Bool in
return i > j
})
}
}
var k = 1 //0: sắp xếp giảm dần, 1: sắp xếp tăng dần
print("Sắp xếp các phần tử trong mảng theo k = \(k) thì \(sortByK(&arr,k))")
//câu 10
var b=3
func ktraDoiXung(_ n:Int){
var temp = 0
var k = n
var a = 0
while(k != 0)
{
a = k%10
temp = temp*10 + a
k = k/10
}
if(temp == n){
print("Là số đối xứng")
}
else{
print("Không phải là số đối xứng")
}
}
ktraDoiXung(b)
//câu 11
func ktraSoNguyenTo(_ n:Int){
let k = Int(sqrt(Double(n)))
if(n < 2)
{
print("Không phải là số nguyên tố")
}
for i in 2...k{
if(n % i == 0){
print("Không phải là số nguyên tố")
}
else{
print("Là số nguyên tố")
}
}
}
ktraSoNguyenTo(b)
//câu 12
/*var mang: [Int]=[1,2,7,5,4,6,8,10,3,9]
func timGiaTriMang(_ a:[Int])->(gtMax:Int,gtMaxChan:Int,gtMaxLe:Int,gtMaxSnt:Int){
var m = a.max
var mc = 0
var ml = 0
var msnt = 0
return (m,mc,ml,msnt)
}
print("\(timGiaTriMang(mang))")*/
//-------------Khác--------------
//câu 1
/*var km = 130
func tinhTienTaxi(_ n:Int)->Double{
var sum = 0.0
switch n{
case 1:
sum = sum + 15000
fallthrough
case 2...5:
sum = sum + 13500
fallthrough
case 6..<120:
sum = sum + 11000
fallthrough
case 120...10000:
sum = sum*0.9
fallthrough
default:
return 0
}
return sum
}
print("\(tinhTienTaxi(km))")*/
//câu 2
//câu 3
//câu 4
About Online Swift Compiler
Try our Online Swift Compiler (Version Swift v5.7.3) to Edit, Run, and Share your Swift Code directly from your browser. This online development environment provides you the latest version Swift v5.7.3.
How to use Online Swift Compiler?
Write and Execute Code
- Write your program (or, paste it) directly under the "Source Code" tab.
- If you want to save your program, go to the "Project" menu and save it.
- You can directly execute your program without saving it by clicking on on "Execute" button.
User Input
The latest version of Coding Ground allows to provide program input at run time from the termnial window exactly the same way as you run your program at your own computer. So simply run a program and provide your program input (if any) from the terminal window available in the right side.
Online Swift Compiler: Keyboard Shortcuts
The following are the keyword shortcut of this Online Swift Compiler:
Shortcut | Description |
⌘ + Enter | Run the program |
⌘ + S | Save Project (Login Required) |
⇧ + ⌘ + S | Save As Project |
⌘ + P | New Project |
⌘ + G | Share Project |
⌘ + Z | Undo Editing |
⌘ + Y | Redo Editing |
⌘ + A | Select All Text |
⌘ + X | Cut Selected Text |
⌘ + C | Copy Selected Text |
⌘ + V | Paste Copied Text |
⌘ + F | Search Text |
⌘ + ⌥ + F | Replace Text |
Shortcut | Description |
Ctrl + Enter | Run the program |
Ctrl + S | Save Project |
Shift + Ctrl + S | Save As Project |
Ctrl + G | Share Project |
Ctrl + Z | Undo Editing |
Ctrl + Y | Redo Editing |
Ctrl + A | Select All Text |
Ctrl + X | Cut Selected Text |
Ctrl + C | Copy Selected Text |
Ctrl + V | Paste Copied Text |
Ctrl + F | Search Text |
Ctrl + H | Replace Text |
Online Swift Compiler: Save and Share Swift Code (Project)
Save Swift Project Online
You can save your Swift Project with us so that you can access this project later on. To save a project you will need to create a login Id with us. So before you save a project, please create a login Id using a link given at the top right corner of this page.
Share Swift Project Online
You can use this feature to share your Swift Code with your teachers, classmates and colleagues. Just click Share Button and it will create a short link, which can be shared through Email, WhatsApp or even through Social Media. A shared link will be deleted if it has been passive for almost 3 months.
More Features of Online Swift Compiler
- Theme – You can change the current editor's theme from the "Editor Theme" option under "Settings" menu.
- Font Size – You can change the font size of the editor /compiler from from the "Font Size" option under "Settings" menu.
- Tab Size – You can change the tab size from the "Tab Size" option under "Settings" Menu.
- Show/Hide Line Numbers – You can show/hide the line number with the code from the "Show Line Numbers" or "Hide Line Numbers" option under "Settings" Menu.
- And, many more.
Benefits of Using Online Swift Compiler
There are several benefits of using the Online Swift Compiler to run your Swift code:
- Platform independence: You can run your code from any device without taking care of operating systems.
- Convenience: You don't need to install anything for using this.
- No setup required: There is no need for additional setup to run your code.
- Updated version: Our online compiler/editors/terminals are the latest up-to-date.