Write a program in java to enter a string and count and print the number of non blank characters present in the string.
THE FOLLOWING PROGRAM SEGMENT SHOWS HOW TO PRINT THE NON-BLANK CHARACTERS FROM A STRING.
import java.util.*;
class NONblank
{
public static void main (String args[])
{
Scanner ob = new Scanner (System.in);
String s; int c = 0;
System.out.println("Enter a string");
s = ob.nextLine();
for(int i = 0; i < s.length(); i++)
{
char ch = s.charAt(i);
if( ch != ' ')
{
c++;
}
}
System.out.println("No of non blank characters present = "+c);
}
}
Hope you guys found it helpful.
Please share it with your friends and leave a comment in the comment section below.
Follow https://askrisfor.blogspot.com/ for more such programs.
Thank you !!!
Visit again.
Comments
Post a Comment