Oracle Interview Question
Software Engineer in TestsCountry: India
Interview Type: In-Person
private static void replaceData(String userInput, String[] datatoReplace) {
String[] userArr = userInput.split(" ");
for(int i = 0; i < userArr.length ; i++) {
if(userArr[i].equals(datatoReplace[0])){
finalOutput.append(datatoReplace[1]);
finalOutput.append(" ");
}
else {
finalOutput.append(userArr[i]);
finalOutput.append(" ");
}
}
// datatoreplace array with oldword,newword.
Please review my code and provide any better solution.
public class ReplaceSubString{
static String s = "kanha is a good boy & he is the best";
static int count,i;
static char[] ch;
static char[]ch1 = new char[100];
static void replace(String ss,String sou,String dest)
{
ch =s.toCharArray();
int jj=0;
for(int j =0;j<ch.length;j++)
{
ch1[jj++]=ch[j];
count =0;
i=0;
if(sou.charAt(i)==ch[j])
{
count++;
int jjj=j;
while(i<sou.length()-1)
{
if(sou.charAt(++i)==ch[++jjj])
count++;
}
if(count == sou.length())
{
int kk=0;
j=jjj;
int ptr = --jj;
for(int iii=ptr;iii<ptr+dest.length();iii++)
{
ch1[iii] = dest.charAt(kk++);
jj++;
}
}
}
}
}
public static void main(String[] args) throws Exception
{
replace(s,"kanha","kanhaiya");
for(char chh:ch1)
System.out.print(chh);
}
}
Here is just another solution to the same problem.
public class StringPractice {
public static void main(String[] args) {
String originalStr = "prasanthi and madhu are one and the same";
String replaceThis = "madhu";
String replaceWith = "madhumita";
String finalStr = replaceSubstring(originalStr, replaceThis,
replaceWith);
System.out.println(originalStr);
System.out.println(finalStr);
}
public static String replaceSubstring(String originalStr,
String replaceThis, String replaceWith) {
String finalStr = "";
char[] original = originalStr.toCharArray();
char[] replaceThisArr = replaceThis.toCharArray();
int tbrLength = replaceThis.length();
for (int i = 0; i < original.length; i++) {
if (original[i] == replaceThisArr[0]
&& original.length - i >= tbrLength) {
String tempStr = "";
for (int j = 0; j < tbrLength; j++) {
tempStr += original[i + j];
}
if (tempStr.equals(replaceThis)) {
finalStr += replaceWith;
i = i + tbrLength - 1;
} else {
finalStr += original[i];
}
} else {
finalStr += original[i];
}
}
return finalStr;
}
}
package example;
public class Test {
public static void main(String[] args) {
String input = "abcdecdfg";
String oldSubStr = "abcdecdfgs";
String newSubStnr = "**";
String result = replace(input, oldSubStr, newSubStnr);
System.out.println(result);
}
private static String replace(String input, String oldSubStr,
String newSubStr) {
String result = null;
if (input.contains(oldSubStr)) {
String[] arr = input.split(oldSubStr);
if (arr.length != 0) {
result = arr[0];
for (int j = 1; j < arr.length; j++) {
result = result + newSubStr + arr[j];
}
}else{
result=newSubStr;
}
} else {
result = input;
}
return result;
}
}
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
StringBuffer sb = new StringBuffer();
System.out.println("Enter the actual string");
String s1 = in.nextLine(); // Actual String
System.out.println("Enter the string to be replaced");
String s2 = in.nextLine(); // String to be replaced
System.out.println("Enter the replacing string");
String s3 = in.nextLine(); // Replacing string
String start;
String end;
while(s1.indexOf(s2) != -1)
{
int k = s1.indexOf(s2);
int p = k+s2.length();
start = s1.substring(0,k);
end = s1.substring(p,s1.length());
s1 = start+s3+end;
}
System.out.println("The string after all replacements = "+s1);
}
String replace(String str, char oldc, char newc){
char[] starr = str.toCharArray();
for(int i=0; str[i]!='\0'; i++)
if(starr[i]==oldc)
starr[i] = newc;
return new String(starr);
}
Here is the code...its not readable but let me know if this is ok..This will replace all occcurence of specified string if both are of same length. I will make the code readable if logic seems fine to you.
- anuj71307 May 21, 2013public String replace(String str,String strreplace,String replacewith) throws Exception{
char[] charStr=str.toCharArray();
char charreplace[]=strreplace.toCharArray();
char replacewit[]=replacewith.toCharArray();
if(strreplace.length()!=replacewith.length()){
throw new Exception();
}else{
if(strreplace.length()>str.length()){
throw new Exception();
}else{
int[] a=new int[charreplace.length];
int k=0;
for(int i=0;i<charStr.length;i++){
for(int j=0;j<charreplace.length;j++,i++){
if(charStr[i]==charreplace[j]){
k++;
a[j]=i;
continue;
}else{
k=0;
break;
}
}
if(k==charreplace.length){
for(int m=0;m<a.length;m++){
charStr[a[m]]=replacewit[m];
System.out.print(a[m]+" ");
}
}
}
}}
return new String(charStr);
}