Most repeated substring in a string in C

131 Views Asked by At

Due to our problem, I need to find the most repeated substring in a string. The way I followed for this was as follows: I found the suffixes of the string, then I found the prefixes of the suffixes and assigned them to a matrix array. Finally, I created a code in the while loop using strcmp and strcpy to assign the repetition amount of the most repetitive value to an int value and to suppress it, but there is a problem in the last loop, when I want to run it, the compiler stops the program and closes it, and it doesn't continue.

Since I wrote the code in my own language, I changed the variables of the code to English while writing it here, if there is an error, I can edit it later.

For example, the prefixes of the suffixes of the word banana are as follows:

banana
banan
bana
ban
ba
b
anana
anan
ana
an
a
nana
nan
na
n
ana
an
a
na
n
a

the part of the code that I think is wrong:

   while(strlen(string[j])>0)
    {
        for(int i=0; i<number; i++)
        {
            if(strcmp(string[j],string[i])==0)
            {
              count++;
              printf("\n %s %s\n",string[j],string[i]);
            }

        }
        if(count>max)
        {
            strcpy(temp,string[j]);
            max=count;
        }
        j++;
        count=0;
   }

part of the code that is the entire function:

void most_repeated(int length,char suffix[length][100])
{
    char *c,*temp;
    strcpy(c,suffix[0]);
    int j=0,count=0,number=0,max=0;
    char string[1000][100];
    while(strlen(c)>0)
    {
        for(int i=number; i<1000; i++)
        {
            if(strlen(c>0)
            {
                strcpy(string[i],c);
                c[strlen(c)-1] = '\0';
            }
            else
            {
                number=i;
                break;
            }
        }
        j++;
        strcpy(c,suffix[j]);
    }
    j=0;
    while(strlen(string[j])>0)
    {
        printf("\n%s\n",string[j]);
        j++;
    }

    j=0;
   while(strlen(string[j])>0)
    {
        for(int i=0; i<number; i++)
        {
            if(strcmp(string[j],string[i])==0)
            {
              count++;
              printf("\n %s %s\n",string[j],string[i]);
            }

        }
        if(count>max)
        {
            strcpy(temp,string[j]);
            max=count;
        }
        j++;
        count=0;
   }
    printf("\nmost repeated:%s\nnumber of repetitions:%d\n",temp,max);*/
}

These are the libraries I used:

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

There are 0 best solutions below