Yahoo Interview Question
Software Engineer / Developersvery nice amishera.... we got to use regexpression here.... one small correct to above. We need to replace us with " them " and trim it. Else your solution will not work with the string "us It always rains when the us bus comes to us"
public class ReplaceTest {
/**
* @param args
*/
public static void main(String[] args) {
String abc = "us It always rains when the us bus comes to us";
System.out.println(abc.replaceAll("(^| )us($| )"," them ").trim());
}
} //
\\bus\\b
package com.test;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MatcherDemo {
private static final String REGEX = "\\bus\\b";
private static final String INPUT = "Us? It usually rains when bus comes to us";
public static void main(String[] args) {
Pattern p = Pattern.compile(REGEX);
Matcher m = p.matcher(INPUT); // get a matcher object
int count = 0;
while(m.find()) {
count++;
System.out.println("Match number "+count);
System.out.println("start(): "+m.start());
System.out.println("end(): "+m.end());
}
String s = INPUT;
System.out.println("Input: " + s);
s = s.replaceAll(REGEX, "them");
System.out.println("Output: " + s);
}
}
public class usTOthem
{
public static void main(String[] args)
{
String st = "Us? It usually rains when bus comes to us";
String splitArr[] = st.split(" ");
for (int i = 0; i < splitArr.length; i++)
{
if(splitArr[i]=="us")
{
splitArr[i] = "them";
}
}
for(String modified: splitArr)
{
System.out.println(modified);
}
}
}
But still not working..help
There's a similar problem with solutions under the Epic Systems section.
- soc November 02, 2009