Although I was tempted to use perl for this exercise, I do want to continue to use a new language and become more familiar with OO programming. So here is another ruby entry:
Code:#!/usr/bin/ruby class Language def initialize (file_in, file_out) @in = File.new(file_in, 'r+') @out = File.new(file_out, 'w') @in_count @out_count = 1 end def process @in.each_line do |line| @in_count, name = line.chomp.split('. ') if name =~ /^(H|S)/ @out.puts @out_count.to_s + '. ' + name @out_count += 1 end end end def add_entry (name) @in_count = @in_count.to_i + 1 @in.puts @in_count.to_s + '. ' + name end end language = Language.new('bhaarat.text', 'out.text') language.process language.add_entry('English') puts "Done!"
Here is my version of the program written in C.
Compile using gcc:PHP Code:
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
/* file pointer declaration */
FILE *fileout, *filein;
/* variables declaration */
short end_of_file = 0;
char line_num[10];
char Lang[30];
/* start of code */
if(argc != 3)
{
printf("Usage:\n");
printf("ch3 <input file> <output file>\n");
return 1;
}
/* open file */
if((filein = fopen( argv[1], "r" ) ) == NULL)
{
printf("Cannot Open Input file %s\n", argv[1]);
return 1;
}
/* open file for writing */
if((fileout = fopen( argv[2], "w" ) ) == NULL)
{
printf("Cannot Open Output file %s\n", argv[2]);
return 1;
}
/* assume the file is populated */
end_of_file = 1;
/* start scanning in all the lines from the file using fscanf
fscanf returns 0 when EOF is encountered */
while( end_of_file == 1 )
{
end_of_file = fscanf(filein, "%s", &line_num);
end_of_file = fscanf(filein, "%s", &Lang);
/* scan each Country to see if there is an "H" or "S" as their first letter */
if( Lang[0] == 'H' || Lang[0] == 'S')
{
fprintf(fileout, "%s %s\n", line_num, Lang);
}
} /* end while */
/* close files */
fclose(filein);
fclose(fileout);
return (0);
}
You run the program by doing the following:Code:gcc -o challenge3 -challenge3.c
I have compiled the program and it gives you line numbers in which the 'H' or 'S' occur as the first letter of the language. Of course choose out.txt for the output filename.Code:challenge3 <input filename with path> <output file with path>
Cheers.
Last edited by stchman; August 9th, 2008 at 11:30 AM.
Windows, only good for gaming.
do we have to create a new text file for the output in the program or is that done manually
also do you mean save it as bhaarat.text or bhaarat.txt?
Last edited by jimi_hendrix; August 9th, 2008 at 12:31 PM.
Last edited by bobbocanfly; August 10th, 2008 at 02:33 PM.
Today we have 15 minutes of fame, tomorrow 15 minutes of anonymity.
My Blog | Veza - Opensource TinyURL clone with sensible URLs
Last edited by ghostdog74; August 9th, 2008 at 12:58 PM.
Python
A nice exercise for a language I'm just beginning to learn. This script prints line numbers into the output file and, as a special feature, prevents the input file to append "23. English" more than once (if you, for example, run the script twice consecutively).
Released into Public Domain.
EDIT: Changed filenames (bhaarat.txt, out.txt) to the correct ones (bhaarat.text, out.text)!
PHP Code:
#!/usr/bin/env python
# Beginner Programming Challenge 3
#
# Copyright (C) 2008 Eugenio M. Vigo (aka "nvteighen")
#
# Released into the public domain in countries where this is legally possible.
# Where not, I grant explicit permission to use this work for any purpose, in
# any medium with no conditions.
try:
inputfile = open("bhaarat.text", "r+")
except IOError:
print "Ouch! File doesn't exist!"
quit()
outputfile = open("out.text", "w")
out_line_num = 0
for line in inputfile:
line_num, line_string = line.split(". ")
if line_string[0] == "H" or line_string[0] == "S":
out_line_num += 1
outputfile.write(str(out_line_num) + ". " + line_string)
outputfile.close()
inputfile.seek(0, 2) # Go to the file's end.
if int(line_num) < 23: # Don't repeat English if already included.
inputfile.write("23. English\n")
inputfile.close()
Last edited by nvteighen; August 12th, 2008 at 02:58 PM.
Code:cat bhaarat.text | tr -d [1-9.' '] | grep -E 'H|S' > out.text && echo "23. English" >> bhaarat.text
My C version. For the sake of learning, I used only the low-level functions (read/write) for file operation. Obviously, fscanf/fprintf would have been more efficient here.
It's funny to compare it to the Bash one-liner above I guess now you know why it's so important to know your way around it.PHP Code:
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#define MAX_LINE_LENGTH 50
int readNextLine(int fd, char* nextLine)
{
/* Reads the next line from the file pointed by the descriptor fd
* and stores it at the location pointed by nextLine, including the
* new line character (if any).
* Returns zero on success, non-zero on failure.
*/
int ok = 0, curPos = 0;
do
{
char* nextChar = calloc(1, sizeof(char));
ok = read(fd, nextChar, sizeof(char));
if(ok == -1)
perror("read");
nextLine[curPos] = *nextChar;
if(*nextChar == '\n')
{
free(nextChar);
return 0;
}
curPos += 1;
free(nextChar);
}
while(ok != 0);
return -1;
}
int processLine(char* inputLine, int lineNumber)
{
char* languageName = calloc(MAX_LINE_LENGTH, sizeof(char));
int tmp = 0;
int ok;
sscanf(inputLine, "%u. %s", &tmp, languageName);
if(languageName[0] == 'H' || languageName[0] == 'S')
{
sprintf(inputLine, "%u. %s\n", lineNumber, languageName);
ok = 0;
}
else
ok = 1;
free(languageName);
return ok;
}
int main(void)
{
int inputFile, outputFile;
inputFile = open("bhaarat.text", O_RDWR);
if(inputFile == -1)
{
perror("open");
exit(-1);
}
outputFile = open("out.text", O_WRONLY | O_CREAT, 0644);
if(outputFile == -1)
{
perror("open");
exit(-1);
}
int curLineNumber = 1;
char* nextLine = calloc(MAX_LINE_LENGTH, sizeof(char));
while(!readNextLine(inputFile, nextLine))
{
if(!processLine(nextLine, curLineNumber))
{
write(outputFile, nextLine, strlen(nextLine)*sizeof(char));
curLineNumber += 1;
}
}
sprintf(nextLine, "%s", "23. English\n");
write(inputFile, nextLine, strlen(nextLine)*sizeof(char));
close(inputFile);
close(outputFile);
free(nextLine);
return 0;
}
Last edited by Bachstelze; August 9th, 2008 at 07:15 PM.
Bookmarks