Epic Systems Interview Question
Software Engineer / DevelopersIn the question, "can't" will be separated to "ca n't", but it seems like your code doesn't do that.
Taking care of string with odd lenght and even length greater than 4
public class ReplaceWordWithSpaceLengthGreaterthan4 {
public static void main(String[] args){
String strinput = "A persons cant walk in this street";
//System.out.println(replace(strinput));
String t = StringReplace(strinput);
System.out.println(t);
}
public static String StringReplace(String str) {
String temp = "";
String regex = " ";
String[] words = str.split(regex);
StringBuffer sb = new StringBuffer();
for(int i=0;i< words.length;i++) {
int len = words[i].length()/2;
if(words[i].length() >= 4 && words[i].length()%2 == 0){
sb.append(words[i].substring(0, len));
sb.append(' ');
sb.append(words[i].substring(len, words[i].length()));
}else if (words[i].length() >= 4 && words[i].length() % 2 == 1){
sb.append(words[i].substring(0, len));
sb.append(' ');
sb.append(words[i].substring(len+1, words[i].length()));
}else {
sb.append(words[i]);
}
sb.append(' ');
}
return sb.toString();
}
Correct answer to the problem is
package b;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class StringReplace {
public static void main(String[] args){
String s = "A person can't walk in this street";
String op = "";
Scanner in = new Scanner(s);
while(in.hasNext()){
String token = in.next();
Pattern p = Pattern.compile("\\W");
Matcher m = p.matcher(token);
int count = 0;
while(m.find()){
count++;
}
int trueLength = token.length() - count;
if(trueLength >= 4 && trueLength % 2 ==0){
String t1 = token.substring(0,trueLength / 2);
String t2 = token.substring(trueLength /2);
op += t1.concat(" ").concat(t2).concat(" ");
}
else{
op += token.concat(" ");
}
}
System.out.println(op);
}
}
int main()
{
string s = "Away person can't walk in this street";
string temp = "";
string rem = s;
int start = 0;
for(int i = 0; i < rem.length();i++)
{
if(rem[i] == ' ')
{
int diff = i - start;
cout<<diff<<endl;
if((diff%2 == 0)&&(diff>=4))
{
temp = temp + rem.substr(0,start+diff/2) +" "+
rem.substr(start + (diff/2),(diff/2));
rem = rem.substr(i,rem.length());
i = 0;
}
start = i+1;
}
}
int i = 1;
start = i;
while(rem[i]!= '\0')
i++;
int diff = i - start;
if((diff%2 == 0)&&(diff>=4))
{
temp = temp + rem.substr(0,start+diff/2)+ " " + rem.substr(start+(diff/2),(diff/2));
rem = rem.substr(i,rem.length());
}
cout<<temp<<endl;
cout<<rem;
getch();
return 0;
}
public static void main(String[] args)
{
String input = "A person can't walk in this street";
String[] words = input.split(" ");
StringBuffer sbf = new StringBuffer();
char[] tempStr;
int counter=0;
for(int i=0; i<words.length; i++)
{
tempStr = words[i].toCharArray();
counter=0;
for(int j=0; j<tempStr.length; j++)
{
if((tempStr[j]>='a' && tempStr[j]<='z') || (tempStr[j]>='A' && tempStr[j]<='Z'))
{
counter++;
}
}
if(counter>=4 && counter%2==0)
{
for(int j=0; j<tempStr.length; j++)
{
sbf.append(tempStr[j]);
if(j==(counter/2)-1)
sbf.append(" ");
}
}
else
{
sbf.append(tempStr);
}
sbf.append(" ");
}
System.out.println(sbf.toString());
}
import java.util.StringTokenizer;
public class AddSpaceBetweenLetters {
/**
* Given a string.Replace the words whose lengt>=4 and is even,with a space between
* the two equal halves of the word.consider only alphabets for finding the eveness
* of the word
I/P "A person can't walk in this street"
O/P "A per son ca n't wa lk in th is stre et"
13
*/
public static void main(String[] args) {
String str="A person can't walk in this street";
String temp="";
String newStr="";
int wordSize;
System.out.println("*******OLD******"+str);
StringTokenizer stokens=new StringTokenizer(str);
while(stokens.hasMoreTokens())
{
temp=stokens.nextToken();
wordSize=temp.length();
char[] c=new char[wordSize+1];
if(wordSize>=4&&(wordSize%2)==0)
{
for(int i=0;i<wordSize;i++)
{
if(i<wordSize/2)
{
c[i]=temp.charAt(i);
}
if(i==wordSize/2)
{
c[i]=' ';
}
if(i>wordSize/2&&i<=wordSize)
{
c[i]=temp.charAt(i-1);
}
c[wordSize]=temp.charAt(wordSize-1);
}
String t=new String(c);
newStr=newStr+" "+t;
}
else
{
newStr+=" "+temp;
}
}
System.out.println("******NEW******"+newStr);
}
}
<pre lang="" line="1" title="CodeMonkey20598" class="run-this">import java.io.*;
import java.util.*;
public class ReplaceWord {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input = br.readLine();
br.close();
String output = "";
StringTokenizer st = new StringTokenizer(input);
while (st.hasMoreTokens()) {
output += getConvertString(st.nextToken());
if (st.hasMoreTokens()) {
output += " ";
}
}
System.out.println(output);
}
public static String getConvertString(String input) {
String output = "";
int length = 0;
for (int i = 0; i < input.length(); ++i) {
char current = input.charAt(i);
if (current != ',' && current != '!' && current != '"' && current != '?' && current != '\'') {
++length;
}
}
if (length % 2 == 0 && length >= 4) {
int size = length / 2;
int setted = 0;
for (int i = 0; i < input.length(); ++i) {
char current = input.charAt(i);
if (setted < size) {
if (current != ',' && current != '!' && current != '"' && current != '?' && current != '\'') {
++setted;
}
}
output += current;
if (setted == size) {
output += " ";
++setted;
}
}
return output;
}
return input;
}
}
</pre><pre title="CodeMonkey20598" input="yes">
</pre>
#include<iostream>
using namespace std;
int count_only_alphabet(string s)
{
int cnt=0;
for (int i=0;i<s.length();i++)
if (('a'<=s[i] && s[i]<='z') || ('A'<=s[i] && s[i]<='Z'))
cnt++;
return cnt;
}
string split(string s, int count)
{
int i=0,b=0;
while (b<s.length() && i<count/2)
{
if (('a'<=s[b] && s[b]<='z') || ('A'<=s[b] && s[b]<='Z'))
i++;
b++;
}
s.replace(b,0," ");
return s;
}
string replace_simple(string str)
{
string temp="";
int space=str.find(' ');
int i=0;
string word=str.substr(i,space-i);
while (word!="")
{
int count=count_only_alphabet(word);
if (count>=4 && count%2==0)
{
word=split(word,count);
}
temp=temp+" "+word;
if(space==string::npos){break;}
i=space+1;
space=str.find(' ',i);
word=str.substr(i,space-i);
}
return temp;
}
int main()
{
cout<< "Enter the string \n";
string s;
getline(cin,s);
cout<<replace_simple(s);
}
#include<iostream>
#include <string>
#include <list>
using namespace std;
int main()
{
string s,temp;
int counter=0;
getline ( cin,s );
int k=0;
list<string> l;
list<string> z;
list<string>::iterator itr,ztr;
for ( int i=0; i<s.length(); i++ )
{
if ( s[i]==' ' )
{
l.push_back ( s.substr ( k,i-k ) );
k=i+1;
}
}
l.push_back ( s.substr ( k,s.length()-k ) );
ztr=z.begin();
for ( itr=l.begin(); itr!=l.end(); itr++ )
{
temp=*itr;
counter=0;
for ( int w=0; w<temp.size(); w++ )
{
if ( ( temp[w]>='a'&&temp[w]<='z' ) || ( temp[w]>='A'&&temp[w]<='B' ) ) {
counter++;
}
}
if ( counter >=4 && counter %2==0 )
{
z.push_back ( temp.substr ( 0,counter/2 ) ) ;
z.push_back ( temp.substr ( counter /2 ,counter ) ) ;
}
else {
z.push_back ( temp ) ;
}
}
for ( ztr=z.begin(); ztr!=z.end(); ztr++ )
{
cout<< *ztr<<" ";
}
getchar();
}
public class ReplaceString1 {
/**
* Given a string.Replace the words whose lengt>=4 and
* is even,with a space between the two equal halves of the word.
* consider only alphabets for finding the eveness of the word
I/P "A person can't walk in this street"
O/P "A per son ca n't wa lk in th is stre et"
*
*/
public static void main(String[] args) {
String str = "A person can''t walk in this street";
String mStr = replace(str);
System.out.println(mStr);
}
private static String replace(String str) {
StringBuffer sb = new StringBuffer();
String[] tokens = str.split(" ");
for(String s: tokens) System.out.println(s);
for(String s: tokens){
if(len(s)<4) {
sb.append(s);
sb.append(" ");
}
else{
if(len(s)%2==0){
sb.append(s.substring(0,len(s)/2));
sb.append(" ");
sb.append(s.substring(len(s)/2) + " ");
}
else{
sb.append(s + " ");
}
}
}
return sb.toString();
}
public static int len(String s){
int totalLen = s.length();
int count = 0;
for(char c:s.toCharArray()){
if(c== '\'') count++;
}
return totalLen - count;
}
}
if __name__ == "__main__":
inputStr = "A person can't walk in this street"
strList = inputStr.split(" ")
print strList
for x in strList:
if (len(x) >= 4) and ((len(x)%2) == 0):
strList[strList.index(x)] = x[0:len(x)/2] + " " + x[len(x)/2:len(x)+1]
print strList
outputStr = ''
for x in strList:
outputStr += x
if strList.index(x) != (len(strList)-1):
outputStr += " "
print outputStr
package practice;
import java.util.*;
public class SplitString {
public String Split(String s){
String regex=" ";
s.trim();
String str1,str2;
String[] part=s.split(regex);
StringBuilder sb=new StringBuilder();
for(int i=0;i<part.length;i++){
char[] temp=part[i].toCharArray();
int counter=0;
for(int j=0; j<temp.length; j++)
{
if((temp[j]>='a' && temp[j]<='z') || (temp[j]>='A' && temp[j]<='Z'))
{
counter++;
}
}
if(counter>=4 && counter%2==0)
{
for(int j=0; j<temp.length; j++)
{
sb.append(temp[j]);
if(j==(counter/2)-1)
sb.append(" ");
}
}
else
{
sb.append(temp);
}
sb.append(" ");
}
return sb.toString();
}
public static void main(String[] args){
SplitString s1=new SplitString();
String result=s1.Split("A person can't walk in this street");
System.out.print(result);
}
}
package practice;
import java.util.*;
public class SplitString {
public String Split(String s){
String regex=" ";
s.trim();
String str1,str2;
String[] part=s.split(regex);
StringBuilder sb=new StringBuilder();
for(int i=0;i<part.length;i++){
char[] temp=part[i].toCharArray();
int counter=0;
for(int j=0; j<temp.length; j++)
{
if((temp[j]>='a' && temp[j]<='z') || (temp[j]>='A' && temp[j]<='Z'))
{
counter++;
}
}
if(counter>=4 && counter%2==0)
{
for(int j=0; j<temp.length; j++)
{
sb.append(temp[j]);
if(j==(counter/2)-1)
sb.append(" ");
}
}
else
{
sb.append(temp);
}
sb.append(" ");
}
return sb.toString();
}
public static void main(String[] args){
SplitString s1=new SplitString();
String result=s1.Split("A person can't walk in this street");
System.out.print(result);
}
}
- rv April 10, 2010