Tuesday, October 8, 2024
HomeGolangGo program to switch a textual content contained in a .txt file...

Go program to switch a textual content contained in a .txt file – Code Evaluation


Hey to everybody. I actually need assistance. I must create a program that modifies the textual content in a file in a number of methods. I’ve already created the splitwhitespace perform,the touppercase perform,the tolowercase perform,the tocapitalize perform,the hexToDecimal perform and the binToDecimal perform however I don’t know learn how to transfer ahead I’m a newbie.
the directions are as follows:
The device you’re about to construct will obtain as arguments the title of a file containing a textual content that wants some modifications (the enter) and the title of the file the modified textual content ought to be positioned in (the output). Subsequent is an inventory of potential modifications that your program ought to execute:

  • Each occasion of (hex) ought to exchange the phrase earlier than with the decimal model of the phrase (on this case the phrase will at all times be a hexadecimal quantity). (Ex: “1E (hex) recordsdata have been added” → “30 recordsdata have been added”)
  • Each occasion of (bin) ought to exchange the phrase earlier than with the decimal model of the phrase (on this case the phrase will at all times be a binary quantity). (Ex: “It has been 10 (bin) years” → “It has been 2 years”)
  • Each occasion of (up) converts the phrase earlier than with the Uppercase model of it. (Ex: “Prepared, set, go (up) !” → “Prepared, set, GO !”)
  • Each occasion of (low) converts the phrase earlier than with the Lowercase model of it. (Ex: “I ought to cease SHOUTING (low)” → “I ought to cease shouting”)
  • Each occasion of (cap) converts the phrase earlier than with the capitalized model of it. (Ex: “Welcome to the Brooklyn bridge (cap)” → “Welcome to the Brooklyn Bridge”)
    • For (low), (up), (cap) if a quantity seems subsequent to it, like so: (low, <quantity>) it turns the beforehand specified variety of phrases in lowercase, uppercase or capitalized accordingly. (Ex: “That is so thrilling (up, 2)” → “That is SO EXCITING”)
  • Each occasion of the punctuations ., ,, !, ?, : and ; ought to be near the earlier phrase and with area aside from the following one. (Ex: “I used to be sitting over there ,after which BAMM !!” → “I used to be sitting over there, after which BAMM!!”).
    • Besides if there are teams of punctuation like: ... or !?. On this case this system ought to format the textual content as within the following instance: “I used to be considering … You have been proper” → “I used to be considering… You have been proper”.
  • The punctuation mark ' will at all times be discovered with one other occasion of it and they need to be positioned to the proper and left of the phrase in the course of them, with none areas. (Ex: “I’m precisely how they describe me: ’ superior ‘” → “I’m precisely how they describe me: ‘superior’”)
    • If there are a couple of phrase between the 2 ' ' marks, this system ought to place the marks subsequent to the corresponding phrases (Ex: “As Elton John stated: ’ I’m probably the most well-known gay on the earth ‘” → “As Elton John stated: ‘I’m probably the most well-known gay on the earth’”)
  • Each occasion of a ought to be changed into an if the following phrase begins with a vowel (a, e, i, o, u) or a h. (Ex: “There it was. A wonderful rock!” → “There it was. A tremendous rock!”).

  • that is what I used to be in a position to do

package deal most important

import (

“strconv”

“strings”

)

// TODO: Take into account dealing with main ‘0x’ and ‘OX’

func hexToDecimal(str string) string {

worth, _ := strconv.ParseUint(str, 16, 64)

return strconv.FormatUint(worth, 10)

}

func binToDecimal(str string) string {

worth, _ := strconv.ParseUint(str, 2, 64)

return strconv.FormatUint(worth, 10)

}

// TODO: This capitalises after each whitespace or hyphen. Identical as toLower Case.

func toUpperCase(str string) string {

return strings.ToUpper(str)

}

// TODO: This capitalises after each whitespace or hyphen. Identical as toUpperCase.

func toLowerCase(str string) string {

return strings.ToLower(str)

}

func capitalize(str string) string {

return strings.Title(str)

}

Hello :slight_smile: ! Studying your request, you’re asking for an enormous work to do. What you need here’s a Scanner device, which parses your textual content into tokens, makes use of a lookahead perform to see if the following token is an effective match for one in all your instances (controlling the potential instances right into a choose assertion for instance at every iteration, principally seeing the earlier token/s) and if it finds an accurate match than replaces the textual content and appends it to the right stream to jot down into the ultimate file. You may assume to jot down it utilizing regex and it will be most likely computational costly and soiled, a pure hell in some instances. It’s not advanced and may be carried out in a clear approach with goroutines, however requires time! I’ll attempt that can assist you if I’ve time, however first attempt to assume one thing like this by your self, following this as thought.



1 Like

Thanks very a lot on your assist, as I stated I’m an actual newbie so it will be a pleasure to have you ever assist me perceive higher.

Your program argument takes two recordsdata, allow us to name them (enter.txt, output.txt)…
Your enter file accommodates the bin, hex, low, and so on that you must take care of.
Don’t be discouraged.
Break it down.
Think about you’ve gotten one line in your enter.txt. And that line is “It has been 10 (bin) years”. You need your output.txt to comprise “It has been 2 years” after you run this system.
How would you method and resolve this?
learn file (enter.txt)
your operations…what you must do…(your features)
write file (output.txt)
Hope that helps. Preserve coding.

I’ve the identical downside, did you get the answer

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments