Write a code that returns the number of names in a given array that end in either "ie" or "y"?

Questions by brookscsloan

Showing Answers 1 - 2 of 2 Answers

The below code produces the output along with the names that have the conditions:
class StringTest
{
 public static void main(String[] args)
 {
  String s[]={"jennie","pety","jenifer"};
  int count =0;
  for (int k = 0;k<s.length;k++ )
  {
   String s3=s[k];
   int i=s3.length();
   
   char s1=s3.charAt(i-1);
   
   char s2=s3.charAt(i-2);
   
   if (s1=='e'&& s2=='i')
   {
   System.out.println("The Name which has ending letters as ie :" +s3);
   count = count+1;
   }
   else if(s1=='y')
    {
   System.out.println("The Name which has ending letter as y :" +s3);
   count = count+1;
   }
   
  }
  System.out.println("Total Names which have ending letters as 'ie' or 'y ' =" + count);
  
  
 }
}

  Was this answer useful?  Yes

disskyz29

  • Sep 3rd, 2008
 

public class Names {
 public static void main (String [] args)

 {
  int count = 0;
  String names[] = {"Valerie", "Jia", "Debbie", "Harvey", "Winslow", "Suzy"  };
  for (int i = 0; i <names.length; i++)
  {
   String a = "y";
   String b = "ie";
   String result;
   result = names[i];
   boolean yChar = result.endsWith(a);
   boolean ieChar = result.endsWith(b);
   if (yChar || ieChar){
    count++;
   }
    
  }
  System.out.println("The number of names ending with ie or y is " + count);
 }
}

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions