In: Computer Science
Code a program which asks user to enter two strings using Scanner class and returns the total number of non-space characters in the Strings. For ex: “Hi there” and “hello” should return 12.
import java.util.*;
public class r{
public static void main(String[] args){
String s1,s2;
int count=0,i;
Scanner s = new Scanner(System.in);
System.out.print("Enter string 1 ");
s1 = s.nextLine();
System.out.print("Enter string 2 ");
s2=s.nextLine();
for(i=0;i<s1.length();i++){
if(s1.charAt(i)!=' ')
count++;
}
for(i=0;i<s2.length();i++){
if(s2.charAt(i)!=' ')
count++;
}
System.out.println("The total number of non space characters is
"+count);
}
}
Thank you.