Thread: error trapping

  1. #1
    Registered User
    Join Date
    Jul 2015
    Posts
    22

    error trapping

    Code:
    bool checkPalindrome(char* inputString) {
        char *start, *end;
        int i, length, counter = 0;
        
        start = inputString;
        end = inputString;
        
        length = stringLength(inputString);
        for(i=0;i<length-1;i++) {
            end++;
        }
        
        if(length<1) {
            return true;
        }
    
    
    
    
            if(*start==*end) {
                return true;
                }
            else {
                return false;
                }
    
    
             
    } 
               
    
    
    int stringLength(char* String) {
        int i, counter=0;
        for(i=0;*(String+i)!='\0';i++) {
            counter++;
        }
        return counter;
    }
    so basically my program fails with the ff test cases can anyone help me?
    error trapping-asda-png

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You only check the first and last character.

    You need to check each pair until you meet in the middle.
    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.

  3. #3
    Registered User
    Join Date
    Jul 2015
    Posts
    22
    yes, but i dont know how can you give me clue? i have this idea to decrement the address of end and compare the value of it to the value of start i tried different for loops still didnt get it

  4. #4
    Registered User
    Join Date
    Jul 2015
    Posts
    22
    Quote Originally Posted by Salem View Post
    You only check the first and last character.

    You need to check each pair until you meet in the middle.
    solved it by adding these codes but i know this is very impractical
    Code:
    if(length<1) {
            return true;
        }
            if(*start==*end) {
                start++;
                end--; 
                }
                if(*start==*end) {
                    start++;
                    end--; 
                    }
                        if(*start==*end) {
                            start++;
                            end--;
                            
                            }
                            if(*start==*end) {
                                start++;
                                end--;
                                return true;
                            }

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Maybe you should use a loop. Just maybe...
    Devoted my life to programming...

  6. #6
    Registered User
    Join Date
    Jun 2017
    Posts
    157
    Here is a other way to do it:
    Code:
    bool isPalindrome(char * input)
    {
      int len = strlen(input);
      if (input == NULL ||  len == 0)
        return false;
    
      int first = 0, last = len - 1;
    
      while (first < last)
      {
        if (input[first] != input[last])
          return false;
    
        first++;
        last--;
      }
    
      return true;
    }

  7. #7
    Registered User
    Join Date
    Jul 2015
    Posts
    22
    Quote Originally Posted by OldGuy2 View Post
    Here is a other way to do it:
    Code:
    bool isPalindrome(char * input)
    {
      int len = strlen(input);
      if (input == NULL ||  len == 0)
        return false;
    
      int first = 0, last = len - 1;
    
      while (first < last)
      {
        if (input[first] != input[last])
          return false;
    
        first++;
        last--;
      }
    
      return true;
    }
    gee, thanks i just learned about memory and i would like to apply it never thought this problem would be that simple

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New guy with a error trapping question
    By Deaf Smith in forum C Programming
    Replies: 11
    Last Post: 06-17-2014, 11:08 AM
  2. Trapping input
    By dragon_man in forum C++ Programming
    Replies: 2
    Last Post: 05-13-2003, 09:07 PM
  3. error trapping ??
    By mellisa in forum C++ Programming
    Replies: 4
    Last Post: 02-14-2003, 09:31 PM
  4. trapping exit??
    By spike232 in forum C Programming
    Replies: 4
    Last Post: 04-03-2002, 06:27 AM
  5. trapping keystokes from the keyboard
    By dills101 in forum C++ Programming
    Replies: 1
    Last Post: 10-09-2001, 02:25 AM

Tags for this Thread