0

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>
Yodax93
  • 21
  • 2
  • `char *c,*temp; strcpy(c,suffix[0]); ` You have to allocate memory to `c` using `malloc` or a similar function before copying to it – Spikatrix Nov 21 '21 at 09:41
  • I don't understand what you mean, I'm not very good at c anyway, but there is no problem in assigning the prefixes of the suffixes to the string matrix, I print them in the second loop I wrote to see if there is a problem – Yodax93 Nov 21 '21 at 09:47
  • 1
    There _is_ a problem there. You see, in C, just because something works doesn't mean it's correct. You copy `suffix[0]` into an uninitialized pointer `c` which means it's copying the string to invalid memory. This might appear to work silently or might crash or do something else. This behavior is called Undefined Behavior which means that anything could happen including the issues you're experiencing. Also, you're not assigning, you're copying. Assigning would be `c = suffix[0]` which is valid code. Lastly, it would be best if you could post an [mcve] – Spikatrix Nov 21 '21 at 09:55
  • How can i fix this memory problem? I've only just moved on to malloc realloc, so I don't have many applications there. I have no idea how to explain this in a more minimal way. You can understand that I'm just starting out because I'm having such a hard time even writing a simple code. – Yodax93 Nov 21 '21 at 10:04
  • As I said, you need to allocate memory for those variables using `malloc` and `free` them once you're done using them. Or you could just use good ol' arrays: `char c[100], temp[100];` – Spikatrix Nov 21 '21 at 10:10
  • temp[100]; When I did, the error was fixed, thank you. I couldn't solve it with malloc and realloc. Do you have a resource you recommend for malloc and realloc? – Yodax93 Nov 21 '21 at 10:52
  • @Yodax93, from what I can see, what you are looking for is not specifically using malloc or realloc, but understading the difference between "Static and Dynamic Memory Allocation". – dandev486 Nov 21 '21 at 11:22
  • The `temp[100]` suggestion makes sure that memory is allocated statically (or in really simple words, "reserved in advance"). The `malloc` suggestion makes sure that memory is allocated dynamically (or in really simple words, "reserved as needed, during execution"). You can't properly use memory that is not "reserved". In C, it is part of the programmer job ensuring that memory is allocated for usage and deallocated after usage (in case of dynamically allocated memory). – dandev486 Nov 21 '21 at 11:28

0 Answers0