views:

61

answers:

3

Firstofall let me tell you what a border of string is.

Let x = abacab.

The borders of x are ε, ab . That is a border of a string is a substring which is both a prefix and suffix of it. for e.g. In string abcde hgrab edcba abcde is prefix as well as suffix .Thus it is also the border of above string.

Guys please could somebody help me in finding the border of a string.

A: 

If linear time is fine, find the longest common prefix of the string and its reverse. This works for your second example. As has been pointed out in comments, the first one is different.

Space_C0wb0y
This solution assumes the second example is true.
BoltClock
A: 
x = abacab
y = String.reverse(x)

border = ""

for (int i = 0; i < x.length; i++)
{
  if (x[i] == y[i])
    border += x[i]
  else
    break
}

print border
Boris Pavlović
A: 

If you are talking about character arrays, I think you want the following. This is based on the border being the first and last character of a string. Your examples aren't clear as to what a border is. You need to more clearly define what a border is.

x = abcde
border = { x[0], x[length(x)-1) }

and if you need length

length(z) {
    return sizeof(z) / (sizeof(z[0])
mike_b