Java Count Characters Not Next to Whitespace

Day 7 Java Logic: Count characters that are NOT next to whitespace While practicing Java, I tried an interesting logic problem: 👉 Count the number of characters in a string that are neither immediately before nor immediately after a whitespace. For example: "My name is Madhukar" We don’t count characters that touch a space on either side. Here’s the Java program I wrote: // Online Java Compiler // Use this editor to write, compile and run your Java code online class Main {       public static void main(String[] args) {           String s ="My name is Madhukar";           char [] charArray=s.toCharArray();           int count=0;           for(int i=0 ;i<charArray.length;i++)     {       if(charArray[i]==' ')       continue;                       boolean whiteSpacePresentAfterCharacter=(i<charArray.length-1 && charArray[i+1]==' ');               boolean whiteSpacePresentBeforeCharacter=(i>0 && charArray[i-1]==' ');               if(!whiteSpacePresentAfterCharacter && !whiteSpacePresentBeforeCharacter)       {         count++;       }     }           System.out.println("Number of chacarters before and after whitespace:"+count);               } } O/P:Number of chacarters before and after whitespace:10 #Java #CodingPractice #ProblemSolving #Programming

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories