Showing posts with label Coding. Show all posts
Showing posts with label Coding. Show all posts

Saturday

Given 2 strings return a new string made of the first char of a and the last char of b.

0 comments
java convert char to string .charAt(). Java logic programming question with answer.
-Given 2 strings, a and b, return a new string made of the first char of a and the last char of b, so "yo" and "java" yields "ya". If either string is length 0, use '@' for its missing char.

lastChars("last", "chars") → "ls"
lastChars("yo", "java") → "ya"
lastChars("hi", "") → "h@"

 Solution :
 
     public String lastChars(String a, String b) {
 
             String n;
             int l2= a.length();
             int l3= b.length();
             char co,ct;
   
              if (l2 == 0)
                   co ='@';
              else
                  co = a.charAt(0);

Friday

A string of odd length, return the string length 3 from its middle, so "Candy" yields "and".

0 comments
Java Logic programming. remove white space from string, substring -Given a string of odd length, return the string length 3 from its middle, so "Candy" yields "and". The string length will be at least 3. 

middleThree("Candy") → "and"
middleThree("and") → "and"
middleThree("solving") → "lvi"

Solution :    (to print string length 3 from its middle)

  class techietouch {

  public void middleThree(String str) {
      String newsub;
      str=str.replaceAll("\\s","");
      int length = str.length();
         if (length == 3)
             System.out.println(str);
         int remo= (length /2)-1;