The program reads a text file, applies several formatting and transformation rules and writes the modified text into another file.
This project focuses on:
- File handling in Go
- String manipulation
- Number conversion
- Text formatting
The program takes two arguments:
- Input – the file containing the original text [sample.txt]
- Output – the file where the modified text will be written[result.txt]
go run . sample.txt result.txtIf (hex) appears after a word, the previous word is treated as a hexadecimal number and converted to decimal.
Example:
1E (hex) files were addedOutput:
30 files were addedIf (bin) appears after a word, the previous word is treated as a binary number and converted to decimal.
Example:
It has been 10 (bin) yearsOutput:
It has been 2 yearsConverts the previous word to uppercase.
Example:
go (up)
Output:
GO
Converts the previous word to lowercase.
Example:
HELLO (low)
Output:
hello
Capitalizes the first letter of previous word.
Example:
hello (cap)
Output:
Hello
Syntax:
(up, n)
(low, n)
(cap, n)
Example:
This is amazing (up, 2)
Output:
This IS AMAZING
Punctuation marks:
. , ! ? : ;
Rules:
- Punctuation attaches to the previous word
- There must be one space after punctuation
Example:
Hello ,world !
Output:
Hello, world!
Groups like:
...
!?
Remain together and attach to the previous word.
Example:
I was thinking ... You were right
Output:
I was thinking... You were right
Single quotes ' ' should wrap words without spaces inside.
Example:
' awesome '
Output:
'awesome'
Example with multiple words:
' I love Go '
Output:
'I love Go'
The word a becomes an when the next word starts with:
a, e, i, o, u, h
Example:
A amazing rock
Output:
An amazing rock
- Create an input file:
sample.txt
- Run the program:
go run . sample.txt result.txt- Check the output:
cat result.txt