Palindrome Function

This is a discussion on Palindrome Function within the C Programming forums, part of the General Programming Boards category; Hey all, So I'm trying to write a program that takes a 5 letter word and displays it as a ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    2

    Palindrome Function

    Hey all,

    So I'm trying to write a program that takes a 5 letter word and displays it as a palindrome with a space between each letter. I can do this in only 11 lines of code, however I need a function part to the program and I'm not sure what this function should do or how to modify my code to fix it? This is what I have so far.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    char palindrome(char word);
    char main(void)
    {
    char palin;
     char letter0, letter1, letter2, letter3, letter4;
       printf("What is the five letter word? ");
        scanf("%c",&letter0);
        scanf("%c",&letter1);
        scanf("%c",&letter2);
        scanf("%c",&letter3);
        scanf("%c",&letter4);
    
    palin = palindrome(letter0, letter1, letter2, letter3, letter4);
    printf("Here is the palindrome:\n", palin);
        return 0;
    }
    
    char palindrome(char word)
    {
    char letter0, letter1, letter2, letter3, letter4;
    char pDrome[50];
    pDrome == printf("%c %c %c %c %c %c %c %c %c", letter0, letter1, letter2, letter3, letter4, letter3, letter2, letter1, letter0);
    return pDrome;
    
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,566
    I suggest you get your textbook and brush up on your C basics. There are also some tutorials on this site to help out too:
    1. Functions
    2. Arrays
    3. Strings

    A quick summary of the problems you have:
    1. Your palindrome function needs to take 5 letters as parameters, not one.
    2. == compares two things, a single = assigns a value.
    3. You can't return an entire local array from a function. Allocate dynamically or pass in an array to fill/modify.
    4. Read the printf docs on how to use format specifiers, namely %s.
    5. char is for individual characters. You need a char * or a char array to represent a string for use with %s.
    6. main should return an int
    7. You don't need stdlib for your current implementation.

  3. #3
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    there is so many things off with this code..
    which anduril has pointed out..
    but your function doesn't check if it is a palindrome...in fact it doesn't do anything..
    simple way i know is
    pass in an array of char - strings - compare the first element with n-1
    compare second with n-2 and so on until j>i
    or just copy into second array in reverse order and compare..
    take anduril's advice first!!!
    You ended that sentence with a preposition...Bastard!

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    2
    No, it's not suppose to check if anything is a palindrome. It's only suppose to generate a palindrome out of the 5 letters that you type in.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,566
    One more thing, don't try to assign something the result of printf, it doesn't give you what you want. You probably want something like sprintf instead. Read the docs for printf and sprintf to find out the details of what they do and what they return.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OpenGL DC Buffer Renders slow
    By Lane the Great in forum Game Programming
    Replies: 10
    Last Post: 01-07-2011, 06:52 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 06:05 PM

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