Question 10:

Define a class to accept and store 10 strings into the array and print the strings with even number of characters.

Note : even no of CHARACTERS does not mean if length of string is even It means whether the no of characters(spaces don't count) are even


import java.util.*;
class evenString
{
    public static void main(String[] args) 
    {
        Scanner sc = new Scanner(System.in) ;
        evenString tool = new evenString() ;

        String arr[] = new String[10] ;
        System.out.println("Enter 10 sentences") ;

        String newArr[] = new String[10] ;
        int index = 0 ;
        for(int j=0;j< 10;j++)
        {
            arr[j] = sc.nextLine() ;
            newArr[j] = tool.eliminateSpaces(arr[j]) ;

            if(newArr[j].length()%2==0)
            {
                index*=10 ;
                index+=j ;
            }
        }
        System.out.println("") ;
        System.out.println("The following are the even strings") ;
        while(index!=0)
        {
            System.out.println(arr[index%10]) ;
            index /= 10 ;
        }
    }

    public String eliminateSpaces(String str)
    {
        String newStr="" ;
        for(int i=0;i< str.length();i++)
        {
            if(str.charAt(i)!=' ')
            newStr +=str.charAt(i) ;
        }
        return newStr ;
    }
} 

           
            
Codely Prompt ----->java evenString.java
Enter 10 sentences
Lorem ipsum dolor sit amet, consectetur adipiscing elit
Curabitur in felis at quam porta accumsan
Nulla et sem interdum, fermentum sapien a
porta at eleifend nec, venenatis ac nulla
Curabitur eu velit accumsan, molestie magna vel
Pellentesque id erat vitae urna convallis pretium eget quis leo
Vestibulum accumsan sapien at arcu malesuada placerat
Sed id magna fermentum
Praesent cursus id tellus sed rhoncus
Nunc viverra vulputate dui

The following are the even strings
Praesent cursus id tellus sed rhoncus
Pellentesque id erat vitae urna convallis pretium eget quis leo
            
        

Contribution by :-

Binit Image

Binit Ranjan Das