0
Can someone translate this code from java to swift or explain how it should look like in Swift (SHORT CODE)
import java.util.Scanner; public class RunLengthEncoding { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter input string: "); String s = input.nextLine(); for (int a = 0; a < s.length(); a++) { if ((s.charAt(a) < 'A' || s.charAt(a) > 'Z')) { System.out.print("Bad input"); System.exit(0); } } System.out.print("Enter flag character: "); char flag = input.nextLine().charAt(0); if (flag == '#' || flag == '
#x27; || flag == '*' || flag == '&') { int count = 0; for (int i = 1; i < s.length(); i++) { if(s.charAt(i)==s.charAt(i-1)); count++; if (count == 1) System.out.print(s.charAt(i)); if (count == 2) System.out.print(s.charAt(i) + s.charAt(i)); if (count == 3) System.out.print(s.charAt(i) + s.charAt(i) + s.charAt(i)); else System.out.print(flag + s.charAt(i) + (count + 1)); } } else System.out.print("Bad input"); } }5 odpowiedzi
+ 1
Yes, you can:
1) Complete the Swift course here in Sololearn.
2) Use Google/Bing to search to find answers to questions like 'if statement in Swift'
I found it hard to find information on input is Swift so I will give you a head start:
var message = readLine()!
let sales = Int(readLine()!)!
+ 1
The answer above me is correct however I can’t stress this enough: Force-unwrapping is Unsafe. Apple suggests you use more safe unwrapping techniques such as “if let” and “guard let” For example “guard let message = readLine() else { return }” Thanks.
+ 1
I agree with Pawel Zabinski on this point. Here, on these small codes, in the Sololearn playground, is one thing. But real world robust error handling is the proper way to write production code.
0
import Foundation
print("Enter input string: ", terminator: "")
if let s = readLine() {
for a in s {
if !("A"..."Z" ~= a) {
print("Bad input")
exit(0)
}
}
print("Enter flag character: ", terminator: "")
if let flag = readLine()?.first, ["#", "quot;, "*", "&"].contains(flag) {
var count = 0
var previousChar: Character?
for i in s {
if previousChar == nil {
previousChar = i
count += 1
} else if i == previousChar {
count += 1
} else {
if count == 1 {
print(previousChar!, terminator: "")
} else if count == 2 {
print(String(repeating: previousChar!, count: 2), terminator: "")
} else if count == 3 {
print(String(repeating: previousChar!, count: 3), terminator: "")
} else {
print("\(flag)\(previousChar!)\(count)", terminator: "")
}
previousChar = i
count = 1
}
}
if let previousChar = previousChar {
if count == 1 {
print(previousChar, terminator: "")
} else if count == 2 {
print(String(repeating: previousChar, count: 2), terminator: "")
} else if count == 3 {
print(String(repeating: previousChar, count: 3), terminator: "")
} else {
print("\(flag)\(previousChar)\(count)", terminator: "")
}
}
} else {
print("Bad input")
}
}
The basic logic of the code remains the same, but there are some differences in syntax between Java and Swift. For example, in Swift, you need to use readLine() to read user input from the console, and you need to use optional to handle cases where there is no input. Additionally, you need to use exit(0) to exit the program in Swift. Finally, note that the loop for counting the c