Interview Question


Country: India
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
2
of 2 vote

Use KMP algo to find all occurrences of the word to be replaced, store the positions in an auxiliary array, allocate extra memory required (if the length of word to be replaced is more than the one being replaced). Use the positions array to replace.

- HardCode June 30, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

There is a new mahatma in town. Born in 1969.

- Anonymous June 30, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

looking for C/C++ solution

- vijay July 03, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

"Should be in-place and do a copy only one time".

LOL. Get your question straight.

- Anonymous June 30, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class CareerCup2 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		
		StringBuilder  str3=new StringBuilder("Gandhi was born in 1969. Gandhi was from gujrat. ");
		String oldString="Gandhi";
		String newString="mahatma";
		System.out.println("before conversion the string:"+str3);
		str3.replace(str3.indexOf(oldString), (str3.indexOf(oldString)+oldString.length()), newString);
		System.out.println("after conversion the string:"+str3);
	}

}

- yugandharr147 July 01, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The above code will replace only first instance of Gandhi with mahatma.How would you handle if its to be done for all instances.

- Vijay July 07, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

# Replace all occurrence of particular word with another word of larger length
# e.g. Gandhi was born in 1969. replace gandhi with mahatma

def main():
	string = raw_input("Please enter a string> ")
	old_word = raw_input("What word do you want to replace>")
	new_word = raw_input("What do you want to replace it with> ")
	print string.replace(old_word,new_word)

if __name__ == "__main__":
    main()

- MR July 09, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//Case Sensitive Solution

#include <iostream>
using namespace std;
#include<string>

int main()
{

string input,remove,replacement;
getline(cin,input);
getline(cin,remove);
getline(cin,replacement);


while(input.find(remove) != string::npos)
{input.replace(input.find(remove),6,replacement);}

cout<<input;
return 0;
}

- Kashish Singhal July 10, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.*;
import java.util.StringTokenizer.*;
public class StringTest {

String cat;
public static void main(String[] args) {
// TODO Auto-generated method stub

System.out.println("enter string which is repeating in sentence quite often.");
Scanner s = new Scanner (System.in);
String bumblebee = s.nextLine();
String cat = new String();
System.out.println("\nenter word which you want to replace \n");
String higuen = s.nextLine();
System.out.println("\nenter word with which you want to replace your text\n");
String robben = s.nextLine();
StringTokenizer st = new StringTokenizer(bumblebee," ");
while(st.hasMoreTokens())
{
String van = st.nextToken();
if(van.equals( higuen))
{
cat = cat + robben + " ";

}
else
{
cat =cat + van + " ";

}

}

System.out.println(cat);

}

}

- Anonymous July 12, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

people i haven't use toLowercase method in string input but u can use it. If user inputs in capital letter.

- akshay July 12, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.*; 
import java.util.StringTokenizer.*; 
public class StringTest { 

String cat; 
public static void main(String[] args) { 
// TODO Auto-generated method stub 

System.out.println("enter string which is repeating in sentence quite often."); 
Scanner s = new Scanner (System.in); 
String bumblebee = s.nextLine(); 
String cat = new String(); 
System.out.println("\nenter word which you want to replace \n"); 
String higuen = s.nextLine(); 
System.out.println("\nenter word with which you want to replace your text\n"); 
String robben = s.nextLine(); 
StringTokenizer st = new StringTokenizer(bumblebee," "); 
while(st.hasMoreTokens()) 
{ 
String van = st.nextToken(); 
if(van.equals( higuen)) 
{ 
cat = cat + robben + " "; 

} 
else 
{ 
cat =cat + van + " "; 

} 

} 

System.out.println(cat); 

} 

}

- Mr. inncredible July 13, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This i think is the right code.The code which interviewee was expecting.
well done sir.

- isha verma July 13, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

We could to a strtok on the word that is to be replaced. The array elements that we have obtained should be concatenated with the word that is to be replaced by in the middle.

- Anonymous July 17, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class StringReplace {

public static void main(String[] args) {
// TODO Auto-generated method stub


StringBuilder str3=new StringBuilder("Gandhi was born in 1969. gandhi was from gujrat. ");
String oldString="Gandhi";
String newString="mahatma";
System.out.println("Original:");
System.out.println(str3);
System.out.println(str3.toString().toLowerCase().replaceAll("gandhi", "Mahatma"));

}

}

- munnira July 23, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You could use strstr of c to get the first position of the substring and then replace that.
Keep running this loop till strstr returns NULL.

- Nim August 03, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String str="Gandhi was born in 1969. Gandhi was from gujarat";
String str1[]=str.split(" ");
str="";
for(int i=0;i<str1.length;i++)
{
if(str1[i].equals("Gandhi"))
str=str+"mahatma";
else
str=str+str1[i];

if(i!=str1.length-1)
{
str=str+" ";
}

}
System.out.println(""+str);

- Anonymous August 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class String_Gandhi {

public static void main(String[] args) {


String S1 = "Gandhi was born in 1969. Gandhi was from gujrat.";
String String_new = "Mahatma ";
String String_to_be_replaced= "Gandhi";
String temp[]=new String[20];
String Final_String="";
int k=0;
temp[k]="";
for(int i=0;i<S1.length();i++){
temp[k]+= S1.charAt(i);
if(S1.charAt(i)==' '){
k++;
temp[k]="";

}


}

for(int i=0;i<=k;i++){

if(temp[i].contains(String_to_be_replaced)){
temp[i]=String_new;
}

Final_String+= temp[i];
}
System.out.println(Final_String);
}

}

- Zuhair August 09, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void replace()
{
String str3=new String("Gandhi was born in 1969. Gandhi was from gujrat. ");
StringBuilder builder = new StringBuilder();
String oldString="Gandhi";
String newString="mahatma";
String[] tokens = str3.split(" ");

for(String str : tokens)
{
if(str.equals(oldString))
{
builder.append(newString + " " );
}
else
builder.append(str + " ");
}
System.out.println(builder.toString());

}

- Anonymous September 03, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.Scanner;


public class ReplaceWord {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		// TODO Auto-generated method stub
		
		System.out.println("Enter a string");
		Scanner input = new Scanner(System.in);
		String s1 = input.nextLine();
		
		System.out.println("Enter a word to be replaced:");
		Scanner input1 = new Scanner(System.in);
		String tempWord = input1.nextLine();
		
		System.out.println("Enter a word to be added:");
		Scanner input2 = new Scanner(System.in);
		String finalWord = input2.nextLine();
		
		System.out.println(s1.replaceAll(tempWord, finalWord));
		
		

	}

}

- Ritika September 12, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

In java, String object is immutable. When you do a replace or replaceAll, you get a new instance of the string. As a result, the replacement is not in place.

- Anonymous October 01, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static String replaceWord(String str){
String answer = str.replaceAll("Gandhi", "mahatma");
return answer;
}

- Abhishek Kumar Gupta October 08, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// os --> original string
// str --> string need to be replaced

char* replaceLargerString(char *os, char *str,char *newString){
int sc=0,i=0,j=0,length=0,osLen=0;
sc = strlen(newString)- strlen(str);
length = strlen(str);
if(sc >= 0){
while(*(os+i)){
if(*(os+i) == *(str+j)){
j++;
}
else
j=0;
if(j==length){
// match exactly now shift the characters.
osLen = strlen(os);
while(osLen!=i){
*(os+osLen+sc) = *(os+osLen);
osLen--;
}
//copy the new string
int cur=i-(j-1);
j=0;
while(*(newString+j)){
cout << *(newString+j) << cur << endl;
*(os+cur) = *(newString+j);
j++;
cur++;
}
}
i++;
}
}
return os;
}

- Sujan December 30, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <string>
using namespace std;

string replaceWithString(string test, string s_to_find, string s_to_replace)
{
  size_t pos = test.find(s_to_find);
  while(pos != string::npos)
  {
    test.erase(pos, s_to_find.length());
    test.insert(pos, s_to_replace);
    pos = test.find(s_to_find);
  }
  return test;
}

int main() 
{  
  string test = "Gandhi was born in 1969. Gandhi was from gujrat.";

  string result = replaceWithString(test, "Gandhi", "Mahatma");
  
  cout << result << endl;
  
  return 0;

}

- benxavior October 07, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

in java....
public static void main(String[] args) throws IOException {
String k="gandhi was born in 1969.gandhi was from gujarat";
k=k.replace("gandhi", "mahatma");
System.out.println(k);

}

- keshav June 30, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

replace() in java does not work in place.

- Ghosh June 30, 2014 | Flag


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More