Improvements for Palindrome

This is a discussion on Improvements for Palindrome within the C Programming forums, part of the General Programming Boards category; Objective: Create a simple Palindrome checker program. The program should allow the user to enter a string and check whether ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    17

    Improvements for Palindrome

    Objective: Create a simple Palindrome checker program. The program should allow the user to enter a string and check whether the given string is a palindrome or not.

    My programming level is quite insubstantial so need some criticism.
    Code:
    #include <stdio.h>
    
    int main()
    {
    	char text[100];
    	int i;
    	int fail=0;
    	int counter=0;
    	printf("Enter something to check if it is a palindrome:");
    	
    	if ( (fgets(text,sizeof text, stdin)) != NULL )
    	{
    		for(i=0; text[i] != '\n'; i++)// there is a \n in the end of the text, for loop goes until it hits the \n
    		{
    			if(text[i]==' ')// counts how many characters there are
    			counter--;
    			counter++;
    		}
    	}
    	
    	int k=0;
    	char newText[counter];//array for new text to be stored in without spaces
    
    		for(i=0; text[i] != '\n'; i++)
    		{
    			if( (text[i] !=' ') )// stores every character except spaces
    			{
    				newText[k]=text[i];
    				k++;
    			}
    		}
    	int l=0;
    
    	for (l=0; l<=k;l++)
    	{
    		if(newText[l] != newText[k-l-1])// compares the first and last character and so on
    			{
    				//printf("newText[l:%d]:%c newText[k-l %d] %c\n", l,newText[l],k-l,newText[k-l-1]);
    				fail=1;
    				l=k;
    			}
    	}
    	
    	int m;
    	if( fail==1)
    	{
    		printf("\"");
    		for(m=0;m<counter; m++)// used to avoid the \n int he end of the text array
    			printf("%c",text[m]);
    		printf("\" is not a palindrome.\n");
    	}
    	else
    		{
    		printf("\"");
    		for(m=0;m<counter; m++)
    			printf("%c",text[m]);
    		printf("\" is a palindrome.\n");
    		}
    
    	return 0;
    }

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Do you know about strlen() or are you not allowed to use it?

    Here's one thing about logic and counters: you can reduce the number of operations by using the counter itself as the iterator:
    Code:
    for(count=0; text[count] != '\n'; count++) if (text[count]==' ') count--;
    You should make more use of functions. For example, you could have a function to extract the spaces. Also, if you used a function to check the palidrome, you could get rid of the "fail" flag and just return a value (0 or 1); as soon as it fails, just return 0 from the function.

    Using discrete functions makes for less repetition and less clunky code, eg, code that is easier to modify, use, and optimize. It will usually improve performance in the process, as with the "fail" flag example (using the flag, you must still continuing checking the entire string even after it fails).
    Last edited by MK27; 08-25-2009 at 12:26 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    17
    Awesome, know I shouldn't really post on a thread that's like a week old but need to say thanks. strlen() is what I'm talking about. Reason I didn't use it was because i didn't know about it.

    Also the tip with logic and counters was even more awesome. I just looked at it and didn't realize how eloquent it was compared to what I did.

    Well I didn't use functions because knew that program wouldn't be too long. Eh, guess I should really get in the habit of making use of functions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. See what you think and suggested improvements
    By Game in forum C++ Programming
    Replies: 6
    Last Post: 11-28-2008, 01:31 PM
  2. Improvements on Recursive Directory Listing
    By laney69er in forum C Programming
    Replies: 1
    Last Post: 03-15-2006, 01:39 AM
  3. Kitchen Timer sugestions for improvements
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 12-08-2005, 11:53 AM
  4. Newbie program - improvements?
    By -JM in forum C++ Programming
    Replies: 9
    Last Post: 06-26-2005, 07:53 PM
  5. improvements
    By gamer4life687 in forum C++ Programming
    Replies: 11
    Last Post: 07-21-2003, 04:53 PM

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