Adobe Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: Phone Interview
Yes.. almost that's (ASCII) what I did, but i did not convert the string to lowercase... Below is a working solution:
class Palindrome
{
static void Main(string[] args)
{
string[] strs = new string[]{"Madam, I'm Adam!", "Bob", "", " ", "fuluf", "full"};
foreach (string str in strs)
{
try
{
if (IsPalindrome(str))
{
System.Console.WriteLine(str + " IS a palindrome");
}
else
{
System.Console.WriteLine(str + " is NOT a palindrome");
}
}
catch (Exception e)
{
System.Console.WriteLine("Exception caught: " + e);
}
}
}
public string splcharfreestring(string str)
{
char[] ca = str.ToCharArray();
StringBuilder sb = new StringBuilder();
int i=0;
for (i=0; i< str.Length; i++)
{
if((ca[i] >= 'A' && ca[i] <= 'Z') || (ca[i] >='a' && ca[i] <= 'z'))
{
sb.Append(ca[i]);
}
else
continue;
}
return sb.ToString();
}
static bool IsPalindrome(string str)
{
Palindrome sl = new Palindrome();
string elimatesplchars;
elimatesplchars = sl.splcharfreestring(str);
bool ispal = false;
char[] ca = elimatesplchars.ToCharArray();
int i = 0, j = elimatesplchars.Length - 1;
if (str == null)
{
throw new Exception("str is null");
}
if (str.Length == 1)
return true;
else if (str == "")
return false;
else
{
for (i = 0; i < ca.Length / 2; i++)
{
char ch = ca[j];
if ((ca[i] == ca[j]) || (ca[i] == (char) (ch + 32) || (ca[i] == (char) (ch - 32)) ))
{
ispal = true;
j--;
continue;
}
else
{
ispal = false;
return ispal;
}
}
}
if (ispal == true)
return ispal;
return ispal;
}
}
int check_paldm(char *str){
char *p,*ptr;
ptr=str;
int len;
len=strlen(str);
p=ptr+len-1;
while(*ptr!='\0'){
if(*p>=97)
*p=*p-32;
if(*ptr>=97)
*ptr=*ptr-32;
if(*ptr>91 || *ptr<65){
ptr++;
continue;
}
if(*p>91 || *p<65){
p--;
continue;
}
if(*p==*ptr){
printf("p = %c and ptr = %c\n",*p,*ptr);
p--;
ptr++;
continue;
}else{
return FALSE;
}
}
return TRUE;
}
use strict;
use warnings;
print "a = ";
my $a = lc <STDIN>;
chomp $a;
my @a_arr = split //, $a;
@a_arr = grep { /[\w]/ } @a_arr;
my $bool = 1;
while(@a_arr)
{
my $f = $a_arr[0];
my $l = pop @a_arr;
if($f ne $l)
{
print "false\n";
$bool = 0;
last;
}
shift @a_arr;
}
print "true\n" if($bool);
Check this out..
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *trim(char *);
int main()
{
char str[80],*beg,*end,*str1;
int i=0,flag=0,x;
printf("Enter the string:");
gets(str);
//preprocess string remove any special char.
str1=trim(str);
strcpy(str,str1);
beg=str;
end=str+strlen(str)-1;
x=strlen(str)/2;
while(i!=x&&x>0)
{
if((*beg++)!=(*end--))
{
flag=1;
printf("\nNot palindrome");
break;
}
i++;
}
if(flag==0)
printf("\nPalindrome");
return 0;
}
char *trim(char *str)
{
char temp[strlen(str)],*str1;
int count=0,k=0,j=0;
while(*str!='\0')
{
if(((*str>='a'&&*str<='z')||(*str>='A'&&*str<='Z')))
{
temp[count++]=*str;
}
str++;
}
str1=malloc(strlen(temp)*sizeof(char*));
strcpy(str1,temp);
return str1;
}
#include <iostream>
#include <algorithm>
using namespace std;
bool palin(string str)
{
transform(str.begin(),str.end(),str.begin(),::tolower); //Convert to lower case
int len = str.length();
int mid = (len)/2;
for(int i=0;i<=len-1;i++)
{
if(str[i]<97 || str[i]>122 || str[len-i-1]<97 || str[len-i-1]>122)
{
continue;
}
if(str[i]!=str[len-i-1])
{
return false;
}
}
return true;
}
int main()
{
string str = "Madam i'm Adam";
if(palin(str))
{
cout << "Palindrome" << endl;
}
else
{
cout << "NOT palindrome" << endl;
}
}
Took out the "mid" from my previous post.
#include <iostream>
#include <algorithm>
using namespace std;
bool palin(string str)
{
transform(str.begin(),str.end(),str.begin(),::tolower); //Convert to lower case
int len = str.length();
for(int i=0;i<=len-1;i++)
{
if(str[i]<97 || str[i]>122 || str[len-i-1]<97 || str[len-i-1]>122)
{
continue;
}
if(str[i]!=str[len-i-1])
{
return false;
}
}
return true;
}
int main()
{
string str = "Madam i'm Adam";
if(palin(str))
{
cout << "Palindrome" << endl;
}
else
{
cout << "NOT palindrome" << endl;
}
}
public class abc {
public boolean palindrom(String str)
{
str=str.toLowerCase();
str=removeSpecialChar(str);
return new StringBuffer(str).reverse().toString().equals(str)?true:false;
}
public String removeSpecialChar(String str){
String s="";
for(int i=0;i<str.length();i++)
{
if(str.charAt(i)>='a' && str.charAt(i)<='z')
{
s=s+str.charAt(i);
}
}
return s.trim();
}
public static void main(String[] args) {
System.out.println(new abc().palindrom("Madam, I'm Adam!!"));
}
}
#include<stdio.h>
#include<string.h>
#include <ctype.h>
bool checkPalindrome(char *seq){
int n = strlen(seq);
int left=0,right=n-1;
while(left<right){
while(((seq[left]<'A' || seq[left]>'Z') && !(seq[left]>'a' && seq[left]<'z'))==true)
left++;
while(((seq[left]<'a' || seq[left]>'z') && !(seq[left]>'A' && seq[left]<'Z')) ==true)
left++;
while(((seq[right]<'A' || seq[right]>'Z') && !(seq[right]>'a' && seq[right]<'z'))==true)
right--;
while(((seq[right]<'a' || seq[right]>'z') && !(seq[right]>'A' && seq[right]<'Z')) ==true)
right--;
if(tolower(seq[left])==tolower(seq[right]))
{
left++;
right--;
}
else
return false;
}
return true;
}
int main(){
char str[]="Madam, I'm Adam!";
if(checkPalindrome(str))
printf("Palindrome");
else
printf("Not a Palindrome");
getchar();
return 0;
}
public bool IsPalindrom(string input)
{
if (string.IsNullOrWhiteSpace(input))
{
return false;
}
int startIndex = 0, endIndex = input.Length - 1;
while (startIndex <= endIndex)
{
while (!IsAnAlphabet(input[startIndex]) && startIndex < endIndex)
{
startIndex++;
}
while (!IsAnAlphabet(input[endIndex]) && startIndex < endIndex)
{
endIndex--;
}
int diff = input[startIndex] - input[endIndex];
if (!(diff == 0 || diff == 32 || diff == -32))
{
return false;
}
startIndex++;
endIndex--;
}
return true;
}
public bool IsAnAlphabet(char c)
{
return (65 <= c && c <= 90 || (97 <= c && c <= 122));
}
Only case won`t be handled is when string is all special case say "!!@#!@$@#$@#$", if will true rather than false.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
main(){
char a[100];
int length,i,j,index=0;
char *p;
gets(a);
length=strlen(a);
p=(char *)malloc(sizeof(char));
for(i=0;i <length; i++){
if( isalpha(a[i])|| isdigit(a[i])){
p[index]=isupper(a[i])? tolower(a[i]): a[i];
index++;
}
}
for(i=0; i <index ;i++) printf("%c",p[i]);
// printf("\nindex=%d \n",index);
for(i =0, j=index-1; i <= j; i++,j--){
if( p[i]== p[j]) continue;
else {
printf("\nstring %s is not palindrom\n",a);
break;
}
}
if(i-1==j+1) printf("\nstring %s is palindrome\n",a);
scanf("%d",&i);
}
public class Palindrome {
private boolean isPalindrom(String s) {
if(s.trim().length()==0){
return false;
}
int i = 0;
int j = s.length() - 1;
while (i < j) {
if (s.toLowerCase().charAt(i) == s.toLowerCase().charAt(j)) {
i++;
j--;
while (s.toLowerCase().charAt(i) < 'a'
|| s.toLowerCase().charAt(i) > 'z') {
i++;
}
while (s.toLowerCase().charAt(j) < 'a'
|| s.toLowerCase().charAt(j) > 'z') {
j--;
}
} else {
return false;
}
}
return true;
}
public class Palindrome {
private boolean isPalindrom(String s) {
if(s.trim().length()==0){
return false;
}
int i = 0;
int j = s.length() - 1;
while (i < j) {
if (s.toLowerCase().charAt(i) == s.toLowerCase().charAt(j)) {
i++;
j--;
while (s.toLowerCase().charAt(i) < 'a'
|| s.toLowerCase().charAt(i) > 'z') {
i++;
}
while (s.toLowerCase().charAt(j) < 'a'
|| s.toLowerCase().charAt(j) > 'z') {
j--;
}
} else {
return false;
}
}
return true;
}
bool check = true;
bool isPalindrome(string str, int start, int end)
{
if(start<end)
{
while(start<=end && !((str[start] >= 'a' && str[start] <= 'z') || (str[start] >= 'A' && str[start] <= 'Z')))
start++;
while(end >=0 && !((str[end] >= 'a' && str[end] <= 'z') || (str[end] >= 'A' && str[end] <= 'Z')))
end--;
if(toupper(str[start]) != toupper(str[end]))
{
check = false;
return false;
}
return check && isPalindrome(str, start+1, end-1);
}
return true;
}
#include <stdio.h>
#include <string.h>
//method should ignore special characters and white spaces
//method should consider lower case and upper case characters to be the same
int check_palindrome(const char *user_str)
{
int i, j;
const int len = strlen(user_str);
char check_str[len];
for(i=len-1, j=0; i>=0; i--, j++)
{
check_str[j] = user_str[i];
}
check_str[j] = '\0';
if( strcmp(user_str, check_str) == 0 )
return 1;
else
return 0;
}
static void prepare_string_for_process2(char *str) //Without using a temporary buffer
{
int i, j;
for(i=0, j = 0; i<strlen(str); i++)
{
if( isalpha(str[i]) )
{
if( isupper(str[i]) )
{
str[j++] = tolower(str[i]);
}
else
str[j++] = str[i];
}
else
{
if( !isdigit(str[i]) )
continue;
else
str[j++] = str[i];
}
}
str[j] = '\0';
printf("\n Modified string is (without temp buffer): %s", str);
}
static void prepare_string_for_process1(char *str) //using a temporary buffer
{
int i, j;
const int len = strlen(str);
char processed_str[len];
for(i=0, j=0; i<len; i++)
{
if( isalpha(str[i]) )
{
if( isupper(str[i]) )
processed_str[j++] = tolower(str[i]);
else
processed_str[j++] = str[i];
}
else if( isdigit(str[i]) )
{
processed_str[j++] = str[i];
}
}
processed_str[j] = '\0';
strcpy(str, processed_str);
printf("\n Modified string is (with temp buffer): %s", processed_str);
}
int main()
{
char str[50];
int n;
printf("\n Please enter a string (Please ensure that the string doesn't exceed 49 characters):");
fgets(str, sizeof(str), stdin);
n = strlen(str)-1;
if( str[n] == '\n' )
str[n] = '\0';
//prepare_string_for_process1(str); //With temporary buffer
prepare_string_for_process2(str); //Without temporary buffer
if( check_palindrome(str) == 1 )
printf("\n The string \'%s\' is a palindrome \n", str);
return 0;
}
Do the same functionality as palindrome function does with below modifications:
- arsragavan March 19, 2014a) you can convert the string to all lowercase
b) consider only ASCII characters within range 97-122 (a-z)