Epic Systems Interview Question
Software Engineer / DevelopersClass Prob
{
String Actual="A man walks in with the Bat";
String converted="";
StringTokenizer st= new StringTokenizer(Actual);
if(st.hasMoreTokens())
{
if(st.nextToken.equals("a")
Converted= Converted + "the";
else
Converted= Converted + st.nextToken;
}
System.out.Println(Converted);
}
Am a fresher.. Do correct me if I am wrong
char str= "this is a test string";
for(i=0;i<strlen(str);i++)
{
if(str[i]=='a' && str[i+1]==' ' && str[i-1]==' ')
{
replace(str, 'a', "the");
}
printf("%c", str);
#include<stdio.h>
#include<string.h>
int main()
{
int i,k;
char buf1[100];
char buf2[]="aaaaaaaaa";
int j=strlen(buf2);
printf("The length of the string is %d\n",j);
k=0;
for(i=0;i<j;i++)
{
if(buf2[i]=='a')
{
buf1[k++]='t';
buf1[k++]='h';
buf1[k++]='e';
}
else
buf1[k++]=buf2[i];
}
k=0;
while(buf1[k]!='\0')
{
printf("%c",buf1[k]);
k++;
}
}
I am a Finance major and was called for S/W dev.becoz I have an undergrad in CS. I am sure I cannot write code like you guys but I am just sharing my code too
Here is the same man again... Reliability 101%
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define MAX 100
void replace(char[],int);
int main()
{
char str[MAX]="Brendon Mccullum smashes a ball a long way!";
int i=0;
int flag=0;
printf("\nEnter a string: ");
gets(str);
printf("\n%s",str);
while(str[i]!='\0')
{
flag=0;
if(str[i]=='a')
{
if((str[i-1]==' ' && str[i+1]==' ') || (str[i-1]==' ' && str[i+1]=='\0'))
flag=1;
}
if(flag==1)
{
//printf("\nThe value of i should be 9. It is %d", i);
replace(str,i);
}
i++;
}
getch();
printf("\n%s",str);
getch();
return 0;
}
void replace(char a[],int pos)
{
int i=1,j=0,k=0,l=1;
//printf("\n%c",a[pos+2]);
j=strlen(a)-1;
//printf("\n%d",j);
a[j+3]='\0';
k=j-pos;
for(i=j,l=1;l<=k;i--,l++)
{
//printf("\nIt does work fine till here");
a[i+2]=a[i];
//printf("\nHere??? %d",i);
//getch();
}
a[i]='t';
a[i+1]='h';
a[i+2]='e';
}
//Making a new string << not changing the actual one >>
//Pesudo code for going in O(n) time and O(n) memory
1. Count No. of single 'a' in the string in O(n).
2. Allocate newstring = strlen(string)+3*num_of_a memory
3. Now scan the string from left and on coming of a put the and continue till end
Solution in python:
'''Replace all single occurrences of "a" with "the" (See question posted by Troy on Oct 7 2010)'''
def replace(str):
i = 0
res = ""
for s in str:
try:
test_condition = str[i-1] == ' ' and str[i+1] == ' ' and s == 'a'
except IndexError:
pass # ignore index out of range.
if test_condition:
res = res + 'the'
else:
res = res + s
i = i+1
return res
string = "This is not a town we were looking for; this is an apple orchard"
print replace(string)
# output: This is not the town we were looking for; this is an apple orchard
''' regex solution. achieves the same thing.'''
import re
re.sub(r'\ba\b', 'the', string)
Hmm.. seems like some of you guys did not even run your code to check its correctness..
class Main
{
public static void main(String[] args) throws java.lang.Exception {
String input = "This is not a town we were looking for; this is an apple orchard";
String result = "";
java.util.StringTokenizer st = new java.util.StringTokenizer(input);
while (st.hasMoreTokens()){
String curr = st.nextToken();
if (curr.equals("a") ) {
result += "the";
} else {
result += curr;
}
if (st.hasMoreTokens()){
result += " ";
}
}
System.out.println(result);
}
}
<pre lang="" line="1" title="CodeMonkey54817" class="run-this">#include <iostream>
#include <cstdio>
#include <string>
#include <sstream>
using namespace std;
int main(){
string line;
getline(cin,line);
istringstream iss(line);
string result="";
while(iss){
string sub;
iss>>sub;
if(sub!=""){
if(sub == "a"){
result+="the ";
}
else
result+=sub+ " ";
}
}
cout<<result<<endl;
return 0;
}</pre>
int main() {
int count = 0;
string str;
cout<<"Enter the string: ";
getline(cin,str);
string str2 = "the";
string :: iterator it;
cout << "Original String is: " << str;
for(unsigned int i=0;i<str.length();i++){
if(str[i]=='a'){
count++;
str.replace(i,1,str2);
}
}
cout<<endl;
for (it=str.begin(); it<str.end(); it++){
cout<<*it;
}
cout<<endl;
cout<<"number of 'a' occurrences are: "<<count<<endl;
return 0;
}
Read the Question; specifically prohibits use of inbuilt replace function for string manipulation.
int main()
{
string s = "I is a bad to do a work a";
string temp = "";
for(int i = 1; i < s.length();i++)
{
if(s[i-1] == ' ' && s[i] == 'a' &&
(s[i+1] == ' ' || s[i+1] == '\0'))
{
temp = temp + s.substr(0,i);
temp = temp + "the";
s = s.substr(i+1,s.length());
i = 1;
}
}
temp = temp + s;
cout<<temp;
}
/*Replace all single occurrences of "a" with "the"*/
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
string str="I am a spiderman.";
unsigned int i,count=0;
//getline(cin,str);
string ch="a";
for(i=0;i<str.length();i++)
{
if(str[i]=='a')
count++;
}
cout<<count<<endl;
//while count,find index of i and replace
size_t found;
while(count)
{
found=str.rfind(ch);
if (found!=string::npos)
str.replace (found,ch.length(),"the");
count--;
}
cout<<str<<endl;
//system("PAUSE");
return 0;
}
/*h t t p://codepad.org/zPETqIWO*/
/*Replace all single occurrences of "a" with "the"*/
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
string str="I am a spidermanaaci.";
unsigned int i,count=0;
//getline(cin,str);
string ch="a";
for(i=0;i<str.length();i++)
{
if(str[i]=='a')
count++;
}
cout<<count<<endl;
//while count,find index of i and replace
size_t found;
while(count)
{
found=str.rfind(ch);
if (found!=string::npos)
str.replace (found,ch.length(),"the");
count--;
}
cout<<str<<endl;
//system("PAUSE");
return 0;
}
this works i compiled it
int main()
{
char input[400],output[400];
int c=0,j=0,b=0;
cout<<"\n Enter string to replace 'A' with 'THE'\n";
cin>> input;
while (input[c] != '\0')
{
if (input[c] == 'a')
{
output[j]='t';
output[j+1]='h';
output[j+2]='e';
j+=3;
}
else
{
output[j]= input[c];
j++;
}
c++;
}
cout<<output;
return 0;
}
public class aReplacement
{
public static String sentence = "A parrot alone in the cage is eating a fruit";
public static String str[];
public static void main(String[] args)
{
str = sentence.split(" ");
for(int i=0;i<str.length;i++)
{
if(str[i].matches("a") || str[i].matches("A") )
{
str[i] = "the";
}
System.out.print(str[i]+" ");
}
}
}
/*
* Replace all single occurrences of "a" with "the"
*/
public class epic_OA_replaceAwithThe {
public static String replace (String input) {
String[] inputSplit = input.split(" a");
input = inputSplit[0];
for(int i=1; i<inputSplit.length; i++) {
input += " the"+inputSplit[i];
}
return input;
}
public static void main(String[] arges) {
System.out.println(replace("This is a English nove, which is a magic"));
}
}
import java.io.*;
public class Test{
public static void main(String args[]){
String input = "This is a sentence in English";
String[] inputSplit = input.split(" a ");
input = inputSplit[0];
for(int i=1; i<=inputSplit.length-1; i++) {
input += " the "+inputSplit[i];
}
System.out.print(input);
}
}
i=-1;
while(source_string[++i]!="\0")
{
if(string[i]!="t")
continue;
else
{
if(string[i+1]!="h")
continue;
else
{
if(string[i+2]!="e")
continue;
else
{
j=i;
source_str[j]="a";
while(source_str[++j+2]!="\0")
source_str[j]=source_str[j+2];
source_str[j]=source_str[j+2];
}
}
}
}
OR
- Anonymous March 24, 2011