Thread: I need some help with this function I have written it

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    9

    I need some help with this function I have written it

    The program needs to tell me if the phrase is a palindrome or not but when compile it and then enter a phrase it tells me everything is a palindrome..something is wrong with my functions but I cannot figure out what it is. I need some help please lol

    Code:
    #include <stdio.h>
    #define true  1
    #define false 0
     
    void make_copy_of_string(char str[], char str_copy []);
    void keep_chars(char str[]);
    void convert_upper_to_lower_case(char str[]);
    _Bool palindromeness(char str[]);
     
    int main(void)
    {
    char phrase[101], phrase_copy[101];
     
    printf("Enter Phrase: ");
    fgets(phrase, 101, stdin);
     
    make_copy_of_string(phrase, phrase_copy);
    keep_chars(phrase_copy);
    convert_upper_to_lower_case(phrase_copy);
     
    if(palindromeness(phrase_copy) == true)
     printf("The phrase: %s\n Is a palindrome!\n", phrase);
    else
     printf("The phrase: %s\nIs not a palindrome\n", phrase);
     
    return 0;
    }
     
    void make_copy_of_string(char str[] , char str_copy[])
    {
      int i=0;
       while(str[i]!= '\n' && str[i] != '\0')
       {
         str[i] = str_copy[i];
          i++;
       }
         str_copy[i]='\0';
         str[i]='\0';
    }
     
    void keep_chars(char str[])
    { int i=0, j=0;
     while(str[i] != '\0')
      {
       if(('A' <= str[i] && str[i] <= 'Z') || ('a' <= str[j] && str[j] <='z'))
        {
            str[i]=str[j];
            i++;
            j++;
        }
      else
       {
        i++;
       }
      }
        str[j]='\0';
     
    }
     
    void convert_upper_to_lower_case(char str[])
    {
      int i=0;
       while(str[i] !='\0')
       {
          if('A' <= str[i] && str[i] <= 'Z')
            {str[i]=str[i]+32;
              i++;
            }
             else
             {
              i++;
             }
            }
       }
    _Bool palindromeness(char str[])
    {
     int i=0;
     char str_copy[i];
     
       if(str[i] == str_copy[i])
         {
     
          return true;
         }
     
        else
        {
     
         return false;
        }
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    All of the characters that you need to check are in the local variable str[] after you call palindromeness(). The source of the errors is using str_copy at all in that function. Just look at all the characters in str[] to determine if it is a palindrome or not.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Boy! Your code is an example of what happens when you make guesses of how to do things without understanding either the problem or the code constructs you're using.

    1) make_copy_of_string() does not actually create a copy of the string. It actually overwrites str (the string being copied) with characters from str_copy (which is presumably intended to become a copy of str). You are aware that an assignment "a = b" changes the value of a so it's equal to b, not the reverse?

    2) I'm at a loss in trying to understand what keep_chars() is trying to achieve. It seems to be swapping characters around in various positions, based on whether (you think) one is upper case and one is lower case. It is probably garbling the input string or doing nothing (I'm not sure which on a quick look, because of the convoluted logic).

    3) The test "('A' <= str[i] && str[i] <= 'Z')", strictly speaking, does not check if str[i] is uppercase. Depending on character set supported by your compilers, there can be non-alphabetic characters between 'A' and 'Z' (although your test is valid for some character sets, like ASCII). Look up the standard function isupper() in <ctype.h> for an approach that will work with all possible character sets.

    4) Similarly, "('a' <= str[j] && str[j] <='z')" does not check if str[j] is lower case. Look up the standard function islower() in <ctype.h>.

    5) Adding 32 is not a guaranteed way of converting an uppercase letter to a lowercase letter. Look up the standard function tolower() in <ctype.h>.

    6) If you're going to use boolean types (and your compiler is compliant with C99 or later, which it presumably is since you're using _Bool) use the standard header <stdbool.h>. It defines macros bool (for the bool type), true, and false. It is not a good idea to write your own macros to #define true or to #define false, and then to mix them with usage of the _Bool type.

    7) Because of the ways the preceding functions are working (and the problems thereof) odds are str[0] and str_copy[0] are both equal in palindromeness(), which would explain why your program is reporting all input as a palindrome.



    As to how to fix the problem. Personally, I suggest you start over from scratch with a simple problem description (read a string, tidy it up if needed, check if its a palindrome). Although some problems I've pointed out need to be fixed anyway, some of the problems you have are easily dealt with by starting over.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 07-07-2013, 01:41 AM
  2. Replies: 21
    Last Post: 11-03-2007, 02:56 PM
  3. In C, How to use a dll written in C++?
    By ansonlyx in forum C Programming
    Replies: 6
    Last Post: 12-16-2006, 05:26 PM
  4. written in C... :°)
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 02-04-2003, 07:24 AM
  5. How come c board is written in PHP?
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 05-23-2002, 01:17 PM

Tags for this Thread