Double Double Words
October 13, 2015
Today’s task is to write a program that reads a file and reports any instances of doubled words, which is a useful program for anyone that does a lot of writing, as I do in this blog.
Your task is to write a program to find doubled words. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.
Perfect example where perl rocks!
Python
Alternate Perl solution:
Here’s my Python version:
Uses fileinput from the standard library to handle opening and closing files provided on the command line. It also keeps track of name of the file and line number. Uses regex’s to find the words in a line.
If a repeated word is found, the program prints the word, the line number(s), and a portion of the line(s) surrounding the repeated word for context.
Example output:
string content = File.ReadAllText("file.txt");
string[] words = text.Split(’ ‘, ‘\t’, ‘\n’);
string output = "";
for (int i = 0; i < words.Length – 1; i++)
{
if (words[i] == words[i1])
{
output += "Word Index: " i.ToString() ", "
"Word: " words[i] "\n";
}
}
MessageBox.Show(output);
1. I don’t know to format the text as code,
2. For some reason, the “+” is removed from some line, for example the correct code is: if (words[i] == words[i+1])
3. Why I cannot edit my comment to correct it?! :D