Interview Question
Country: India
Interview Type: In-Person
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);
}
}
# 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()
//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;
}
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);
}
}
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);
}
}
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"));
}
}
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);
}
}
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());
}
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));
}
}
// 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;
}
#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;
}
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);
}
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