1. Write a Java program to count the letters, spaces, numbers
and other characters of an input string.
2. Write a Java program to print numbers between 1 to 100 which are
divisible by 3, 5 and by both
3. Write a Java program to convert a given string into
lowercase
Note: Could you plz go through this code and let me
know if u need any changes in this.Thank You
=================================
1)
// CountChars.java
import java.util.Scanner;
public class CountChars {
public static void main(String[] args) {
/*
* Declaring variables
*/
int cntLetters = 0, cntSpaces = 0,
cntNumbers = 0, cntOtherChars = 0;
String str = "";
char ch;
/*
* Creating an Scanner class object
which is used to get the inputs
* entered by the user
*/
Scanner sc = new
Scanner(System.in);
// Getting the input entered by
the user
System.out.print("Enter String
:");
str = sc.nextLine();
for (int i = 0; i <
str.length(); i++) {
ch =
str.charAt(i);
if ((ch >=
'A' && ch <= 'Z') || (ch >= 'a' && ch <=
'z')) {
// Counting no of letters
cntLetters++;
} else if (ch
>= '0' && ch <= '9') {
// counting no of digits
cntNumbers++;
} else if (ch ==
' ') {
// Counting no of letters
cntSpaces++;
} else {
// counting no of special chars
cntOtherChars++;
}
}
// Displaying the output
System.out.println("No of Letters
:" + cntLetters);
System.out.println("No of Digits :"
+ cntNumbers);
System.out.println("No of Spaces :"
+ cntSpaces);
System.out.println("No of Other
characters :" + cntOtherChars);
}
}
========================================
===========================================
Output:
===========================================
2)
// PrintNosDivisibleBy3Or5OrBoth.java
public class PrintNosDivisibleBy3Or5OrBoth {
public static void main(String[] args)
{
int cnt=0;
// Displaying the numbers which are
divisible by 3 or 5 or both
for (int i = 1; i <= 100; i++)
{
if ((i % 3 == 0)
|| (i % 5 == 0) || ((i % 3 == 0) && (i % 5 == 0))) {
cnt++;
System.out.printf("%5d",i);
if(cnt%10==0)
{
System.out.println();
}
}
}
}
}
===========================================
===========================================
Output:
===========================================
3)
// ConvertUpperToLower.java
import java.util.Scanner;
public class ConvertUpperToLower {
public static void main(String[] args) {
/*
* Declaring variables
*/
String str = "", newStr = "";
char ch;
/*
* Creating an Scanner class object
which is used to get the inputs
* entered by the user
*/
Scanner sc = new
Scanner(System.in);
// Getting the input
entered by the user
System.out.print("Enter String
:");
str =
sc.nextLine();
for (int i = 0; i
< str.length(); i++) {
ch =
str.charAt(i);
if (ch >= 'A'
&& ch <= 'Z') {
ch = (char) (((int) ch) + 32);
newStr += ch;
} else {
newStr += ch;
}
}
System.out.println("After Converting to lowercase :" +
newStr);
}
}
========================================
===========================================
Output:
=====================Could you plz rate me well.Thank You
Get Answers For Free
Most questions answered within 1 hours.