I want to create a program that converts the odd number of characters in the string into minus one ASCII code.

Asked 2 years ago, Updated 2 years ago, 33 views


Ask code for all characters in the string I could create a program to convert to minus 1 characters, but I couldn't create a program to convert the odd number of characters.I would like you to tell me what to do.Thank you for your cooperation.

swift ios

2022-09-30 16:37

1 Answers

This is a sample code that assumes ASCII code only and starts with 0 (0 origin) and starts with -1 odd characters only.

let str="abcdef"
vars=""
for(i,ch)instr.utf8.enumerated(){
    ss.append(String(UnicodeScalar(i%2==1&ch>0?ch-1:ch)))
}
print(ss)//"aaccee"


2022-09-30 16:37

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.