Thread: Palindromes - Determining the _Bool function

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    14

    Palindromes - Determining the _Bool function

    Hey guys!

    I am working on a palindrome assignment and I have pretty much everything figured out up until the end. Here is my code so far:

    Code:
    #include <stdio.h>#define TRUE 1
    #define FALSE 0
    
    
    void make_copy_of_string(char str[], char str_copy[]);
    void keep_chars(char string[]);
    void convert_uper_to_lower_case(char string[]);
    _Bool palindromness(char string[]);
    
    
    int main(void)
    {
    
    
        char phrase[101]; //create array to hold input
        char phrase_copy[101]; //array that holds copy of input which will be modified
    
    
        printf("Enter a phrase: ");
        fgets(phrase, 101, stdin); //stores input into 'phrase' with a max 100 chars
        printf("%s\n", phrase); //tests if fgets is working
    
    
        make_copy_of_string(phrase, phrase_copy); //makes a copy of phrase
        printf("%s\n\n", phrase_copy);
    
    
        keep_chars(phrase_copy);
        printf("%s\n", phrase_copy);
    
    
        convert_uper_to_lower_case(phrase_copy);
        printf("%s\n", phrase_copy);
    
    
    
    
        return 0;
    }
    
    
    void make_copy_of_string(char str[], char str_copy[])
    {
        int i = 0; //to keep track of which char is being copied
    
    
        while(str[i] != '\n' && str[i] != '\0')
        {
            str_copy[i] = str[i];
            i++;
        }
        str_copy[i] = '\0';
        str[i] = '\0';
    }
    
    
    void keep_chars(char string[])
    {
        int i=0, j=0;
    
    
        while (string[i] != '\0')
        {
            if ( ('A'<= string[i] && string[i] <= 'Z') || ('a' <= string[i] && string[i] <= 'z'))
            {
                string[j] = string[i];
                i++;
                j++;
            }
            else
                i++;
        }
        string [j] = '\0';
    }
    
    
    void convert_uper_to_lower_case(char string[])
    {
        int i = 0;
    
    
        while (string[i] != '\0')
        {
            if ('A' <= string[i] && string[i] <= 'Z')
            {
                string[i] += 32;
                i++;
            }
            else
                i++;
        }
    
    
    }
    
    
    _Bool palindromeness(char string[])
    {
    
    
    }
    The only issue I am running in, is figuring out how to set up the _Bool palindromeness function. I was thinking of perhaps making a copy of "convert_uper_to_lower_case" and doing an if loop to see if the first char matched the last char of the copy.

    With this setup I described, I am still not sure how to set it up really, and I would rather work on a better method. I am only allowed to use stdio.h as well.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,255
    You need to find the length of the string( without the '\0' character of course ). Loop from 0 to length/2 and check if str[i] and str[length-i-1] are equal.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    May 2017
    Posts
    14
    Quote Originally Posted by GReaper View Post
    You need to find the length of the string( without the '\0' character of course ). Loop from 0 to length/2 and check if str[i] and str[length-i-1] are equal.
    How would I check if str[i] and str[length-i-1] are the same? Also what is str[length-i-1]? I just learned about Arrays and don't know too much about them.

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,255
    Quote Originally Posted by joshlete View Post
    How would I check if str[i] and str[length-i-1] are the same
    By using the equality operator, '=='.

    Quote Originally Posted by joshlete View Post
    Also what is str[length-i-1]
    Hopefully you've learned that you can address a specific element of an array by using an index, like "array[i]".
    In my example, 'length' is supposed to be the string length. When you have a counter from 0 to length/2, "length-i-1" is essentially the mirror index of "i", allowing you to check if those two char are the same, and in the end if the string is a palindrome. For example:
    Code:
    length = 7
    "i" "length-i-1"
     0       6
     1       5
     2       4
     3       3
     4       2
     5       1
     6       0
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    May 2017
    Posts
    14
    Quote Originally Posted by GReaper View Post
    By using the equality operator, '=='.


    Hopefully you've learned that you can address a specific element of an array by using an index, like "array[i]".
    In my example, 'length' is supposed to be the string length. When you have a counter from 0 to length/2, "length-i-1" is essentially the mirror index of "i", allowing you to check if those two char are the same, and in the end if the string is a palindrome. For example:
    Code:
    length = 7
    "i" "length-i-1"
     0       6
     1       5
     2       4
     3       3
     4       2
     5       1
     6       0
    Okay that makes sense now. For some reason, I imagined you can only put 'i' into the brackets of an array for some reason. Knowing I can put an expression in the brackets opened up my mind. Here is what I got for the _Bool function:

    Code:
    _Bool palindromeness(char string[])
    {
        int i, index_last_char;
    
    
        for(i=0; string[i] != '\0'; i++);
        index_last_char = i-1;
    
    
        for (i=0; i<=(index_last_char)/2; i++)
        {
            if(string[i] != string[index_last_char - i])
                return FALSE;
        }
        return TRUE;
    
    
    
    
    }

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,255
    Doesn't it work? I think that should work now.
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function or code to Determining class of IP address
    By hari_bly in forum C Programming
    Replies: 1
    Last Post: 06-10-2011, 01:59 AM
  2. Determining function based an input
    By Suchy in forum C++ Programming
    Replies: 7
    Last Post: 02-20-2008, 02:52 PM
  3. Simple Function Pointers - Determining Output
    By Adrian in forum C++ Programming
    Replies: 1
    Last Post: 12-07-2007, 08:10 PM
  4. Determining if a function exists
    By tyouk in forum C Programming
    Replies: 8
    Last Post: 12-14-2004, 02:52 PM
  5. Replies: 1
    Last Post: 03-23-2003, 05:04 PM

Tags for this Thread