Adobe Interview Question
Developer Program EngineersCountry: United States
Interview Type: Written Test
Hello Rocky
I forgot the last case
Here's java code again
thx
public static void main(String[] args){
String a="12345a";
System.out.println(solve(a));
}
static boolean solve(String a){
char[] arr = a.toCharArray();
int tmp=0;
for( int i=0;i<arr.length;i++){
char c=arr[i];
tmp=(int)(c-'0');
if(i!=arr.length-1){
if(tmp>0 && tmp<10)
continue;
else
return false;
}{
if(c==' ' || (tmp>0 && tmp<10))
continue;
else
return false;
}
}
return true;
}
public static void main(String[] args){
String a="1234a56";
System.out.println(solve(a));
}
static boolean solve(String a){
char[] arr = a.toCharArray();
int tmp=0;
for(char c:arr){
tmp=(int)(c-'0');
if(tmp>0 && tmp<10)
continue;
else
return false;
}
return true;
}
youngsam I think your code fail on this test case when we have space at end
01234567" " but that we have to take valid
Test case
0123456789 valid
012345678a invalid
0123a56789 invalid
01234" " valid
012345678" " valid
01234567" " valid
" "12345 invalid
1234" "678 invalid
123" "4" "5678 invalid
#include <string.h>
#include <stdio.h>
bool solve(char* c, int len);
int main()
{
char* str = " 12345678";
printf("isnumber:%d\n", solve(str, strlen(str)));
return 1;
}
bool solve(char* c, int len)
{
for (int i=0; i<len; i++)
{
char tmp = c[i]-'0';
if(i!=(len-1))
{
if(tmp<0 || tmp>10)
{
return false;
}
}else if(tmp== ((int)(' ')-'0') || (tmp>=0 && tmp<10))
{
return true;
}
}
return false;
}
bool myFunction(char *a, int len)
{
bool valid = false;
for(int i=0; i<len-1; i++)
{
if(a[0] == ' ')
{
return false;
}
if(a[i] >= '0' && a[i]<= '9')
{
valid = true;
continue;
}
else
{
if(a[i] == ' ' && a[i+1]== '\0')
{
return true;
}
else
{
return false;
}
}
valid = true;
}
return valid;
}
Returns -1 if invalid and 0 if valid. All spaces is valid, but can be made invalid easily.
int ValidateNumber(string s)
{
size_t len = s.size();
int i = 0;
while(i < len)
{
if(s[i] == ' ')
break;
if(s[i] >= '0' && s[i] <= '9')
i++;
}
while(i<len)
if(s[i++] != ' ')
return -1;
return 0;
}
If you're going to use C++ then you might as well use bool. Also, it's easy to correct the all spaces problem. (Which would return a false value) Just track to see if you've ever seen a digit. If you've never seen one before spaces/other characters occured, it's false, otherwise, true - with trailing spaces.
bool ValidateNumber(string s)
{
size_t len = s.size();
int i = 0;
bool hasDigit = false;
while(i < len)
{
if(s[i] == ' ')
break;
if(s[i] >= '0' && s[i] <= '9') {
i++;
hasDigit = true;
}
}
while(i<len)
if(s[i++] != ' ')
return false;
return hasDigit;
}
public static void main(String[] args) {
System.out.println("Enter the string :\n");
Scanner in = new Scanner(System.in);
String inputString = in.nextLine();
int i = 0;
boolean flag = true;
while (i < inputString.length() - 1) {
if (inputString.charAt(i) == ' ') {
flag = false;
break;
} else if (inputString.charAt(i) >= '0'
&& inputString.charAt(i) <= '9') {
i++;
continue;
} else {
flag = false;
break;
}
}
if (i == inputString.length() - 1) {
if (inputString.charAt(i) == ' '
|| (inputString.charAt(i) >= '0' && inputString.charAt(i) <= '9')) {
flag = true;
} else
flag = false;
}
if (flag == true)
System.out.println("Valid string");
else
System.out.println("invalid string");
}
}
#include <stdio.h>
#include <string.h>
int match(char [], char []);
int main() {
char a[100], b[100];
int position;
printf("Enter some text\n");
gets(a);
printf("Enter a string to find\n");
gets(b);
position = match(a, b);
if(position != -1) {
printf("Found at location %d\n", position + 1);
}
else {
printf("Not found.\n");
}
return 0;
}
int match(char text[], char pattern[]) {
int c, d, e, text_length, pattern_length, position = -1;
text_length = strlen(text);
pattern_length = strlen(pattern);
if (pattern_length > text_length) {
return -1;
}
for (c = 0; c <= text_length - pattern_length; c++) {
position = e = c;
for (d = 0; d < pattern_length; d++) {
if (pattern[d] == text[e]) {
e++;
}
else {
break;
}
}
if (d == pattern_length) {
return position;
}
}
return -1;
}
Here's the JAVA code.:
public boolean isValidNumber(char[] str)
{
boolean result = true;
int begin = 0;
int end = str.length;
while(begin < end)
{
// If current element is a space then
// all following elements should be spaces.
if(str[begin]-' '==0)
{
for(int i=begin+1;i<end;i++)
{
if(str[i]-' '==0)
{
continue;
}
else
{
result = false;
break;
}
}
break;
}
//if current element is a character,
//then number is invalid
else if(str[begin]-'0'<=0 || str[begin]-'0' >=9)
{
result = false;
break;
}
//if current element is neither a space,
//nor a character then it is a number, carry on the checks further
else
begin++;
}
return result;
}
#include<iostream.h>
#include<conio.h>
int isValid(char *, int);
int main ()
{
clrscr();
char *arr[5] = {"92 345 678","92345678 "," 92345678","923a45678","92345678"};
int j,count;
for(int i=0; i<5; i++)
{ j=0;
count=0;
while(arr[i][j]!=NULL)
{count++;
j++;}
cout<<"size: "<<count<<endl;
cout <<"check: "<<isValid(arr[i],count)<<endl<<endl;
}
getch();
return 0;
}
int isValid(char *arr, int len)
{
if(arr[0]==' ')
{
cout<<"first is space"<<endl;
return 0;
}
for(int i=0; i<len ;i++)
{
if((arr[i]!=' ')&&((arr[i]<48)||(arr[i]>57)))
{
cout<<"Not a number"<<endl;
return 1;
}
else if((arr[i]==' ')&& (arr[i+1]!=' ') && (arr[i+1]!=NULL))
{
cout<<"space in between "<<endl;
return 2;
}
}
cout<<"this is awesome!2!!!"<<endl;
return 10;
}
apologise examples
- rocky September 11, 20140123456789 valid
012345678a invalid
0123a56789 invalid
01234" " valid
012345678" " valid
01234567" " valid
" "12345 invalid
1234" "678 invalid
123" "4" "5678 invalid
in b/w inverted commas we have spaces and each array is of length 10