The Code Marathon. (1 Viewer)

parad0xica

Active Member
Joined
Mar 24, 2016
Messages
204
Gender
Male
HSC
N/A
Question: write a program to count the number of words in a text file.
 

FlyingKanga

The optimistic pessimist.
Joined
Dec 2, 2012
Messages
410
Gender
Male
HSC
2015
Question: write a program to count the number of words in a text file.
Java:
Code:
import java.util.*;
import java.io.*;
public class wordCount {
	public static void main(String[] args) {
		File newFile = new File(args[0]);
		int count = 0;
		try {
			Scanner scan = new Scanner(newFile);
			while (scan.hasNext()) {
				String word = scan.nextLine();
				String[] words = word.split("\\s+");
				count += words.length;
			}
		} catch(Exception err) {
			System.out.println("Not found");
		}

		System.out.println("Total words: " + count);	

		}
	}
 

Drsoccerball

Well-Known Member
Joined
May 28, 2014
Messages
3,650
Gender
Undisclosed
HSC
2015
Question: write a program to count the number of words in a text file.
C code :

Code:
#include <stdio.h>
#define MAX_CHARACTERS 1000
#define MAX_LINE 100

int main(int argc, char *argv[]){
	FILE *stream = fopen(argv[1], "r");

	if(stream == NULL){
		printf("The file %s doesn't exist.\n", argv[1]);
		return 1;
	}
	char file[MAX_LINE][MAX_CHARACTERS];
	int j = 0;
	while(fgets(file[j], MAX_CHARACTERS, stream) != NULL){
		j++;
	}
	int i = 0;
	j = 0;
	int character = 0;

	while(file[j][i] != '\0'){
		while(file[j][i] != '\0'){
			if((file[j][i] >= 'a' && file[j][i] <= 'z') || (file[j][i] >= 'A' && file[j][i] <= 'Z')){
				character++;
			}
			i++;
		}
		i = 0;
		j++;
	}
	printf("The text file %s has %d characters.\n", argv[1],character);
	fclose(stream);
	return 0;
}
 

Drsoccerball

Well-Known Member
Joined
May 28, 2014
Messages
3,650
Gender
Undisclosed
HSC
2015
Write a method/program which displays Pascal's triangle up to n rows, n a given positive integer.
Do this but with letters.
./triangle 3
A
BCB
DEFED
(Click reply to quote to see how the triangle should look)
When the letter reaches Z loop around.
 

parad0xica

Active Member
Joined
Mar 24, 2016
Messages
204
Gender
Male
HSC
N/A
C code :

Code:
#include <stdio.h>
#define MAX_CHARACTERS 1000
#define MAX_LINE 100

int main(int argc, char *argv[]){
	FILE *stream = fopen(argv[1], "r");

	if(stream == NULL){
		printf("The file %s doesn't exist.\n", argv[1]);
		return 1;
	}
	char file[MAX_LINE][MAX_CHARACTERS];
	int j = 0;
	while(fgets(file[j], MAX_CHARACTERS, stream) != NULL){
		j++;
	}
	int i = 0;
	j = 0;
	int character = 0;

	while(file[j][i] != '\0'){
		while(file[j][i] != '\0'){
			if((file[j][i] >= 'a' && file[j][i] <= 'z') || (file[j][i] >= 'A' && file[j][i] <= 'Z')){
				character++;
			}
			i++;
		}
		i = 0;
		j++;
	}
	printf("The text file %s has %d characters.\n", argv[1],character);
	fclose(stream);
	return 0;
}
My bad. I should have defined what a word means. A word is a string containing no spaces.

"Mathematics is fun. Have a nice day, G." contains 8 words
 

turntaker

Well-Known Member
Joined
May 29, 2013
Messages
3,908
Gender
Undisclosed
HSC
2015
Create a compiler that compiles a programming language you made up.
 

Drsoccerball

Well-Known Member
Joined
May 28, 2014
Messages
3,650
Gender
Undisclosed
HSC
2015
My bad. I should have defined what a word means. A word is a string containing no spaces.

"Mathematics is fun. Have a nice day, G." contains 8 words
Im pretty sure my program does this? I accidentally wrote 'characters' instead of 'words' though.

EDIT: brb
 

Drsoccerball

Well-Known Member
Joined
May 28, 2014
Messages
3,650
Gender
Undisclosed
HSC
2015
My bad. I should have defined what a word means. A word is a string containing no spaces.

"Mathematics is fun. Have a nice day, G." contains 8 words
My code assumes that only valid inputs are given :p.
Code:
#include <stdio.h>
#include <ctype.h>
#define MAX_CHARACTERS 1000
#define MAX_LINE 100

int main(int argc, char *argv[]){
	FILE *stream = fopen(argv[1], "r");

	if(stream == NULL){
		printf("The file %s doesn't exist.\n", argv[1]);
		return 1;
	}
	char file[MAX_LINE][MAX_CHARACTERS];
	int j = 0;
	while(fgets(file[j], MAX_CHARACTERS, stream) != NULL){
		j++;
	}
	int i = 0;
	j = 0;
	int character = 0;

	while(file[j][i] != '\0'){
		while(file[j][i] != '\0'){
			if(((file[j][i] >= 'a' && file[j][i] <= 'z') || (file[j][i] >= 'A' && file[j][i] <= 'Z'))){
				if(isspace(file[j][i + 1]) != 0){
					character++;
				}
			}
			else if(((file[j][i - 1] >= 'a' && file[j][i - 1] <= 'z') || (file[j][i - 1] >= 'A' && file[j][i - 1] <= 'Z'))){
				if(isspace(file[j][i + 1]) != 0){
					character++;
				}
			}
			i++;
		}
		i = 0;
		j++;
	}
	printf("The text file %s has %d words.\n", argv[1],character);
	fclose(stream);
	return 0;
}
 

parad0xica

Active Member
Joined
Mar 24, 2016
Messages
204
Gender
Male
HSC
N/A
That's better but it still fails for a text-file containing:

Mathematics is fun. Have a nice day, G.

Your program produces 7.
 

Drsoccerball

Well-Known Member
Joined
May 28, 2014
Messages
3,650
Gender
Undisclosed
HSC
2015
That's better but it still fails for a text-file containing:

Mathematics is fun. Have a nice day, G.

Your program produces 7.
I don't want to abuse the if statements so ill leave my program broken :p
 

FlyingKanga

The optimistic pessimist.
Joined
Dec 2, 2012
Messages
410
Gender
Male
HSC
2015
That's better but it still fails for a text-file containing:

Mathematics is fun. Have a nice day, G.

Your program produces 7.
Mine works right? It just splits each word whenever there's a space in between. But then it'll have day, as a word .-.
 

KingOfActing

lukewarm mess
Joined
Oct 31, 2015
Messages
1,016
Location
Sydney
Gender
Male
HSC
2016
I'm not quite sure what you mean

To do Key-Value mappings I'd just use a HashMap, why recreate the wheel? Also why the "no method" restriction? Do you mean no static methods or no methods at all? None at all means you can't even print anything... Unless you meant no creation of ADDITIONAL methods, in which case the following (awful) code would fit all the requirements:

Code:
public class Entry
{
    public final K key;
    public final V value; 

    public Entry(K key, V value) {
        this.key = key;
        this.value = value;
    }
}
Then your "app" would have an array of these, it would cycle through them and compare keys to the input key, and once a match is found the value is printed.

This implementation is not OO though. The key idea of OOP is that objects aren't just 'things' that hold data - objects are defined by behaviour and well, this object has none. :p not to mention it's completely inefficient - like I said, don't reinvent the wheel!

Why am I not studying for my trial tomorrow?
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Top