Any ideas why this wont work?

This is a discussion on Any ideas why this wont work? within the C Programming forums, part of the General Programming Boards category; Im trying to run this code... Code: #include <stdio.h> #include <string.h> // Recursive palindrome finder function bool palindrome(char* word, int ...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    4

    Any ideas why this wont work?

    Im trying to run this code...

    Code:
    #include <stdio.h>
    #include <string.h>
    
    // Recursive palindrome finder function
    bool palindrome(char* word, int length);
    
    int main() {
    
    	char word[100];
    
    	printf("Please enter a word to see if it is a palindrome: ");	// Get word from user
    	scanf("%s",&word);
    
    	if((palindrome(word,strlen(word))) == 1) {	// If palindrome returns true, say its a palindorme...
    		printf("%s is a palindrome\n",word);
    	}
    	else {
    		printf("%s is not a palindrome\n",word);	// Otherwise, say it isnt
    	}
    
    	return 0;
    }
    
    // Recursive palindrome finder function
    bool palindrome(char* word, int length) {
    	int i=0;
    	char tempWord[100];
    	int length;
    	if(strlen(word) == 1) {	// If the word is only 1 letter long, it is a palindrome
    		return true;
    	}
    	else {
    		if(word[0] == word[(strlen(word) - 1)]) {	// If first letter equals the last letter...
    			for(i = 0; i < strlen(word); i++) {
    				tempword[i] = word[(i+1)];	// Cut the first and last letters off the word...
    			}
    			strcpy(word,tempWord);
    			length = strlen(word);
    			palindrome(word,length);	// Rerun recursive function
    		}
    		else {
    			return false;
    		}
    	}
    }
    ...but Im getting these errors:

    --------------------Configuration: cc - Win32 Debug--------------------
    Compiling...
    c.c
    C:\Documents and Settings\**********\Desktop\cc\c.c(4) : error C2061: syntax error : identifier 'palindrome'
    C:\Documents and Settings\**********\Desktop\cc\c.c(4) : error C2059: syntax error : ';'
    C:\Documents and Settings\**********\Desktop\cc\c.c(4) : error C2059: syntax error : 'type'
    C:\Documents and Settings\**********\Desktop\cc\c.c(13) : warning C4013: 'palindrome' undefined; assuming extern returning int
    C:\Documents and Settings\**********\Desktop\cc\c.c(23) : error C2061: syntax error : identifier 'palindrome'
    C:\Documents and Settings\**********\Desktop\cc\c.c(23) : error C2059: syntax error : ';'
    C:\Documents and Settings\**********\Desktop\cc\c.c(23) : error C2059: syntax error : 'type'
    Error executing cl.exe.

    c.obj - 6 error(s), 1 warning(s)

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    I don't think C has a bool datatype.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    4
    Hmmm, you're right... I like C++ sooo much better than C... argh...

  4. #4
    Registered User
    Join Date
    Nov 2003
    Posts
    9
    hi,

    I just wrote a recursive prgm for checking if the word is palindrome or not. The prgm goes like this -

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int palindrome(char *arg1,int arg2)
    {
       if(strlen(arg1)/2!=arg2-1)
       {
    	   if(arg1[strlen(arg1)-arg2]==arg1[arg2-1]) 
    	   {
    			palindrome(arg1,arg2-1);
    			return(0);
    		}
    		else
    			return(1);
       }
    }
                 
             
    
    int main()
    {
        char check_word[100];
        int length;
        
        printf("Enter the word to check for its palindrome : ");
        scanf("%s",&check_word);
        
        length=strlen(check_word);
        if(palindrome(check_word,length)==0)
        {
            printf("%s is a palindrome word....!\n");
        }
        else
        {
            printf("%s is not a palindrome..........!\n");
        }    
    }
    It is very simple to understand and implement.

    bye
    Praveen

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by praveen_23
    hi,

    I just wrote a recursive prgm for checking if the word is palindrome or not. The prgm goes like this -

    ...code snipped...

    It is very simple to understand and implement.

    bye
    Praveen
    Stop doing everyone's work for them. I didn't mention it before to you, because you'd done it only a few times. Help, sure, but don't give them the whole program. That doesn't help anyone. That's not what the board is here for. Read the forum rules while you're at it.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    It is very simple to understand and implement.
    It also doesn't work.
    - Not all paths through palindrome() return a value.
    - It thinks "everyone" is a palindrome
    - It thinks "rr" isn't
    - printf() doesn't get passed enough parameters
    and don't tell me you did that on purpose as an
    exercise for Corallis...
    DavT
    -----------------------------------------------

  7. #7
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Just out of curiosity, why use recursion to test a palindrome?

    Code:
    for(i = 0; i < strlen(word); i++) {
    	tempword[i] = word[(i+1)];	// Cut the first and last letters off the word...
    }
    I don't think this does what the comment says it does. Your compiler should also give you a warning about a missing return value.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >why use recursion to test a palindrome?
    Because when some people learn about recursion they think it's really nifty and try to use it everywhere even when it doesn't make sense or result in a sound design.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Project ideas.
    By Wraithan in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 04-25-2009, 04:29 PM
  2. Ideas, how do you get them? xD
    By Akkernight in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 01-22-2009, 03:53 AM
  3. Ideas needed
    By sean in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-16-2002, 06:33 PM
  4. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 05:36 PM

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