Epic Systems Interview Question
Software Engineer / Developersvoid epic(char* str)
{
stdext::hash_map<char, bool> replace;
replace['a'] = true;
replace['e'] = true;
replace['i'] = true;
replace['o'] = true;
replace['u'] = true;
int exclusionnum = 3;
int replacenum = 4;
for(int i=strlen(str)-1; i--; i>=0)
{
if(replace.find(str[i])!=replace.end())
{
if(exclusionnum > 0)
exclusionnum--;
else
{
str[i] = (char)(str[i] - 'a' + 'A') ;
replacenum--;
if(replacenum == 0)
return;
}
}
}
}
int main(){
string str;
int count=0;
int replace = 0;
cout<<"Enter the String: ";
getline(cin,str);
for(unsigned int i=0;i<str.length();i++){
if ((int)str[i]==97 or (int)str[i]==101 or (int)str[i]==105 or (int)str[i]==111 or (int)str[i]==117){
count++;
}
}
cout << "Total count of a,e,i,o,u are: " << count << endl;
if (count<=7){
replace = (count-3);
}
else replace = 4;
for(unsigned int i=str.length()-1;i>0;i--){
if (replace>0){
if ((int)str[i]==97 or (int)str[i]==101 or (int)str[i]==105 or (int)str[i]==111 or (int)str[i]==117){
str[i] = (char)((int)str[i]-32);
replace--;
}
}
}
cout<<"New String is: " << str << endl;
return 0;
}
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String newstr = "";
String res = "";
String str = "an apple a morning keeps me yawning";
StringTokenizer st = new StringTokenizer(str);
while (st.hasMoreElements()) {
newstr = st.nextToken();
for (int i = 0; i < newstr.length(); i++) {
if (newstr.length() > 3) {
if (i > 2) {
if (newstr.charAt(i) == 'a') {
newstr = newstr.replace('a', 'A');
}
if (newstr.charAt(i) == 'e') {
newstr = newstr.replace('e', 'E');
}
if (newstr.charAt(i) == 'i') {
newstr = newstr.replace('i', 'I');
}
if (newstr.charAt(i) == 'o') {
newstr = newstr.replace('o', 'O');
}
if (newstr.charAt(i) == 'u') {
newstr = newstr.replace('u', 'U');
}
}
}
}
res = res + newstr + " ";
}
System.out.println(res);
}
}
<pre lang="" line="1" title="CodeMonkey46288" class="run-this">/*replace a,e,i,o,u with A,E,I,O,U
at most four eligible letters from the rear of the string are replaced
the first three eligible letters in the string are always exempted from replacement
*/
#include <stdio.h>
#include <string.h>
int main()
{
int i,tmp,length;
int count=0;
char a[100];
printf("Enter the string(max 100 characters)\n");
scanf("%s",a);
length = strlen(a);
for (i=0;i<length;i++){
if((a[i]=='a') || (a[i]=='e') || (a[i]=='i') || (a[i]=='o') || (a[i]=='u')){
count++;
}
}
if(count>3)
count = count-3;
else
count = 0;
tmp = count;
if(count>0){
for (i=length;i>0;i--){
if(a[i]=='a'){
a[i] = 'A';
count--;
}
if(a[i]=='e'){
a[i] = 'E';
count--;
}
if(a[i]=='i'){
a[i] = 'I';
count--;
}
if(a[i]=='o'){
a[i] = 'O';
count--;
}
if(a[i]=='u'){
a[i] = 'U';
count--;
}
if((tmp-count)==4)
break;
}
}
printf("Its %s\n",a);
return 0;
}
</pre><pre title="CodeMonkey46288" input="yes">String(max 100 characters)
</pre>
public class firEndAeiou {
public static void main(String[] args) {
String str = "an apple a morning keeps me yawning";
char[] s=str.toCharArray();
for(char c:s){
System.out.print(c);}
System.out.println("");
int count=0;
for(char c:s){
if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'){
count++;
}
}
int len=0;
if(count<=7){
len=count-3;
}
else{
len=4;
}
System.out.println(len);
for(int i=s.length-1;i>0;i--){
if(len>0){
if(s[i]=='a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u'){
s[i]=(char) (s[i]-'a'+'A');
len--;
}
}
}
for(char c:s){
System.out.print(c);}
}
}
Here is a working C++ code:
#include <cstdio>
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char arr[100];
int len, i, ct=0, ct1=0;
cin>>arr;
len=strlen(arr);
for (i=0; i<len; i++)
{
if (arr[i]=='a' || arr[i]=='e' || arr[i]=='i' || arr[i]=='o' || arr[i]=='u')
{
ct++;
}
}
cout<<"ct: "<<ct<<endl;
if (ct<=7)
{
for (i=0; i<len; i++)
{
if ((arr[i]=='a' || arr[i]=='e' || arr[i]=='i' || arr[i]=='o' || arr[i]=='u') && ct1<3)
{
ct1++;
cout<<arr[i];
}
else if (arr[i]=='a' || arr[i]=='e' || arr[i]=='i' || arr[i]=='o' || arr[i]=='u')
{
cout<<char(toupper(arr[i]));
}
else
cout<<arr[i];
}
}
else
{
ct-=4;
cout<<"new ct: "<<ct<<endl;
for (i=0; i<len; i++)
{
if ((arr[i]=='a' || arr[i]=='e' || arr[i]=='i' || arr[i]=='o' || arr[i]=='u') && ct1<ct)
{
ct1++;
cout<<arr[i];
}
else if (arr[i]=='a' || arr[i]=='e' || arr[i]=='i' || arr[i]=='o' || arr[i]=='u')
{
ct1++;
cout<<char(toupper(arr[i]));
}
else
{
cout<<arr[i];
}
}
}
return 0;
}
public class replaceaeiou {
public static void replaceChar(String str){
char[] strCh = str.toCharArray();
int i=0;
int count1=0;
int count2=0;
for(;i<str.length();i++){
if(strCh[i]=='a'||strCh[i]=='e'||strCh[i]=='i'||strCh[i]=='o'||strCh[i]=='u')
count1++;
if(count1==3)
break;
}
if(count1>=3){
for(int j=str.length()-1;j>i;j--){
if(strCh[j]=='a'||strCh[j]=='e'||strCh[j]=='i'||strCh[j]=='o'||strCh[j]=='u'){
strCh[j] = Character.toUpperCase(str.charAt(j));
count2++;
if(count2>4)
break;
}
}
}
System.out.println(strCh);
}
public static void main(String[]args){
String str = "A quick sly fox jumps over the lazy dog";
replaceChar(str);
}
}
My solution. complied and tested. Scope for improvement.
public class StringReplace {
static char vowels [] = {'a', 'e', 'i', 'o', 'u'};
static char replacements [] = {'A', 'E', 'I', 'O', 'U'};
public static void replaceChars(String input) {
int i, j, third_vowel_pos = 0;
int n = input.length();
int count = 0;
String output = "";
boolean isVowel = false;
for(i = 0; i < n; ++i) {
isVowel = false;
for(j =0; j < vowels.length;++j) {
if(input.charAt(i) == vowels[j]) {
count++;
isVowel = true;
break;
}
}
output += input.charAt(i);
if(count == 3) {
third_vowel_pos = i;
break;
}
}
count = 0;
String temp = "";
for(i = n - 1; i > third_vowel_pos; --i) {
isVowel = false;
for(j =0; j < vowels.length && count < 4;++j) {
if(input.charAt(i) == vowels[j]) {
isVowel = true;
count++;
break;
}
}
if(isVowel) {
temp += replacements[j];
}
else{
temp += input.charAt(i);
}
}
for(i = temp.length() - 1; i >= 0; --i)
output += temp.charAt(i);
System.out.println(output);
}
public static void main(String args[]) {
String input = "ac1;fheojkuvakioka";
String input2 = "aei";
String input3 = "aeiouia";
replaceChars(input);
replaceChars(input2);
replaceChars(input3);
}
}
static String replace(String str){
if(str.length()<4)return str;
String s=str.substring(3,str.length()-3);
for(int i=0;i<s.length();i++){
if(s.charAt(i)=='a'||s.charAt(i)=='e'||s.charAt(i)=='i'||s.charAt(i)=='o'||s.charAt(i)=='u'){
s=s.replace(s.charAt(i),(char) (s.charAt(i)-'a'+'A'));
}
}
return str.substring(0,3)+s+str.substring(str.length()-3);
}
<pre lang="" line="1" title="CodeMonkey71814" class="run-this">//replace a,e,i,o,u with A,E,I,O,U
- Anonymous August 29, 2011//at most four eligible letters from the rear of the string are replaced
//the first three eligible letters in the string are always exempted from replacement
#include<stdio.h>
int main()
{
int i, length=0;
int count=0;
// Bad, Idea. Just for here. Dont enter a string bigger than 100 chars
char a[100];
printf("Enter the string\n");
scanf("%s",a);
length = strlen(a);
for (i=0; i<length; i++)
{
if((a[i] == 'A') ||
(a[i] == 'E') ||
(a[i] == 'I') ||
(a[i] == 'O') ||
(a[i] == 'U') )
{
count++;
}
}
if(count <= 7)
count = count - 3;
else
count =4;
for (i=length; i>0; i--)
{
if(count>0)
{
if((a[i] == 'A') ||
(a[i] == 'E') ||
(a[i] == 'I') ||
(a[i] == 'O') ||
(a[i] == 'U') )
{
a[i] = a[i] + 'a' - 'A';
count--;
}
}
}
printf("Its %s",a);
}
</pre><pre title="CodeMonkey71814" input="yes">
</pre>