2

I'm trying to create a program which returns the Longest repeated substring. I've almost got the solution, but for some reason my strcmp gives an error when he is busy to find the LRS. Could someone explain me why there is an error and how I solve this?

The code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define NSTRINGS 4

char* searchLongestRepeatedSubstring(char* string);
char** makeSuffixArray(char* string);
void freeSuffixArray(char** suffixArray);
int cmp(const void*a, const void* b);

/* do not change this code */
int main(void)
{
    char* strings[NSTRINGS] = {
        "bananas",
        "ask not what your country can do for you, but what you can do for your country",
        "main",
        "" };
    char* result;

    for (int i = 0; i < NSTRINGS; ++i)
    {
        result = searchLongestRepeatedSubstring(strings[i]);
        if (result != NULL)
        {
            /* write out LRS */
            printf("%s: \"%s\"\n", strings[i], result);

            /* free() the result */
            free(result);
            result = NULL;
        }
        else
            printf("Geen longest repeated substring.\n");
    }

    return 0;
}

/**
* Finds the LRS from a string using the function makeSuffixArray
*
* @param   the given string
* @return  the longest repeated substring
*/
char* searchLongestRepeatedSubstring(char* string)
{

    char **p;
    p = makeSuffixArray(string);
    size_t length = strlen(string);
    int max_sz = 0;
    char max_word;
    int curr_sz = 0;
    int curr_word;
    char* word1;
    char* word2;
    char s1, s2;
    size_t length1;
    size_t length2;

    for (size_t i = 0; i < length; i++)
    {
        for (size_t j = i + 1; j < length; j++)
        {
            word1 = p[i];
            word2 = p[j];
            length1 = strlen(word1);
            length2 = strlen(word1);
            for (size_t x = 0; x < length1; x++)
            {
                s1 = word1[x];
                for (size_t y = 0; y < length2; y++)
                {
                    s2 = word2[y];
                    if (strcmp(s1, s2) == 0) {
                        curr_sz++;
                        strcat(curr_word, s1);
                        x++;
                    }
                    else
                        break;
                }
            }
            if (curr_sz > max_sz) {
                max_sz = curr_sz;
                curr_sz = 0;
                max_word = curr_word;
                curr_word = "";
            }
            else {
                curr_sz = 0;
                curr_word = "";
            }
        }
    }
    return max_word;
}

/**
* Creates the suffix array of the given string
*
* @param   the given string
* @return  the suffix array
*/
char** makeSuffixArray(char* string)
{
    size_t length = strlen(string);
    char **p = (char**)malloc(length * sizeof(char*));
    for (size_t i = 0; i < strlen(string); i++)
    {
        p[i] = &string[i];
        puts(p[i]);
    }
    qsort(p, length, sizeof(char*), cmp);
    return p;
}


int cmp(const void* a, const void* b)
{
    char ** p = (char**)a;
    char ** t = (char**)b;

    return strcmp(*p, *t);
}

/**
* free() the memory allocated for the suffix array
*
* @param   the given suffix array
*/
void freeSuffixArray(char** suffixArray)
{
  free(suffixArray);
}
SpartanHero
  • 107
  • 8

1 Answers1

1

In your function char* searchLongestRepeatedSubstring-

 if (strcmp(s1, s2) == 0) {

s1 and s2 are both char variables , and you pass them to strcmp which expects const char * as arguments , therefore, your compiler complaints .

You can compare them like this-

if(s1==s2)

Also this statement in same if block -

strcat(curr_word, s1);

instead you can do this -

size_t len=strlen(curr_word);
curr_word[len]=s1;                  // make sure curr_word is large enough
curr_word[len+1]='\0';
Weather Vane
  • 33,872
  • 7
  • 36
  • 56
ameyCU
  • 16,489
  • 2
  • 26
  • 41