Thread: Project Euler 22

  1. #1
    Registered User
    Join Date
    Aug 2011

    Project Euler 22

    I'm having a bit of an issue with my solution to Euler problem 22. I have a list of names that are surrounded by quotation makes and separated by commas (I.E. "MARY",). I have created a function (called format) which removes the quotation marks and commas and builds a new array with the names, delimiting them with line feeds (I.E. MARY\n). When I try printing this list with puts(), I get a formatted list as expected, but at the end all of the unformatted names also appear. Obviously, I would like to have only the formatted list returned. If anybody could offer some advice, I would be obliged.

    /* Project Euler problem 22
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #define MAX_NAMES	6000
    void alpha_sort(char *buff)
    	const char alpha[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    	char *p;
    	int i;
    	// array of char pointers
    	char *names[MAX_NAMES];
    	/*names[0] = "test";*/
    	/*p = names[0];*/
    	/*printf("%c\n", p[0]);*/
    char *format(char *buff)
    	int i = 1, j = 0;
    	char *p;
          	p = malloc(sizeof(buff));
    	while(buff[i] != '\0') 
    			p[j] = buff[i];
     			p[j] = '\n';
    			} while ( (isalpha(buff[i]) == 0) && (buff[i] != '\0') );
    	return p;
    char *rfile(void)
    	long size = 0;
    	FILE *fp;
    	fp = fopen("names.txt", "r");
    	fseek(fp, 0, SEEK_END);
    	size = ftell(fp);
    	// allocate memory size of file
    	char *buff = (char*) malloc(sizeof(char) * size);
    	fread(buff, 1, size, fp);
    	return buff;
    int main(void)
    	char *p = format(rfile());
    	return 0;

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > char *buff = (char*) malloc(sizeof(char) * size);
    This doesn't allow for adding a nul character at the end to mark the end of the string.

    > fread(buff, 1, size, fp);
    This won't add the nul character.

    > char *p;
    > p = malloc(sizeof(buff));
    Do you know the difference between sizeof() and strlen() ?

    My guess is you tried strlen() first, but it blew up because of the nul missing problem, so you tried sizeof() instead and got garbage results.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Project Euler problem
    By nimitzhunter in forum C++ Programming
    Replies: 6
    Last Post: 04-15-2012, 09:00 PM
  2. Project Euler Problem 8
    By deadrabbit in forum C Programming
    Replies: 2
    Last Post: 10-06-2011, 10:56 PM
  3. project euler challenge 11
    By 4play in forum C Programming
    Replies: 3
    Last Post: 07-13-2010, 02:09 PM
  4. Project Euler
    By CodeGuru25 in forum C Programming
    Replies: 2
    Last Post: 01-13-2010, 06:25 AM
  5. Project Euler Question
    By Head In Jar Man in forum C++ Programming
    Replies: 6
    Last Post: 04-26-2009, 02:59 PM

Tags for this Thread