Friday, 2 October 2015

Basic puzzle in java programming #1

In this program the below scenario will be implemented.

Given a string, return a new string where "not " has been added to the front. However, if the string already begins with "not", return the string unchanged. Note: use .equals() to compare 2 strings.

notString("hi") ? "not hi"
notString("a") ? "not a"
notString("not bad") ? "not bad"

 /* Program to print
 * "hi" to "not hi"
 *  "String to not String"
 */

package in.blogspot.java2bigdata;

public class addNotToString {
 public String notString(String str) {
    if (str.length() >= 3 && str.substring(0, 3).equals("not")) {
      return str;
    }
    
    return "not " + str;
  }
 public static void main(String[] args){
  addNotToString word = new addNotToString();
  
  System.out.println(word.notString("Hi"));
  System.out.println(word.notString("good"));
  System.out.println(word.notString("not good"));
  System.out.println(word.notString("is not"));
  
 }

 }


click here for star pattern in java programming #1
click here for star pattern in java programming #2

No comments:

Post a Comment