Go program to modify a text contained in a .txt file

Hello to everyone. I really need help. I need to create a program that modifies the text in a file in several ways. I have already created the splitwhitespace function,the touppercase function,the tolowercase function,the tocapitalize function,the hexToDecimal function and the binToDecimal function but I don’t know how to move forward I am a beginner.
the instructions are as follows:
The tool you are about to build will receive as arguments the name of a file containing a text that needs some modifications (the input) and the name of the file the modified text should be placed in (the output). Next is a list of possible modifications that your program should execute:

  • Every instance of (hex) should replace the word before with the decimal version of the word (in this case the word will always be a hexadecimal number). (Ex: “1E (hex) files were added” → “30 files were added”)
  • Every instance of (bin) should replace the word before with the decimal version of the word (in this case the word will always be a binary number). (Ex: “It has been 10 (bin) years” → “It has been 2 years”)
  • Every instance of (up) converts the word before with the Uppercase version of it. (Ex: “Ready, set, go (up) !” → “Ready, set, GO !”)
  • Every instance of (low) converts the word before with the Lowercase version of it. (Ex: “I should stop SHOUTING (low)” → “I should stop shouting”)
  • Every instance of (cap) converts the word before with the capitalized version of it. (Ex: “Welcome to the Brooklyn bridge (cap)” → “Welcome to the Brooklyn Bridge”)
    • For (low), (up), (cap) if a number appears next to it, like so: (low, <number>) it turns the previously specified number of words in lowercase, uppercase or capitalized accordingly. (Ex: “This is so exciting (up, 2)” → “This is SO EXCITING”)
  • Every instance of the punctuations ., ,, !, ?, : and ; should be close to the previous word and with space apart from the next one. (Ex: “I was sitting over there ,and then BAMM !!” → “I was sitting over there, and then BAMM!!”).
    • Except if there are groups of punctuation like: ... or !?. In this case the program should format the text as in the following example: “I was thinking … You were right” → “I was thinking… You were right”.
  • The punctuation mark ' will always be found with another instance of it and they should be placed to the right and left of the word in the middle of them, without any spaces. (Ex: “I am exactly how they describe me: ’ awesome '” → “I am exactly how they describe me: ‘awesome’”)
    • If there are more than one word between the two ' ' marks, the program should place the marks next to the corresponding words (Ex: “As Elton John said: ’ I am the most well-known homosexual in the world '” → “As Elton John said: ‘I am the most well-known homosexual in the world’”)
  • Every instance of a should be turned into an if the next word begins with a vowel (a, e, i, o, u) or a h. (Ex: “There it was. A amazing rock!” → “There it was. An amazing rock!”).
  • this is what I was able to do

package main

import (

“strconv”

“strings”

)

// TODO: Consider handling leading ‘0x’ and ‘OX’

func hexToDecimal(str string) string {

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

return strconv.FormatUint(value, 10)

}

func binToDecimal(str string) string {

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

return strconv.FormatUint(value, 10)

}

// TODO: This capitalises after every whitespace or hyphen. Same as toLower Case.

func toUpperCase(str string) string {

return strings.ToUpper(str)

}

// TODO: This capitalises after every whitespace or hyphen. Same as toUpperCase.

func toLowerCase(str string) string {

return strings.ToLower(str)

}

func capitalize(str string) string {

return strings.Title(str)

}

Hi :slight_smile: ! Reading your request, you’re asking for a huge work to do. What you want here is a Scanner tool, which parses your text into tokens, uses a lookahead function to see if the next token is a good match for one of your cases (controlling the possible cases into a select statement for example at each iteration, basically seeing the previous token/s) and if it finds a correct match than replaces the text and appends it to the correct stream to write into the final file. You could think to write it using regex and it would be probably computational expensive and dirty, a pure hell in some cases. It’s not complex and can be implemented in a clean way with goroutines, but requires time! I’ll try to help you if I have time, but first try to think something like this by yourself, following this as idea.

1 Like

Thank you very much for your help, as I said I am a real beginner so it would be a pleasure to have you help me understand better.

Your program argument takes two files, let us call them (input.txt, output.txt)…
Your input file contains the bin, hex, low, etc that you have to deal with.
Don’t be discouraged.
Break it down.
Imagine you have one line in your input.txt. And that line is “It has been 10 (bin) years”. You want your output.txt to contain “It has been 2 years” after you run the program.
How would you approach and solve this?
read file (input.txt)
your operations…what you have to do…(your functions)
write file (output.txt)
Hope that helps. Keep coding.

ok thanks a lot

I have the same problem, did you get the solution