16.3k views
5 votes
Writa function to read content from "Zone. txt" and write it into another file "zone1. txt" by reversing each line if the line starts with A​

User HavanaSun
by
5.4k points

1 Answer

3 votes

Answer:

You have specified no language. Here's an algorithm to follow with most platforms, my example is in NodeJS

const fs = require('fs');

let zone = fs.readFile('Zone.txt', 'utf8', function (err, data) {

if (err) throw new Error(err);

data = data.toString(); //convert the buffer to a string

let lines = data.split("\\"),

content = [];

for (i in lines) {

let l = lines[i];

if (l.toLowerCase().startsWith("a")) l = reverseString(l);

content.push(l);

}

content = content.join("");

fs.writeFile('zone1.txt', content, , function (err) {

if (err) throw new Error(err);

}

});

function reverseString (str) {

return str.split("").reverse().join("");

}

User Bgplaya
by
5.1k points