String Suffix and Prefix. Take a piece that serves both

Asked

Viewed 919 times

3

I need to do a function that returns the Length of a part of the string that is both suffix and prefix. So: Given the string: abbabba, the answer would be 4, because that would be:

Prefix:

a ab abb abba abbab abbabb 

Suffix:

a ba bba abba babba bbabba

Logo Abba is both prefix and suffix. How do I do this?

I started like this and I can’t and I only have 10 more minutes.

public int teste(string nome)
        {

            string[] pre = new string[nome.Length];
            string[] suf = new string[nome.Length];

            for (int i = 0; i < nome.Length - 1; i++)
            {
                pre = nome[i].ToString();// Erro aqui
            }

        }
  • Do you have a goal? Or is the idea purely didactic?

  • I’m doing a test and I can do research on the test so there’s nothing fraudulent.

  • I don’t know what the c# code would look like, but you can count the string lenght, do one for creating an array for the prefix and another for the suffix with each of the variations as shown in the question and then compare the two to get the values that are present in the two. In the end just take what has the largest lenght and you will have your (su)prefix.

  • It’s not the longest length, but the ones that are equal, then yes, I take that length.

  • only count the same then, instead of the greater length as @Rodrigoborth said

  • That’s exactly my question. I did an edit on the post.

  • in the end you can take the lenght of all and check which is the highest and return the value

  • "I’m taking a test and I can do research" - this is not research.

Show 3 more comments

3 answers

3


I made a while that compares the beginning with the end until reaching the middle

String nome = "abbabba";

var i = 0; // começo
var j = nome.Length-1; // fim, -1 porque começo do 0
var tamanho = 0;
var sufixoEprefixo = "";

while (i <= j) {
    if (nome[i] == nome[j]) { // mesmo caracter
        tamanho++;
        sufixoEprefixo += nome[i];
    } else { // se for diferente pode parar 
        break;
    }
    i++; // avança letra
    j--; // retrocede letra
}
Console.WriteLine(tamanho);
Console.WriteLine(sufixoEprefixo);

4

Abba

Ideone Exemplo

  • Too bad my time’s up.

0

I don’t know what the code in C would look like, but let’s take it step by step:

1 Pass string to function
2 Declare 1 array for prefixes and 1 array for suffixes
3 Declare 1 variable to count how many prefixes are equal to suffixes
4 fill in the prefix array
5 fill the suffix array
6 for each element in the prefix array, check that it is in the suffix array
   6.1 if you are, add 1 to your variable
7 return the variable



NOTE: You will not match the array to a string, you will add the string to the array.

  • Name[i] is a char and not a string

  • is! but the other note is still valid

0

I don’t know how it looks in C#,but I’ll give you the answer in with logic and you can apply it in your case.

var String = "abbabba";
var len = String.length; //Aqui você terá o tamanho da sua string
var array1, array2;
var lenfinal;


para(i=1;i<=len;i++){
    array1[i] = String.caracter(0,i);
    array2[len-i] = String.caracter(len-i,i);
}

//Nesse ponto você deveria ter 2 arrays(array1 e array2) com todos os prefixos e sufixos da string

para(u=0;u<len;u++) {
    if(array1[u] estacontidoem array2){
        lenfinal = array[u].length;
    }
}

imprime(lenfinal);

That’s basically it, just apply in C# now

In this case I believe that the correct answer would be 7. Because we have a palindrome the whole word can be suffix and prefix at the same time

  • Are you interested in learning C#? http://chat.stackexchange.com/rooms/25342/c-asp-net-asp-net-mvc-entity-framework-etc

Browser other questions tagged

You are not signed in. Login or sign up in order to post.