[Informatik] Debug: a1.c

  • From: Bennett Piater <bennett@xxxxxxxxxxx>
  • To: cs-coop-uibk@xxxxxxxxxxxxx
  • Date: Sat, 15 Nov 2014 14:22:58 +0100

Falls jemand Zeit haben sollte, einen Blick auf meinen Code zu werfen,
wäre ich sehr dankbar... Er scheint zu funktionnieren, aber eine meiner
eigenen Fehlermeldungen wird zu oft ausgegeben.

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
const int STR_MAX = 25;

bool check_palindrome(const char string[]);
int last_str_index(const char string[]);
void substring(const char string[], char* target, int begin, int end);
void max_palindrome(const char string[], char* target);


int main()
{
        char string[STR_MAX];
        printf("Enter a string without spaces: ");
        scanf("%s", string);
        bool palindrome = check_palindrome(string);
        printf("Is this a palindrome? ");
        printf("%s\n", palindrome ? "true" : "false");

        char string2[15] = {0};
        substring(string, string2, 2, 4);
        printf("A substring from your string:\n");
        printf("%s\n", string2);

        char string3[STR_MAX];
        printf("%lu\n", sizeof(string3));
        max_palindrome(string, string3);
        printf("The longest palindrome in your string:\n");
        printf("%s\n", string3);
        return EXIT_SUCCESS;
}

// Find the index of the last non-zero-item in a string
int last_str_index(const char string[])
{
        // Iterate through the string
        for (int i = 0; i < STR_MAX; ++i)
        {
                // Stop as soon as a zero is found
                if (string[i] == 0)
                        return i-1;
        }
        // Satisfy GCC for a case that will never happen
        return 0;
}

bool check_palindrome(const char string[])
{
        bool result = true;
        // Pre-calculate this for performance
        int last = last_str_index(string);
        // Reversely iterate through the characters of a string
        for (int i = last; i >= 0; --i)
        {
                // Check if the item is equal to it's opposite within the 
characters
                if ((string[i] != string[last - i]))
                {
                        result = false;
                        break;
                }
        }
        return result;
}

void substring(const char string[], char* target, int begin, int end)
{
        // Check if the source is readable
        if ((begin > end) || (end > last_str_index(string)))
        {
                printf("These values won't work...\n");
                return;
        }
        // Check if  the target is long enough
        else if((end - begin) > sizeof(target)) // use sizeof because it may 
contain zeroes
                // DEBUG - why does this return 8 when called from 
max_palindrome? sizeof(target) should be 25!
        {
                printf("%lu ", sizeof(target));
                printf("The target is too short!\n");
                return;
        }
        int j = 0;
        for (int i = begin; i <= end; ++i, ++j)
        {
                target[j] = string[i];
                target[j+1] = 0; // Make the array a valid string
        }
}

void max_palindrome(const char string[], char* target)
{
        int new_len;
        int old_len = 0;
        // Pre-calculate this for performance
        int length = last_str_index(string);
        char tmp[length+1]; // This will always be long enough
        for (int i = 0; i <= length; ++i) // i is the beginning,
        {
                for (int j = i; j <= length; ++j) // j is the end of the tested 
substring
                {
                        substring(string, tmp, i, j);
                        if (check_palindrome(tmp))
                        {
                                new_len = last_str_index(tmp);
                                // Check if this palindrome is longer than the 
old one
                                if (new_len > old_len)
                                {
                                        // It is, so make this the new result
                                        old_len = new_len;
                                        substring(string, target, i, j);
                                }
                        }
                }
        }
}

Other related posts: