Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


Write a program which has a function and that function
should take 2 or 3 or any number of strings and it should
return the largest common prefix of all those strings. If
there is no common prefix it should return an empty string.

for eg:- INPUT OUTPUT
glo {glory,glorious,glod}


gl {glad,glow}


{calendar,phone}

empty string

Answers were Sorted based on User's Feedback



Write a program which has a function and that function should take 2 or 3 or any number of strings..

Answer / jayakrishnan

strings[0].Substring(0,GetIndex(strings));

public static int GetIndex(string[] s)
{
int index=0;
for (int i = 0; i <= s.Length; i++)
{
if (i == s[0].Length) return i;
char comp = s[0][i];
for (int j = 1; j <= s[i].Length; j++)
{
if (j == s.Length)
{
index = j;
break;
}
if (i >= s[j].Length)
{
index = s[j].Length;
return index;
}
if (comp != s[j][i])
{
index = i;
return index;
}
}
}
return index;
}

Is This Answer Correct ?    2 Yes 0 No

Write a program which has a function and that function should take 2 or 3 or any number of strings..

Answer / naresh

public class commonprefix
{
private static String CommonPrefix(String[] ss)
{
if (ss.length == 0)
{
return "";
}

if (ss.length == 1)
{
return ss[0];
}

int prefixLength = 0;

for(char c:ss[0].toCharArray())
{
for(String s:ss)
{
if (s.length()<= prefixLength ||
s.charAt(prefixLength)!= c)
{
return ss[0].substring(0, prefixLength);
}
}
prefixLength++;
}

return ss[0]; // all strings identical
}
public static void main(String args[])
{
System.out.println(""+CommonPrefix(args));

}
}

Is This Answer Correct ?    0 Yes 1 No

Post New Answer

More C Sharp Code Interview Questions

how do i copy textbox contents of 1 form to another form

4 Answers   Wipro,


program for addition of fraction(M/N + P/Q = Y/Z)

1 Answers   Mind Tree,


c# code to Count number of 1's in a given range of integer (0 to n)

0 Answers  


how can i split sting in textbox in windows application using c# .net

2 Answers  


Write a program to input an integer and - display the reverse - display the sum of each digit - should include logic that considers the input number as any number of digits long

2 Answers   Mind Tree,


How to use ASP.NET 2.0's TreeView to Display Hierarchical Data?

1 Answers  


How to export 2 datatables of a single dataset to 2 different worksheets of a single MSExcel file ?

0 Answers   Eastcom Systems,


How to Link Different Data Sources Together?

0 Answers  


write a Program to copy the string using switch case.

0 Answers   Mind Tree, Wipro,


Write a function which accepts list of nouns as input parameter and return the same list in the plural form. Conditions: i) if last letter is r then append s ii) if word ends with y then replace it by ies iii) call this function in main() and produce the required output. for eg:- if chair is input it should give chairs as output.

0 Answers   Mind Tree,


program for string reverse(eg:- i am boy -> boy am i)

8 Answers   Black Pepper, Infosys, Mind Tree,


Write a program to convert postfix expression to infix expression.

0 Answers   Mind Tree,


Categories
  • ASP.NET Code Interview Questions ASP.NET Code (46)
  • VB.NET Code Interview Questions VB.NET Code (9)
  • C Sharp Code Interview Questions C Sharp Code (51)
  • ADO.NET Code Interview Questions ADO.NET Code (8)