Epic Systems Interview Question
Software Engineer / Developerspublic static String strConverter(String str)
{
StringBuilder newStr = new StringBuilder();
int shift = 2; // shift value
for(int i=0;i< str.length();i++)
{
char ch = str.charAt(i);
// perform Caesar shift
if((ch >= 'a') && (ch <= 'z'))
ch = (char)('a'+(ch-'a'+shift)%26);
else if((ch >= 'A') && (ch <= 'Z'))
ch = (char)('A'+(ch-'A'+shift)%26);
// build the string with the shifted char
newStr.append(ch);
}
return newStr.toString();
}
string GreetingText="abcD";
//GreetingText+="We do hope you enjoy this book as much as we enjoyed writing it.";
for(int i =(int)'z';i>=(int)'a';i--)
{
char old1=(char)i;
char New1=(char)(i+2);
GreetingText=GreetingText.Replace(old1,New1 );
}
for(int i= (int)'Z';i>=(int)'A';i--)
{
char old1=(char)i;
char new1=(char)(i+2);
GreetingText=GreetingText.Replace(old1,new1);
}
Console.WriteLine("Encoded:\n"+GreetingText) ;
Console.Read();
public String strConverter(String str)
{
StringBuilder newStr = new StringBuilder();
int shift = 2; // shift value
for(int i=0;i< str.length();i++)
{
char ch = str.charAt(i);
// perform Caesar shift
ch = (char)(‘a’+(ch-‘a’+shift)%26);
// build the string with the shifted char
newStr.append(ch);
}
return newStr.toString();
}
I think we need to even take care of the cycling at the end. i.e if the letter is Y, then Y+2 = A , y+2 = a , Z+2 = B , z+2 = b.
This code works in C++
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
char shift_string[10]="rlzY";
for(int i=0;shift_string[i]!='\0';i++)
{
if(shift_string[i]=='Y')
shift_string[i]='A';
else if(shift_string[i]=='y')
shift_string[i]='a';
else if(shift_string[i]=='Z')
shift_string[i]='B';
else if(shift_string[i]=='z')
shift_string[i]='b';
else
shift_string[i]=(char)shift_string[i]+2;
}
cout<<"\n Shifted string is " <<shift_string;
cout<<"\n";
return(0);
}
public static String strConverter(String str)
{
StringBuilder newStr = new StringBuilder();
int shift = 2; // shift value
for(int i=0;i< str.length();i++)
{
char ch = str.charAt(i);
// perform Caesar shift
if((ch >= 'a') && (ch <= 'z'))
ch = (char)('a'+(ch-'a'+shift)%26);
else if((ch >= 'A') && (ch <= 'Z'))
ch = (char)('A'+(ch-'A'+shift)%26);
// build the string with the shifted char
newStr.append(ch);
}
return newStr.toString();
}
void convertString(string inputString, int shiftpos)
{
int start = 0;
int end = 0;
char referencechar;
string newstr = "";
int len = inputString.length();
for(int i =0;i<len;i++)
{
start = i;
end = start+1;
char ch = (char)inputString[i];
if(ch>='a'&&ch<='z')
{
referencechar = 'a';
}
else if(ch>='A'&&ch<='Z')
{
referencechar = 'A';
}
ch = (char)(referencechar+(ch-referencechar+shiftpos)%26);
cout<<"[newstring:"<<ch<<"]\n";
}
}
void convertString(string inputString, int shiftpos)
{
int start = 0;
int end = 0;
char referencechar;
string newstr = "";
int len = inputString.length();
for(int i =0;i<len;i++)
{
start = i;
end = start+1;
char ch = (char)inputString[i];
if(ch>='a'&&ch<='z')
{
referencechar = 'a';
}
else if(ch>='A'&&ch<='Z')
{
referencechar = 'A';
}
ch = (char)(referencechar+(ch-referencechar+shiftpos)%26);
cout<<"[newstring:"<<ch<<"]\n";
}
}
string convert(string inp)
{
int i;
for(i=0;i<inp.size();i++)
{
if(inp[i]>='a' && inp[i]<='z')
inp[i]='a'+(inp[i]-'a'+2)%26;
else
inp[i]='A'+(inp[i]-'A'+2)%26;
}
return inp;
}
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char c[10];
int b[10];
int i;
printf("Enter the String:");scanf("%s",&c);
int l=strlen(c);
printf("String Length: %d\n", l);
printf("Input String:");
for(i=0;i<l;i++)
{
printf("%c",c[i]);
}
printf("\n Shifted String :");
for(i=0;i<l;i++)
{
b[i]=c[i];
}
for(i=0;i<l;i++)
{
b[i]=b[i]+2;
c[i]=b[i];
printf("%c",c[i]);
}
// printf("%d\n",b[i]);
getch();
}
#include<stdio.h>
main()
{
char p;
printf("Enter a string\t");
scanf("%s",&p);;
convertString(&p);
}
convertString(char *s)
{
int j = 0,i;
while(s[j] != '\0')
{
if(s[j] == 'y')
{
s[j] = 'a';
j++;
}
else if(s[j] == 'z')
{
s[j] = 'b';
j++;
}
else if(s[j] == 'Y')
{
s[j] = 'A';
j++;
}
else if(s[j] == 'Z')
{
s[j] = 'B';
j++;
}
else
{
i = s[j];
i = i + 2;
//printf("%d ---->>>>> %c\n",i,i);
s[j] = i;
j++;
}
}
printf("%s\n",s);
}
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
string input = Console.ReadLine();
ConvertString(input);
Console.ReadLine();
}
static void ConvertString(string stringinput)
{
int shift = 2;
string temp = "";
for (int i = 0; i < stringinput.Length; i++)
{
temp += Convert.ToChar((stringinput[i] + shift-'a')%26+'a');
}
Console.WriteLine(temp);
}
}
}
package b;
public class CovertStringToAnother {
public static void main(String[] args){
String in = "plxy";
String op = "";
for(int i = 0; i < in.length(); i++){
char c = in.charAt(i);
if(c == 'y' || c=='z' || c=='Y' || c=='Z'){
switch(c){
case 'y' : op += 'a';
break;
case 'z' : op += 'b';
break;
case 'Y' : op += 'a';
break;
case 'Z' : op += 'b';
break;
}
}else{
c += 2;
op += c;
}
}
System.out.println(op);
}
}
package b;
public class CovertStringToAnother {
public static void main(String[] args){
String in = "plxy";
String op = "";
for(int i = 0; i < in.length(); i++){
char c = in.charAt(i);
if(c == 'y' || c=='z' || c=='Y' || c=='Z'){
switch(c){
case 'y' : op += 'a';
break;
case 'z' : op += 'b';
break;
case 'Y' : op += 'a';
break;
case 'Z' : op += 'b';
break;
}
}else{
c += 2;
op += c;
}
}
System.out.println(op);
}
}
void convertString(char *input)
{
int i=0;
while(input[i]!='\0')
{
if(input[i]<=122 && input[i]>=92)
{
input[i]=input[i]+2>122 ? input[i]-24: input[i]+2;
}
else if(input[i]<=90 && input[i]>=65)
{
input[i]=input[i]+2>90 ? input[i]-24: input[i]+2;
}
else
{
printf("Please enter the letter between 'A'-'Z' or 'a'-'z'\n");
}
i++;
}
}
#include<stdio.h>
#include<string.h>
int main()
{
char str[]="loLaAyY";
int len=strlen(str);
auto int i;
for(i;i<len;i++)
{
if(str[i]>='a' && str[i]<='z')
{
if(str[i]+2>='z')
str[i]=((str[i]+2)% 'z')+'a'-1;
else
str[i]=(str[i]+2);
}
else if(str[i]>='A' && str[i]<='Z')
{
if(str[i]+2>='Z')
str[i]=((str[i]+2)% 'Z')+'A'-1;
else
str[i]=(str[i]+2);
}
}
printf("%s\n",str);
return 0;
}
#include<stdio.h>
#include<string.h>
int main()
{
char str[]="loLaAyY";
int len=strlen(str);
auto int i;
for(i;i<len;i++)
{
if(str[i]>='a' && str[i]<='z')
{
if(str[i]+2>='z')
str[i]=((str[i]+2)% 'z')+'a'-1;
else
str[i]=(str[i]+2);
}
else if(str[i]>='A' && str[i]<='Z')
{
if(str[i]+2>='Z')
str[i]=((str[i]+2)% 'Z')+'A'-1;
else
str[i]=(str[i]+2);
}
}
printf("%s\n",str);
return 0;
}
#include<stdio.h>
#include<string.h>
int main()
{
char str[]="loLaAyY";
int len=strlen(str);
auto int i;
for(i;i<len;i++)
{
if(str[i]>='a' && str[i]<='z')
{
if(str[i]+2>='z')
str[i]=((str[i]+2)% 'z')+'a'-1;
else
str[i]=(str[i]+2);
}
else if(str[i]>='A' && str[i]<='Z')
{
if(str[i]+2>='Z')
str[i]=((str[i]+2)% 'Z')+'A'-1;
else
str[i]=(str[i]+2);
}
}
printf("%s\n",str);
return 0;
}
#include<stdio.h>
#include<string.h>
int main()
{
char str[]="loLaAyY";
int len=strlen(str);
auto int i;
for(i;i<len;i++)
{
if(str[i]>='a' && str[i]<='z')
{
if(str[i]+2>='z')
str[i]=((str[i]+2)% 'z')+'a'-1;
else
str[i]=(str[i]+2);
}
else if(str[i]>='A' && str[i]<='Z')
{
if(str[i]+2>='Z')
str[i]=((str[i]+2)% 'Z')+'A'-1;
else
str[i]=(str[i]+2);
}
}
printf("%s\n",str);
return 0;
}
This is the exact working code
#include<stdio.h>
main()
{ int i,j=2;
char str[30],test[30];
char t;
printf("Enter the alphabetical code \n");
scanf("%s",str);
i=0;
while(str[i]!='\0')
{ str[i]=str[i]+2;
if(str[i]>90 && str[i]<97)
str[i]=((str[i]-'A')%26) + 'A';
if(str[i]>122)
str[i]=((str[i]-'a')%26) + 'a';
i++;
}
printf("The output string is %s\n",str);
}
class Program
{
static void Main(string[] args)
{
String result = strConvert("abcD");
Console.WriteLine(result);
Console.Read();
}
public static String strConvert(String str)
{
StringBuilder newStr = new StringBuilder();
int shift = 2; //shift value
for (int i = 0; i < str.Length; i++)
{
char ch = str[i];
if ((ch >= 'a') && (ch <= 'z'))
{
ch = (char)('a' + (ch - 'a' + shift) % 26);
}
else if ((ch >= 'A') && (ch <= 'Z'))
{
ch = (char)('A' + (ch - 'A' + shift) % 26);
}
newStr.Append(ch);
}
return newStr.ToString();
}
}
import java.util.Scanner;
public class convertString {
public static void convert(String str) {
char[] ch = str.toCharArray();
for (int i = 0; i < ch.length; i++) {
if (ch[i] >= 'a' && ch[i] <= 'z')
ch[i] = (char) ('a' + (ch[i] + 2 - 'a') % 26);
else if (ch[i] >= 'A' && ch[i] <= 'Z')
ch[i] = (char) ('A' + (ch[i] + 2 - 'A') % 26);
}
for (char c : ch)
System.out.print(c);
}
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter String");
String s = sc.next();
convert(s);
}
}
public class Solve {
public static void main(String[] args) throws Exception {
Deal_With_It("plxY");
}
public static String Deal_With_It(String str){
int number[]=new int[str.length()];
if(str.length()==0)
return "";
if(str.length()==1)
return str.toUpperCase();
for (int i = 0; i < str.length(); i++) {
number[i]=(int)str.charAt(i);
}
if(number[number.length-1]>=89)
number[number.length-1]=64+(90-number[number.length-1]);
else
number[number.length-1]+=2;
String result="";
for (int i = 0; i < number.length-1; i++) {
result+=Character.toString((char)(number[i]+2));
}
result+=Character.toString((char)number[number.length-1]);
System.err.println(result);
return "";
}
}
Here is a working code in C++.
#include <cstdio>
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char str[100];
int len, i, temp;
cin>>str;
len=strlen(str);
for (i=0; i<len; i++)
{
str[i]+=2;
if (str[i]>=97 && str[i]>122)
{
temp=(int)str[i];
temp-=122;
temp+=96;
str[i]=(char)temp;
}
else if(str[i]>90 && str[i]<97)
{
temp=(int)str[i];
temp-=90;
temp+=64;
str[i]=(char)temp;
}
}
for (i=0; i<len; i++)
{
cout<<str[i];
}
return 0;
}
public static String convertString(String inp, int shift) {
StringBuilder sb = new StringBuilder();
for (int i=0; i<inp.length(); i++) {
if (inp.charAt(i) <= 'Z' && inp.charAt(i) >= 'A') {
int x = (int) inp.charAt(i);
char ch = (char) ((((x%64)+shift)%26)+64);
sb.append(ch);
} else if (inp.charAt(i) <= 'z' && inp.charAt(i) >= 'a') {
int x = (int) inp.charAt(i);
char ch = (char) ((((x%96)+shift)%26)+96);
sb.append(ch);
}
}
return sb.toString();
}
public String shift(String word)
{
StringBuilder result = new StringBuilder();
for(int i=0;i<word.length();i++)
{
char start = '\0';
char cur = word.charAt(i);
if(cur>='a' && cur<='z')
start = 'a';
else if(cur>='A' && cur<='Z')
start = 'A';
char change = (char) (((char) ((cur-start)+2)%26) + start);
result.append(change);
}
return result.toString();
}
- coder February 05, 2009