To Find the Number of Lowercase letters
import java.util.*;
class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
String S = sc.nextLine();
int count =0;
char[] arr = S.toCharArray();
for(int i=0;i<arr.length;i++){
if(Character.isLowerCase(arr[i])){
count++;
}
}
System.out.println(count);
}
}
Sample Input:
WelcomeToJava
Sample Output:
10
1
To Find the Character of a String at Odd Index
import java.util.*;
class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
String str1 = "";
String str = sc.nextLine();
char[] arr = str.toCharArray();
for(int i=0;i<arr.length;i++){
if(i%2==1){
str1 = str1+arr[i];
}
}
System.out.println(str1);
}
}
Sample Input:
HelloWorld
Sample Output:
elWrd
2
To Find out the number of Words in a sentence
import java.util.*;
class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
String[] arr = str.split(" ");
int count =0;
for(int i=0;i<arr.length;i++){
if(arr[i]!=" "){
count++;
}
}
System.out.println(count);
}
}
Sample Input:
Welcome to My World of Programming
Sample Output:
6
3
To find the number of Even digits in a given Number
import java.util.*;
class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int m = sc.nextInt();
int count =0;
while(m!=0){
int n=m%10;
if(n%2==0){
count++;
}
m=m/10;
}
System.out.println(count);
}
}
Sample Input:
14236
Sample Output:
3
4
To Reverse the words in a String
import java.util.*;
class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
String str2 = "";
String[] str1 = str.split(" ");
for(int i=0;i<str1.length;i++){
str2 = str1[i] +" "+ str2;
}
System.out.println(str2);
}
}
Sample Input:
Welcome to my Java Programming
Sample Output:
Programming Java my to Welcome
5
To find the first letter of each Word in a String
import java.util.*;
class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
String str1 = "";
str1 = str1 + str.charAt(0);
for(int i=1;i<str.length();i++){
if(str.charAt(i)==' '){
str1 = str1 + str.charAt(i+1);
}
}
System.out.println(str1);
}
}
Sample Input:
Hello How are You
Sample Output:
HHaY
6
To remove the Character in a given String
import java.util.*;
class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
String ch = sc.nextLine();
String s = str.replace(ch,"");
System.out.println(s);
}
}
Sample Input:
Hello
l
Sample Output:
Heo
7
To reverse a String
import java.util.*;
class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
String s = "";
for(int i=0;i<str.length();i++){
s = str.charAt(i) + s;
}
System.out.println(s);
}
}
Sample Input:
Hello How are you
Sample Output:
uoy era woH olleH
8
To find the last Letter of each word in a given String
import java.util.*;
class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
String s = "";
for(int i=0;i<str.length();i++){
if(str.charAt(i)==' '){
s = s + str.charAt(i-1);
}
}
System.out.print(s);
System.out.print(str.charAt(str.length()-1));
}
}
Sample Input:
Hello How are you
Sample Output:
oweu
9
To find the Average of the Following Numbers according to the Limit
import java.util.*;
class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for(int i=0;i<n;i++){
arr[i] = sc.nextInt();
}
int count=0;
int sum=0;
int Limit1 = sc.nextInt();
int Limit2 = sc.nextInt();
for(int j=0;j<n;j++){
if(arr[j]>Limit1 && arr[j]<Limit2){
sum = sum + arr[j];
count++;
}
}
System.out.print(sum/count);
}
}
Sample Input:
n=5
arr = [1, 2, 3, 4, 5]
Limit1 = 2
Limit2 = 6
Sample Output:
4
10
To find whether the given number is Perfect Number or not
import java.util.*;
class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int sum = 0;
for(int i=1;i<n;i++){
if(n%i==0){
sum = sum +i;
}
}
if(sum == n){
System.out.print("Perfect Number");
}
else{
System.out.print("Not Perfect Number");
}
}
}
Sample Input 1:
n=6
Sample Output:
Perfect Number
Sample Input 2:
n=8
Sample Output:
Not Perfect Number
11
To count the number of Spaces in a String
import java.util.*;
class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
int sum = 0;
for(int i=0;i<str.length();i++){
if(str.charAt(i)==' '){
sum = sum + 1;
}
}
System.out.print(sum);
}
}
Sample Input :
Hello Hi
Sample Output:
1
12
To Remove the Duplicate Character from the string(including Spaces)
import java.util.*;
class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
String s = "";
for(int i=0;i<str.length();i++){
for(int j=0;j<=i;j++){
if(i==j){
s = s + str.charAt(i);
}
else if(str.charAt(i)==str.charAt(j)){
break;
}
}
}
System.out.print(s);
}
}
Sample Input :
Hello hey guys
Sample Output:
Helo hygus
13
To find the Largest Word in the given String
import java.util.*;
class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
String largest = "";
String[] arr = str.split(" ");
for(int i=0;i<arr.length;i++){
if(arr[i].length()>largest.length()){
largest = arr[i];
}
}
System.out.print(largest);
}
}
Sample Input :
Hey Hello you are having any trouble
Sample Output:
trouble
14
To find the Second Largest Element from an Array
import java.util.*;
class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for(int i=0;i<n;i++){
arr[i] = sc.nextInt();
}
int largest = 0;
int secondLargest = arr[0];
for(int j=0;j<arr.length;j++){
if(arr[j]>largest){
secondLargest = largest;
largest = arr[j];
}
else if(arr[j] < largest && arr[j] > secondLargest){
secondLargest = arr[j];
}
}
System.out.println(secondLargest);
}
}
Sample Input:
n=5
Array = [4, 8, 12, 6, 2]
Sample Output:
8
15