01_ Coding: String reverse ............

 String reverse

Method 1. 

public static void main(String[] args){
        String input = "GeeksForGeeks";
        // convert String to character array
        // by using toCharArray
        char[] try1 = input.toCharArray();
 
        for (int i = try1.length - 1; i >= 0; i--)
            System.out.print(try1[i]);
    }

Method 2. 

public static void main (String[] args) {
        String str= "Geeks";
        String nstr="";
        char ch;
      System.out.print("Original word: ");
      System.out.println("Geeks"); //Example word
      for (int i=0; i<str.length(); i++)
      {
        ch= str.charAt(i); //extracts each character
        nstr= ch+nstr; //adds each character in front of the existing string
      }
      System.out.println("Reversed word: "+ nstr);
    }

Method 3.

 public static void main(String[] args)
    {
        String str = "Geeks";
        // conversion from String object to StringBuffer
        StringBuffer sbr = new StringBuffer(str);
        // To reverse the string
        sbr.reverse();
        System.out.println(sbr);
    }

Method 4.

public static void main(String[] args) {
        String input = "Geeks for Geeks";
        StringBuilder input1 = new StringBuilder();
        input1.append(input);
        input1.reverse();
        System.out.println(input1);
    }


Print a to z 

public static void main(String[] args) {

    char c;

    for (c = 'a'; c <= 'z'; c++) {

        System.out.print(c + ", ");

    }

} 

Rendom num generation with bound
public static void main(String[] args) {

int min=4;
int max =10;
int num = (int)(Math.random()*(max-min-1)+min);
System.out.println("random usint Math.random method " +num);
Random rn = new Random();
int ny= rn.nextInt(max-min-1)+min;
System.out.println("random usint random Class " +ny);

}

Date and time formate

//Formatting Date/Time to String:
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedDateTime = now.format(formatter2);
System.out.println(formattedDateTime);
//Parsing String to Date/Time:
String dateTimeString = "02-03-2024 14:30:00";
DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("MM-dd-yyyy HH:mm:ss");
LocalDateTime parsedDateTime = LocalDateTime.parse(dateTimeString, formatter3);
System.out.println(parsedDateTime);


























 

Comments

Popular posts from this blog

Important link