Java Program to Count Number of Characters in two Strings

Java Program Count Characters- In this Program, we are going to take two Strings let’s say s1, s2, and will count the Number of Characters of each String Respectively. After finding the Character count we will compare which string has more characters and thus return the largest String.

Example
String s1= "Welcome to Noteshacker "
String s1 =" Noteshacker is the best website"
character count of s1- 20
                   s2- 27
Largest String is String s2

Java Program Count Characters

Algorithm/Program Explanation

  1. Start
  2. Define and Initialize two Strings
    • String s1=”Welcome to Noteshacker.
    • String s2=”Noteshacker is the best Website
  3. Define Two For Loop to Count Characters of s1 and s2 Respectively
    • for(int i=0;i
    • Check for Condition
      • if(s1.charAt(i)!=’ ‘)
      • count1=count1+1
  4. for(int i=0;i
  5. Check for Condition
    • if(s2.charAt(i)!=’ ‘)
    • count2=count2+1
  6. Check for Condition
    • (count1>count)
    • String s1 is Greater
    • else String 2 is Greater
  7. End

Example-1 Java Program Count Characters | Static Inputs

/* 
Java Program To find Character Count of two Strings and return largest String */
import javax.swing.*;
import java.util.Scanner;

public class CountString {
    public static void main(String args[]){
        String s1="Welcome to Noteshacker";
        String s2="Noteshacker is the best Website";
                 int count1=0,count2=0;
                 for(int i=0;i<s1.length();i++){
                     if(s1.charAt(i)!=' '){
                        count1=count1+1;
                     }

                 }System.out.println("Character count of String s1:"+ count1);
        for(int i=0;i<s2.length();i++){
            if(s2.charAt(i)!= ' '){
                count2=count2+1;
            }

        }System.out.println("Character count of String s1:"+ count2);
        if(count1>count2) {
            System.out.println( s1 + "String 1 is Greater");
        }
         else
             System.out.println(s2 + " String 2 is greater");
            }
        }

Output:
Character count of String s1:20
Character count of String s1:27
Noteshacker is the best Website String 2 is greater

Example-2 User Defined Inputs

import javax.swing.*;
import java.util.Scanner;

public class CountString {
    public static void main(String args[]){
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the String -1");

        String s1=sc.next();
        System.out.println("Enter the String -2");
        String s2=sc.next();
                 int count1=0,count2=0;
                 for(int i=0;i<s1.length();i++){
                     if(s1.charAt(i)!=' '){
                        count1=count1+1;
                     }

                 }System.out.println("Character count of String s1:"+ count1);
        for(int i=0;i<s2.length();i++){
            if(s2.charAt(i)!= ' '){
                count2=count2+1;
            }

        }System.out.println("Character count of String s1:"+ count2);
        if(count1>count2) {
            System.out.println( s1 + "String 1 is Greater");
        }
         else
             System.out.println(s2 + " String 2 is greater");
            }
        }

Output:
Enter the String -1
Welcome
Enter the String -2
Noteshacker
Character count of String s1:7
Character count of String s1:11
Noteshacker String 2 is greater

Questions Asked in this Topic

  • Write a Java Program to count the number of Characters of two Different Strings.
  • Code for Comparing Two Different Strings and returning the largest among them?
  • Derive the code for Counting Characters of two string,after counting character return largest among them?
  • Given are the two String Count Number of Characters of each of them respectively?

Related Topics

Back

Leave a Comment