Project Euler 22

This is a discussion on Project Euler 22 within the C Programming forums, part of the General Programming Boards category; I'm having a bit of an issue with my solution to Euler problem 22. I have a list of names ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    24

    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.

    Code:
    /* Project Euler problem 22
     *
     * http://projecteuler.net/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') 
    	{
    		if(isalpha(buff[i]))
    		{
    			p[j] = buff[i];
    			j++;
    			i++;
    		}
    
    		else
    		{
     			p[j] = '\n';
    			j++;
    			do
    			{
    				i++;
    			} while ( (isalpha(buff[i]) == 0) && (buff[i] != '\0') );
    		}
    	}
    
    	free(buff);
    
    	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);
    
    	rewind(fp);
    	fread(buff, 1, size, fp);
    	fclose(fp);
    
    	return buff;
    }
    
    int main(void)
    {
    	char *p = format(rfile());
    	puts(p);
    
    	alpha_sort(p);
    
    	return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,823
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 10:00 PM
  2. Project Euler Problem 8
    By deadrabbit in forum C Programming
    Replies: 2
    Last Post: 10-06-2011, 11:56 PM
  3. project euler challenge 11
    By 4play in forum C Programming
    Replies: 3
    Last Post: 07-13-2010, 03: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, 03:59 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21