Question

1. Write a Java program to count the letters, spaces, numbers and other characters of an...

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

Homework Answers

Answer #1

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
1 Design and implement FileCompare program that compares two text input files (file1.txt and file2.txt), line-by-line,...
1 Design and implement FileCompare program that compares two text input files (file1.txt and file2.txt), line-by-line, for equality. Print any lines that are not equivalent indicating the line numbers in both files. The language of implementation is java 2 . Create a program that reads a string input from the user, then determines and prints how many of each lowercase vowels (a, e. i, o, and u) appear in the entire string. Have a separate counter for each vowel. Also...
Java Write a program that prints the letters corresponding to the numbers on the telephone. So...
Java Write a program that prints the letters corresponding to the numbers on the telephone. So if you were to enter 6 it should print MNO. Now since you do not know how to do a System.in just do you for the number 7. So your code should be like int numberEntered = 7; String value; swtich (numberEntered){ case 1: value = "Nothing definded for 1"; break; case 2 : value = "A,B,C"; break ..... ..... default: value = "Not...
Write a java program that prints the letters corresponding to the numbers on the telephone. So...
Write a java program that prints the letters corresponding to the numbers on the telephone. So if you were to enter 6 it should print MNO. Now since you do not know how to do a System.in just do you for the number 7. So your code should be like int numberEntered = 7; String value; swtich (numberEntered){ case 1: value = "Nothing definded for 1"; break; case 2 : value = "A,B,C"; break ..... ..... default: value = "Not...
PYTHON 3 Write a program that prints the count of all prime numbers between A and...
PYTHON 3 Write a program that prints the count of all prime numbers between A and B (inclusive), where A and B are defined as follows: A = 21212 B = A + 5000 Just a recap on prime numbers: A prime number is any number, greater or equal to 2, that is divisible ONLY by 1 and itself. Here are the first 10 prime numbers: 2, 5, 7, 11, 13, 17, 19, 23, and 29. Rules: You should first...
Using for loops: - write a Java program that adds numbers between 1 to 100 and...
Using for loops: - write a Java program that adds numbers between 1 to 100 and prints the result. - write a Java program that adds odd numbers between 1 to 100 and prints the result - write a Java program that averages even numbers between 5 to 30 and prints the result
Write a program in python that prints the count of all prime numbers between A and...
Write a program in python that prints the count of all prime numbers between A and B (inclusive), where A and B are defined as follows: A = The 5 digit unique number you had picked at the beginning of the semester B = A + 5000 Just a recap on prime numbers: A prime number is any number, greater or equal to 2, that is divisible ONLY by 1 and itself. Here are the first 10 prime numbers: 2,...
Write a program that takes a string of characters (including spaces) as input, computes the frequency...
Write a program that takes a string of characters (including spaces) as input, computes the frequency of each character, sorts them by frequency, and outputs the Huffman code for each character.   When you are writing your program, you should first test it on a string of 7 characters, so you can check it. PLEASE NOTE: Your program must work for any text that has upper and lower case letters digits 0 - 9, commas, periods, and spaces. Please submit the...
Write an assembly program that reads characters from standard input until the “end of file” is...
Write an assembly program that reads characters from standard input until the “end of file” is reached. The input provided to the program contains A, C, T, and G characters. The file also may have new line characters (ASCII code 10 decimal), which should be skipped/ignored. The program then must print the count for each character. You can assume (in this whole assignment) that the input doe not contain any other kinds of characters.  the X86 assembly program that simply counts...
Write a Java program that prompts the user to input a word (String). The program must...
Write a Java program that prompts the user to input a word (String). The program must print the reversed word with all consecutive duplicate characters removed. The program must contain the following classes: - The StackX class (you can use the Java Stack class). - The Reverse class which must contain a private data field called “word” of type string, a constructor, and a void method called revNoDup(). The revNoDup() method must reverse the word and remove the consecutive duplicate...
C++ PROGRAM. (C++ INTRO QUESTION) Write a program that prints the count of all prime numbers...
C++ PROGRAM. (C++ INTRO QUESTION) Write a program that prints the count of all prime numbers between A and B (inclusive), where A and B are defined as follows: A = 55000 B = A + 5000 Just a recap on prime numbers: A prime number is any number, greater or equal to 2, that is divisible ONLY by 1 and itself. Here are the first 10 prime numbers: 2, 5, 7, 11, 13, 17, 19, 23, and 29. Rules:...