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
- Start
- Define and Initialize two Strings
- String s1=”Welcome to Noteshacker.
- String s2=”Noteshacker is the best Website
- 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
- Check for Condition
- for(int i=0;i
- for(int i=0;i
- Check for Condition
- if(s2.charAt(i)!=’ ‘)
- count2=count2+1
- Check for Condition
- Check for Condition
- (count1>count)
- String s1 is Greater
- else String 2 is Greater
- 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
- Arithmetic Operations Java Program
- Java program to perform Logical and Relational operations
- Java program to Implement Mathematical functions defined in Java Math class
- Java basic Programs with Explanation
- Java Program to find Min,Max Element in an Array
- Java Program to sort Array Elements in ascending and descending order
- Java Program to display Array item at Even & Odd position
- Program to Calculate Sum and Average of Array elements
- Java Program to find Index of specific Element in an array
- Program to find common elements between two arrays
- Java Program to Add/Insert specific Element to an Array
- Java Program to delete specific element from an Array
- Program to calculate frequency of each element in Array
- Java Program to find Even and Odd Numbers in the arrays
- Java Program to search Given Number in an Array
- Java Program to Add or Sub Two Matrices
- Java Program to Multiply two or More Matrices
- Java Program to Print Array in Reverse Order
- Java Program To Transpose Given Matrix
- Java Programs List- Java Programs Examples With Output