Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Open the input file
- local input_file = io.open("input.txt", "r")
- -- Read the contents of the file
- local input_text = input_file:read("*all")
- -- Close the input file
- input_file:close()
- -- Set the maximum number of characters per line
- local max_chars_per_line = 50
- -- Split the input text into lines
- local lines = {}
- for line in input_text:gmatch("[^\r\n]+") do
- table.insert(lines, line)
- end
- -- Format each line to the maximum number of characters per line
- for i, line in ipairs(lines) do
- local formatted_line = ""
- local line_length = 0
- for word in line:gmatch("%S+") do
- if line_length + #word > max_chars_per_line then
- formatted_line = formatted_line .. "\n" .. word .. " "
- line_length = #word + 1
- else
- formatted_line = formatted_line .. word .. " "
- line_length = line_length + #word + 1
- end
- end
- lines[i] = formatted_line
- end
- -- Join the formatted lines back into a single string
- local output_text = table.concat(lines, "\n")
- -- Open the output file
- local output_file = io.open("output.txt", "w")
- -- Write the formatted text to the output file
- output_file:write(output_text)
- -- Close the output file
- output_file:close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement