Sometimes interview questions may be little bit tricky . The following tricky questions may be asked
1. A string has thousand or two thousand zeros . Replace all consecutive zeros with a single zero without using loop
2 String str = "200000000000000000000000500000000000000000" . Make it to 2050 without using loop
The answer should be in one line and should be a general solution to all similar problems , should not be a specific solution to a particular problem.
In college / school computer practical lab , the following program may be given to solve. WAP that replaces two or more consecutive characters / blanks in a string by a single character / blank. For example, if the input is Keppplarrr-222 2b issss 222.444 timmes the sizzzze of the eeearth and the output should be Keplar-2 2b is 2.4 times the size of the earth.
The solution to all the above questions, without using loop will be using Regular Expression (RegExp) . The answer using RegExp is , str.replaceAll( "(.)\\1+", "$1" )
"(.)\\1+ " - The meaning of the metacharater used in the expression is
(.) - match any charater
\1 - matchs text similar to that matched within the first parenthesized .
+ - One or more times
The program for the above questions is as follows . The problems are solved in two ways . One is using Regular Expressions and another is using for loop. In method 2 , charAt (int index), deleteCharAt(int index) are used where char charAt (int index) - Returns the char value at the specified index and StringBuffer deleteCharAt(int index) - Removes the char at the specified index*/
1. A string has thousand or two thousand zeros . Replace all consecutive zeros with a single zero without using loop
2 String str = "200000000000000000000000500000000000000000" . Make it to 2050 without using loop
The answer should be in one line and should be a general solution to all similar problems , should not be a specific solution to a particular problem.
In college / school computer practical lab , the following program may be given to solve. WAP that replaces two or more consecutive characters / blanks in a string by a single character / blank. For example, if the input is Keppplarrr-222 2b issss 222.444 timmes the sizzzze of the eeearth and the output should be Keplar-2 2b is 2.4 times the size of the earth.
The solution to all the above questions, without using loop will be using Regular Expression (RegExp) . The answer using RegExp is , str.replaceAll( "(.)\\1+", "$1" )
"(.)\\1+ " - The meaning of the metacharater used in the expression is
(.) - match any charater
\1 - matchs text similar to that matched within the first parenthesized .
+ - One or more times
The program for the above questions is as follows . The problems are solved in two ways . One is using Regular Expressions and another is using for loop. In method 2 , charAt (int index), deleteCharAt(int index) are used where char charAt (int index) - Returns the char value at the specified index and StringBuffer deleteCharAt(int index) - Removes the char at the specified index*/
class ReplaceConsecutiveChar
{
public static void main (String args[])
{
//String inputStr ="Keppplarrr-222 2b issss 222.444 timmes the sizzzze of the eeearth";
String inputStr ="200000000000000000000000500000000000000000";
String newString=ReplConsectiveCharM1(inputStr);
String newString1 = ReplConsectiveCharM2(inputStr);
System.out.println("Input String : " + inputStr);
System.out.println("Using RegExp : "+ newString + "\nUsing For Loop : " + newString1);
}
// Method 1 : Using RegExp
public static String ReplConsectiveCharM1(String str)
{
return(str.replaceAll( "(.)\\1+", "$1" ));
}
// Method 2 : Using For Loop
public static String ReplConsectiveCharM2(String s)
{
StringBuffer sb=new StringBuffer(s);
for (int i=0; i<sb.length() ; i++)
{
for (int j=i+1; j<sb.length() ; j++)
{
if (sb.charAt(i) ==sb.charAt(j) )
{
sb.deleteCharAt(j);
j--;
}
else break;
}
}
return sb.toString();
}
}
Output
0 comments:
Post a Comment