Logic help...

This is a discussion on Logic help... within the C Programming forums, part of the General Programming Boards category; Hi everyone, I have a question on the logic of the attached code. In the code there is four "isapha" ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    178

    Logic help...

    Hi everyone, I have a question on the logic of the attached code. In the code there is four "isapha" loops. The first two are nested inside of while loop one and the next two are nested inside of while loop two. My question is why is there a need of two sets. I know what "isalpha" does.

    Code:
    /* preprocessor directives */
    
    #include <stdio.h>
    #include <ctype.h>
    
    /* Function declaration and function. */
    
    int palindrome (char* string)
    {
        char *strg_beg, *strg_end;
        strg_beg = strg_end = string;
    
        while (*strg_end != '\0')
            strg_end++;
            strg_end-=2;
    
            while (!isalpha(*strg_beg))
                strg_beg++;
    
            while (!isalpha (*strg_end))
                strg_end--;
    
                    while (tolower(*strg_beg) == tolower(*strg_end) && strg_beg <= strg_end)
                    {
                        strg_beg++;
                        strg_end--;
    
                        while (!isalpha (*strg_beg))
                        strg_beg++;
                        while (!isalpha (*strg_end))
                        strg_end--;
                    }
    
                    while (strg_beg < strg_end)
                    {
                        if (*strg_beg != *strg_end)
    
                        return 0;
    
                        strg_beg++;
                        strg_end--;
                    }
                        return 1;
    }
    
    /* Main function */
    
    int main (void)
    
    {
        char input[80] = "";
    
        printf ("Enter a string: ");
    
        fgets (input, 80, stdin);
    
        input[79] = 0;
    
        if (palindrome (input))
        {
            printf("\nThe string, %s\nis a Palindrome.\n", input);
        }
        else
        {
            printf("\nThe string, %s\nis NOT a Palindrome.\n", input);
        }
    
        return 0;
        }

  2. #2
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    265
    its for the positioning of those string pointers. You can get the string length in a similar fashion:

    Code:
    while(str[i++]);

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    Quote Originally Posted by Syscal View Post
    its for the positioning of those string pointers. You can get the string length in a similar fashion:

    Code:
    while(str[i++]);
    I, not following. Can you elaborate a bit please?

  4. #4
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    265
    In my example, so long as "str[i]" isnt "NULL" "i" will increment. The loop continues until it finds 0.

    In your case:
    Code:
    strg_beg = strg_end = string;
    strg_beg and strg_end start off pointing to the "string" pointer. Where you see "strg_beg++" that points "strg_beg" to the next character in the string. Does that help?

  5. #5
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    Quote Originally Posted by Syscal View Post
    In my example, so long as "str[i]" isnt "NULL" "i" will increment. The loop continues until it finds 0.

    In your case:
    Code:
    strg_beg = strg_end = string;
    strg_beg and strg_end start off pointing to the "string" pointer. Where you see "strg_beg++" that points "strg_beg" to the next character in the string. Does that help?
    I am not sure what that would have to do with the two "isalpha" loops. I know the isalpha keyword is used to take out punctuation, but why two diferrent sets. One set outside of the toupper loop and one inside the toupper loop.

  6. #6
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    265

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    He has two "walkers" going from each end, and headed for each other in the middle.

    What he does with one "walker", he needs to repeat with the other, as well. The logic isn't as concise as it could be.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,851
    strg_end++;
    strg_end-=2;
    The code equivalent of one step forwards, two steps backwards.

    You're heading in the wrong direction!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    265
    I posted this snippet in another post to help someone. Maybe it could be of use here. Now I know that this could be way better but it still just follows the same flow here, only its case sensitive and very limited.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    typedef enum{false, true}bool;
    
    bool is_palendrome(char* str){
    
        char* b = str, *e = str + strlen(str) - 1;
    
        while(*b == *e && b <= e)printf("%c == %c\n",*b++,*e--);
    
        return ((*b == *e) ? true : false);
    }
    
    int main(){
    
        char buffer[1024];
    
        fgets(buffer,sizeof(buffer),stdin);
    
        buffer[strlen(buffer) -1] = '\0';
    
        printf("%s\n",((is_palendrome(buffer)) ? "Is a palendrome" : "Is not a palendrome"));
    
    
        return 0;
    }

    Edit: Maybe it's pretty much the same and doesn't help afterall...
    Last edited by Syscal; 09-15-2010 at 12:32 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 09-10-2010, 03:40 PM
  2. Handling logic and draw cycles
    By DavidP in forum Game Programming
    Replies: 1
    Last Post: 07-25-2009, 11:15 AM
  3. Digital Logic
    By strokebow in forum Tech Board
    Replies: 3
    Last Post: 12-09-2006, 01:05 PM
  4. Logic
    By LordBronz in forum C++ Programming
    Replies: 6
    Last Post: 05-23-2006, 06:41 PM

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