Hello friends, I hope you all are doing great. In this post today we will learn to solve the 2 coding questions which is being asked by the Accenture company recently. If you are preparing for selection in any company then this post will help you definitely.
Lets Start
1.Move Hyphen To Front
Implement the following functions
String MoveHyphen(String str,int n);
The function accepts a string “str” of length ‘n’, that contains alphabets and hyphens (-). Implement the function to move all hyphens(.) in the string to the front of the given string.
NOTE:- Return null if str is null.
Example :-
- Input:
- str.Move-Hyphens-to-Front
- Output:
- -MoveHyphenstoFront
Explanation:-
The string “Move-Hyphens -to-front” has 3 hyphens (.), which are moved to the front of the string, this output is “— MoveHyphen”
Sample Input
- Str: String-Compare
Sample Output-
- -StringCompare
/*Solution in JAVA*/ class MoveHyphenToFront { static String MoveHyphen(String str,int n) { if(str==null) return null; char[] ch=str.toCharArray(); String hyphen=""; for (int i = 0; i < ch.length; i++) { if(ch[i]=='-') hyphen+="-"; } str=str.replace("-",""); return hyphen+str; } public static void main(String[] args) { System.out.println(MoveHyphen("Move-Hyphens-to-Front", 18)); } } /* OUTPUT ---MoveHyphenstoFront */
2. Password Checker
Problem statement
You are given a function:
int CheckPassword(char str[ ], int n);
The function accepts string 'std of size int as argument Implement the function which returns 1 if given string 'str' is a valid password else 0.
'str' is a valid password if it satisfies below conditions:
- At least 4 characters
- At least one numeric digit
- At least one Capital letter
- Must not have space or slash (/)
- Starting character must not be a number
Assumption: Input string will not be empty.
Example:
Input:
aA1_67
Output:
1
class PasswordChecker { static int CheckPassword(String str, int n) { boolean isLen=false,isNum=false,isCap=false,isSpace=true,isFirst=true; if(str.length()>=4) isLen=true; if(Character.isDigit(str.charAt(0))) isFirst=false; char[] ch=str.toCharArray(); for (int i = 0; i < ch.length; i++) { if(Character.isDigit(ch[i])) isNum=true; if(ch[i]>=65 || ch[i]<=97) isCap=true; if(ch[i]==' ' || ch[i]=='/') isSpace=false; } if(isLen && isNum && isCap && isSpace && isFirst) return 1; else return 0; } public static void main(String[] args) { System.out.println(CheckPassword("aA1_67",6)); } } /* OUTPUT 1 */
Request:-If you found this post helpful then let me know by your comment and share it with your friend.
0 Comments