String Related programs in java
Java program to count the number of words in a string? solution 1: Using Split() method import java.util.Scanner; class WordsCount { public static void main( String [] args) { System . out .println("Enter the string"); Scanner sc = new Scanner( System . in ); String s = sc.nextLine(); String [] words = s.trim().split(" "); System . out .println("Number of words in the string = " + words. length ); } } solution 2: Using charAt(-) method import java.util.Scanner; class WordsCount2{ public static void main( String [] args) { System .out.println("Enter the string"); Scanner sc = new Scanner ( System . in ); String s=sc.nextLine(); int count = 1; for ( int i = 0; i < s.length()-1; i++) { if ((s.charAt(i) == ' ') && (s.charAt(i+1) != ' ')) { count++; } ...