Java Basic Code Practice
String
-----------------------------------------------------------------------------------------------------------------------------
1) I/p - Sudhanshu-Kumar-Shekhar -> O/p - SKS
I/p - Ram-Shyam-Mohan -> O/p - RSM
Code : -
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.next();
String currStr ="";
String result ="";
for(int i=0; i<s.length(); i++){
if(s.charAt(i)=='-' && currStr!=""){
result+= currStr.charAt(0);
currStr="";
}
else{
currStr+=s.charAt(i);
}
}
if(currStr!=""){
result+=currStr.charAt(0);
System.out.println(result);
}
}
}
output :-
Shu-Kr-RA
SKR
-----------------------------------------------------------------------------------------------------------------------------
2) These Students marks Answer these Pattern
Sudhanshu- A,B,C,A,B,C, - - - - - - - - - -
Subodh - B,A,B,C,B,A,B,C, - - - - - - - - -
Ajay - C,C,A,A,B,B,C,C,A,A,B,B, - - - - - -
Input:-
1) Contain an Integer N(1<=N<=100) -> No. of Question in Exam,
2) Answer of Question in String forms .
Output:-
1) Integer (M) -> Largest No. of correct answers in the three boys gets.
2) Name of that boys whose largest no. of correct answer.
Code:-
import java.util.*;
public class Main
{
public static void main(String[] args) {
String Sudhanshu = "ABC";
String Subodh = "BABC";
String Ajay = "CCAABB";
int SudhanshuScore=0;
int SubodhScore = 0;
int AjayScore = 0;
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
while(Sudhanshu.length()<n){
Sudhanshu+="ABC";
}
while(Subodh.length()<n){
Subodh+="BABC";
}
while(Ajay.length()<n){
Ajay+="CCAABB";
}
String ans = sc.next();
for(int i=0;i<n;i++){
if(ans.charAt(i)==Sudhanshu.charAt(i))
SudhanshuScore++;
if(ans.charAt(i)==Subodh.charAt(i))
SubodhScore++;
if(ans.charAt(i)==Ajay.charAt(i))
AjayScore++;
}
int maxScore = Math.max(SudhanshuScore, Math.max(SubodhScore,AjayScore));
System.out.println(maxScore);
if(maxScore==SudhanshuScore)
System.out.println("Sudhanshu");
if(maxScore==SubodhScore)
System.out.println("Subodh");
if(maxScore==AjayScore)
System.out.println("Ajay");
}
}
OUTPUT:-
5
BAACC
3
Subodh
-----------------------------------------------------------------------------------------------------------------------
3) Time Conversion :-
12-hr AM/PM - 24-hr military time String format
Note:- 12:00:00AM --> 00:00:00
12:00:00PM --> 12:00:00
Code:-
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.next();
if(s.charAt(8)=='P'){
int hrValue = Integer.parseInt(s.substring(0,2));
hrValue%=12;
hrValue+=12;
String hrValueString = String.valueOf(hrValue);
System.out.println(hrValueString + s.substring(2,8));
}
else{
int hrValue = Integer.parseInt(s.substring(0,2));
hrValue%=12;
String hrValueString;
if(hrValue<10){
hrValueString = "0"+String.valueOf(hrValue);
}
else{
hrValueString=String.valueOf(hrValue);
System.out.println(hrValue+s.substring(2,8));
}
}
}
}
OUTPUT:- 03:00:00PM
15:00:00
-----------------------------------------------------------------------------------------------------------------------------
4) TYPE OF TRIANGLE - ACUTE , RIGHT OR OBTUSE ANGLE TRIANGLE
I/P - THREE SIDE OF TRIANGLE (a,b,c)
Code:-
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
checkTriangle(a,b,c);
}
public static void checkTriangle(int a, int b, int c){
int sqa = (int)Math.pow(a,2);
int sqb = (int)Math.pow(b,2);
int sqc = (int)Math.pow(c,2);
if(sqa==sqb+sqc||sqb==sqc+sqa||sqc==sqa+sqb){
System.out.println("Right Angle Triangle");
}
else if(sqa>sqb+sqc||sqb>sqc+sqa||sqc>sqa+sqb){
System.out.println("Obtuse Angle Triangle");
}
else if(sqa<sqb+sqc||sqb<sqc+sqa||sqc<sqa+sqb){
System.out.println("Acute Angle Triangle");
}
}
}
OUTPUT:-
3 4 5
Right Angle Triangle
-----------------------------------------------------------------------------------------------------------------------------
5) Array Rotation
I/p-
3 2 3
array Size(n) No. of rotation(k) size of quriey to print(q)
1 2 3 -> array
0 1 2 - > array quirey
O/P -
2
3
1
code:-
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
int q = sc.nextInt();
int arr[] = new int[n];
int a = k%n;
for(int i=0;i<n;i++){
arr[i] = sc.nextInt();
}
for(int i=0;i<q;i++){
int s = sc.nextInt();
if(s-k>=0){
System.out.println(arr[s-k]);
}
else{
System.out.println(arr[s-k+arr.length]);
}
}
}
}
Output:-
3 2 3
1 2 3
0 1 2
2
3
1
----------------------------------------------------------------------------------------------------------------------------
6) Remove Zero in String with some conditions
-- Given a string of 0 or 1. so u remove min. 0 in between 1 and 1 so that string in the form 1111
ex- 3 -> No. of input
i/p o/p
010011 -> 2
111000 -> 0
10100 -> 1
Code:-
import java.util.Scanner;
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String s;
for(int i=0;i<n;i++){
s = sc.next();
int firstIndex =-1;
for(int j =0;j<s.length();j++){
if(s.charAt(j)=='1'){
firstIndex=j;
break;
}
}
int lastIndex =-1;
for(int j=s.length()-1; j>=0;j--){
if(s.charAt(j)=='1'){
lastIndex =j;
break;
}
}
int result= 0;
if(firstIndex==-1){
System.out.println(result);
}
else{
for(int j = firstIndex;j<=lastIndex;j++){
if(s.charAt(j)=='0'){
result++;
}
}
System.out.println(result);
}
}
}
}
Output:-
3
010011
2
000
0
11110000
0
----------------------------------------------------------------------------------------------------------------------------
7) In a string count integer
eg)
1sd23 -> o/p 1 + 23 = 24
s2du23s1 -> o/p 2 + 23 + 1 = 26
Code: -
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.next();
String num = "";
int sum = 0;
for(int i=0;i<str.length();i++){
if(Character.isDigit(str.charAt(i))){
num = num+ str.charAt(i);
}
else{
if(num!=""){
sum+=Integer.parseInt(num);
num="";
}
}
}
sum+=Integer.parseInt(num);
System.out.println(sum);
}
}
Output : -
s2du23s1
26
---------------------------------------------------------------------------------------------------------------------------
8) A and B called anagrams if they contain all the same frequency . For this challenge the taste case is not case - sensitive.
eg) String a g n m r
anagrams 3 1 1 2 1
marganaa 4 1 1 1 1
- Output- Not anagrams
String h e l o
hello 1 1 2 1
olleh 1 1 2 1
- Output- anagrams
Code: -
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String a = sc. next();
String b = sc.next();
sc.close();
boolean ret = isAnagram(a,b);
System.out.println((ret)? "Anagrams": " Not Anagrams");
}
public static boolean isAnagram(String a, String b){
if(a.length()!=b.length()){
return false;
}
a = a.toLowerCase();
b=b.toLowerCase();
int sum = 0;
for(char c='a';c<='z';c++){
for(int i=0;i<a.length();i++){
if(a.charAt(i)==c){
sum++;
}
if(b.charAt(i)==c){
sum--;
}
}
if(sum!=0){
return false;
}
}
return true;
}
}
Output: -
hellow
whello
Anagrams
-----------------------------------------------------------------------------------------------------------------------------
9) GCD of two given Number
code:
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int ans = GCD(a,b);
System.out.println(ans);
}
public static int GCD(int a, int b){
if(a==0) return b;
if(b==0) return a;
if(a==b) return a;
if(a>b) return GCD(a-b,b);
return GCD(a,b-a);
}
}
Output:-
5
10
5
-----------------------------------------------------------------------------------------------------------------------------
10)
Comments
Post a Comment