Epic Systems Interview Question
Software Engineer / Developersimport java.lang.*;
import java.util.*;
public class One
{
public static void getString (String s)
{
String output="";
StringTokenizer stk = new StringTokenizer(s);
String tok="";
while(stk.hasMoreElements())
{
tok = stk.nextToken();
if(tok.equals("A"))
{
tok = "ONE";
}
if(tok.equals("a"))
{
tok = "one";
}
output = output +" "+ tok;
}
System.out.println(output);
}
public static void main(String args[])
{
getString("A boy is playing in a garden");
}
}
String stringReplace(String str)
{
String regex = " ";
String str1 = "a";
String str2 = "A";
StringBuilder builder = new StringBuilder();
String[] parts = str.split(regex);
for(int i=0; i<parts.size; i++)
{
if (parts[i].equals(str1))
builder.add("one");
else if (parts[i].equals(str2))
builder.add("ONE");
else
builder.add(parts[i]);
if (i<parts.size-1)
builder.add(" ");
}
}
1. size cannot be used for finding array size it should be "parts.length"
2. StringBuilder class has no add() method. Use append()
3. return statement missing. Before returing use toString() so that we can convert to String.
4. Also instead of hard coding space " " in line
if (i<parts.size-1)
builder.add(" ");
use builder.append(regex);
Below is the complete code
public class HelloString{
public static void main(String[] args){
String t = stringReplace("A boy is playing in a garden");
System.out.println(t);
}
static String stringReplace(String str)
{
String regex = " ";
String str1 = "a";
String str2 = "A";
StringBuilder builder = new StringBuilder();
String[] parts = str.split(regex);
for(int i=0; i<parts.length; i++)
{
if (parts[i].equals(str1))
builder.append("one");
else if (parts[i].equals(str2))
builder.append("ONE");
else
builder.append(parts[i]);
if (i<parts.length-1)
builder.append(regex);
}
return builder.toString();
}
}
split is to split the given string into words based on the regex(here it is space)..so in guess in the above logic we can use. The algo is
1. get the string
2. split the string into words based on regex(space here)
3. loop into the words and replace it based on the given criteria
4. return the modified string
When you can use regex y can't you guys just use the regex to replace the char??
Below is the VB.NEt code...
Imports System.Text.RegularExpressions
Module Replace
Sub main()
System.Console.Write("Please Enter the Sentance/ String to be replaced: ")
Dim Str As String = System.Console.ReadLine() '"A boy is playing in a garden"
Str = Regex.Replace(Str, "(( )+a( )+)|(^a( )+)|(( )+a$)", " one ")
Str = Regex.Replace(Str, "(( )+A( )+)|(^A( )+)|(( )+A$)", " ONE ")
System.Console.WriteLine(Str)
System.Console.Read()
End Sub
End Module
replace_A_and_a(char* mystring)
{
char* buff = mystring;
string replacedString;
char* token;
vector<string> vec;
token = strtok(buff, " ");
cout << token << endl;
vec.push_back(token);
while(token != NULL)
{
token = strtok(NULL, " ");
if(token)
{
cout << token << endl;
vec.push_back(token);
}
}
cout << vec.size() << endl;
for(int count = 0; count < vec.size(); ++count)
{
if(vec[count] == "A")
{
vec[count] = "ONE";
}
else if(vec[count] == "a")
{
vec[count] = "one";
}
replacedString += vec[count];
if(count < vec.size())
{
replacedString += " ";
}
}
cout << replacedString << endl;
}
C# solution is
static string replaceStr(string _sent)
{
StringBuilder sb = new StringBuilder();
for(int i= 0; i < _sent.Length; i++)
{
if (_sent[i] == ('a'))
sb.Append("one");
else if (_sent[i] == ('A'))
sb.Append("ONE");
else sb.Append(_sent[i]);
}
return sb.ToString();
}
// you don't need to split on white space
// and string cannot compare anystring[i].equals(somestring)
// it should be string[i] == ('Character') because anystring[i] returns character
public class General {
static String replace = "A in the a fora testA";
public static void main(String[] args) {
replace= replace.replaceAll(" A ", " ONE ");
replace= replace.replaceAll("A ", "ONE ");
replace= replace.replaceAll(" A", " ONE");
replace= replace.replaceAll(" a ", " one ");
replace= replace.replaceAll("a ", "one ");
replace= replace.replaceAll(" a", " one");
System.out.println(replace);
}
}
it was never mentioned in the Qs to write huge chunk of shitty codes.
Be smart and use the following one-liner to do the job and try it out as well
ubuntu@ubuntu:~$ echo " A boy is playing in a garden"|sed 's/ a / one /' |sed 's/ A / ONE /'
ONE boy is playing in one garden
ubuntu@ubuntu:~$
Thats it baby!
namespace stringbuilder
{
class Program
{
static void Main(string[] args)
{
Program p = new Program();
Console.WriteLine("Enter string:");
string str = Console.ReadLine();
string newstr = p.ReplaceString(str);
Console.WriteLine(newstr);
Console.ReadLine();
}
public string ReplaceString(string str)
{
string sendstr = "";
string[] sendstr1 = splitstring(str);
//char[] newstr = str.ToCharArray();
for (int i = 0; i < sendstr1.Length; i++)
{
if (sendstr1[i].Equals("a"))
{
sendstr = sendstr + "one";
}
else if (sendstr1[i].Equals("A"))
{
sendstr = sendstr + "ONE";
}
else
{
sendstr = sendstr + sendstr1[i];
}
}
return sendstr;
}
public string[] splitstring(string str)
{
char[] charstr = str.ToCharArray();
int token = 0, initstring = 0; ;
for (int i = 0; i < charstr.Length; i++)
{
if (charstr[i] == ' ')
{
if(initstring>0)
{ token++; }
token++;
initstring = 0;
}
else if (i == charstr.Length - 1)
{
token++;
initstring++;
}
else
{
initstring++;
}
}
string[] strings = new string[token];
for (int i = 0; i < token; i++)
{
strings[i] = "";
}
int j = 0,chk = 0;
for (int i = 0; i < charstr.Length; i++)
{
if (charstr[i] == ' ')
{
j++;
strings[j] = strings[j] + charstr[i];
//if (chk > 0)
//{
// j++;
// chk = 0;
//}
}
else
{
if (i > 0)
{
if (charstr[i - 1] == ' ')
{
j++;
}
}
strings[j] = strings[j] + charstr[i];
}
}
return strings;
}
}
}
#include<string>
#include<sstream>
using namespace std;
int main()
{
string str = "A boy is playing in a garden";
string final = "";
string word;
stringstream stream(str);
while(getline(stream, word, ' '))
{
if(word == "A")
final = final + "ONE ";
else if(word == "a")
final = final + " one ";
else
final = final + " " + word + " ";
}
cout << final << endl;
return 0;
}
package EPIC;
import java.util.StringTokenizer;
public class ReplaceAaONEone {
public static String getString(String s) {
StringTokenizer ST = new StringTokenizer(s);
String next = "";
String output = "";
while (ST.hasMoreElements()) {
next = ST.nextToken();
if (next.equals("A")) {
output += "ONE ";
} else if (next.equals("a")) {
output += "one ";
} else {
output += next + " ";
}
}
System.out.println(output);
return output;
}
public static void main(String args[]) {
getString("A boy is playing in a garden.");
}
}
public class charReplacement {
public static void main(String[] args) {
String str = "BanAna boy";
StringBuilder newString = new StringBuilder();
int i = 0;
while(i < str.length()){
if(str.charAt(i) != 'a' && str.charAt(i) != 'A'){
newString.append(str.charAt(i));
i++;
}
else{
if(str.charAt(i+1) != ' '){
newString.append(str.charAt(i));
i = i+1;
}
else if(str.charAt(i) == 'a' && str.charAt(i+1)==' ' && str.charAt(i-1)==' ' ){
newString.append("one ");
i = i+2;
}
else if(str.charAt(i) == 'A'&& str.charAt(i+1)==' ' && str.charAt(i-1)==' '){
newString.append("ONE ");
i = i+2;
}
else{
newString.append(str.charAt(i));
i+=1;
}
}
}
System.out.println(newString.toString());
}
}
- Anonymous March 22, 2009