Print all substrings of a given string:
import java.util.Scanner;
class SubstringsOfAString
{
public static void main(String args[])
{
String string, sub;
int i, c, length;
Scanner in = new Scanner(System.in);
System.out.println("Enter a string to print it's all substrings");
string = in.nextLine();
length = string.length();
System.out.println("Substrings of \""+string+"\" are:");
for (c = 0; c < length; c++)
{
for(i = 1; i <= length - c; i++)
{
sub = string.substring(c, c+i);
System.out.println(sub);
}
}
}
}
Output:
D:\Java pgm>java SubstringsOfAString
Enter a string to print it's all substrings
welcome
Substrings of "welcome" are:
w
we
wel
welc
welco
welcom
welcome
e
el
elc
elco
elcom
elcome
l
lc
lco
lcom
lcome
c
co
com
come
o
om
ome
m
me
e
Date:
import java.text.SimpleDateFormat;
import java.util.Date;
import java.text.ParseException;
public class Example{
public static boolean validateJavaDate(String strDate)
{
/* Check if date is 'null' */
if (strDate.trim().equals(""))
{
return true;
}
/* Date is not 'null' */
else
{
/*
* Set preferred date format,
* For example MM-dd-yyyy, MM.dd.yyyy,dd.MM.yyyy etc.*/
SimpleDateFormat sdfrmt = new SimpleDateFormat("MM/dd/yyyy");
sdfrmt.setLenient(false);
/* Create Date object
* parse the string into date
*/
try
{
Date javaDate = sdfrmt.parse(strDate);
System.out.println(strDate+" is valid date format");
}
/* Date format is invalid */
catch (ParseException e)
{
System.out.println(strDate+" is Invalid Date format");
return false;
}
/* Return true if date format is valid */
return true;
}
}
public static void main(String args[]){
validateJavaDate("12/29/2016");
validateJavaDate("12-29-2016");
validateJavaDate("12,29,2016");
}
}
Output:
D:\Java pgm>javac Example.java
D:\Java pgm>java Example
12/29/2016 is valid date format
12-29-2016 is Invalid Date format
12,29,2016 is Invalid Date format
Substring:
class GFG {
// Returns true if s1 is substring of s2
static int isSubstring(String s1, String s2)
{
int M = s1.length();
int N = s2.length();
/* A loop to slide pat[] one by one */
for (int i = 0; i <= N - M; i++) {
int j;
/* For current index i, check for
pattern match */
for (j = 0; j < M; j++)
if (s2.charAt(i + j) != s1.charAt(j))
break;
if (j == M)
return i;
}
return -1;
}
/* Driver program to test above function */
public static void main(String args[])
{
String s1 = "for";
String s2 = "geeksforgeeks";
int res = isSubstring(s1, s2);
if (res == -1)
System.out.println("Not present");
else
System.out.println("Present at index "
+ res);
}
}
Output:
D:\Java pgm>java GFG
Present at index 5
Date sorting:
import java.util.*;
import java.text.*;
public class app{
public static void main(String[] args) {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
List<Date> birthdays = new ArrayList<>();
try
{
birthdays.add(dateFormat.parse("2016-01-20"));
birthdays.add(dateFormat.parse("1998-12-03"));
birthdays.add(dateFormat.parse("2009-07-15"));
birthdays.add(dateFormat.parse("2012-04-30"));
}
catch(Exception e)
{}
System.out.println("Before sorting: ");
for (Date date : birthdays) {
System.out.println(dateFormat.format(date));
}
Collections.sort(birthdays);
System.out.println("After sorting: ");
for (Date date : birthdays) {
System.out.println(dateFormat.format(date));
}
}
}
Output:
D:\Java pgm>java app
Before sorting:
2016-01-20
1998-12-03
2009-07-15
2012-04-30
After sorting:
1998-12-03
2009-07-15
2012-04-30
2016-01-20
Date Comparison:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class app{
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = sdf.parse("2009-12-31");
Date date2 = sdf.parse("2010-01-31");
System.out.println("date1 : " + sdf.format(date1));
System.out.println("date2 : " + sdf.format(date2));
if (date1.compareTo(date2) > 0) {
System.out.println("Date1 is after Date2");
} else if (date1.compareTo(date2) < 0) {
System.out.println("Date1 is before Date2");
} else if (date1.compareTo(date2) == 0) {
System.out.println("Date1 is equal to Date2");
} else {
System.out.println("How to get here?");
}
}
Output:
D:\Java pgm>java app
date1 : 2009-12-31
date2 : 2010-01-31
Date1 is before Date2
Count number of words in statement:
class app
{
public static void main(String args[])
{
int word=1;
String str="count number of words and sapces";
for(int i=0;i<str.length();++i)
{
if(str.charAt(i)==' ')
word++;
}
System.out.println("Number of words="+word);
System.out.println("Number of spaces="+(word-1));
}
}
Output:
D:\Java pgm>java app
Number of words=6
Number of spaces=5
Largest in array:
import java.util.Scanner;
public class app
{
public static void main(String[] args)
{
int n, max;
Scanner s = new Scanner(System.in);
System.out.print("Enter number of elements in the array:");
n = s.nextInt();
int a[] = new int[n];
System.out.println("Enter elements of array:");
for(int i = 0; i < n; i++)
{
a[i] = s.nextInt();
}
max = a[0];
for(int i = 0; i < n; i++)
{
if(max < a[i])
{
max = a[i];
}
}
System.out.println("Maximum value:"+max);
}
}
Output:
D:\Java pgm>java app
Enter number of elements in the array:4
Enter elements of array:
10
40
20
30
Maximum value:40
Array Sorting:
import java.util.Scanner;
public class app
{
public static void main(String[] args)
{
int n, temp;
Scanner s = new Scanner(System.in);
System.out.print("Enter no. of elements you want in array:");
n = s.nextInt();
int a[] = new int[n];
System.out.println("Enter all the elements:");
for (int i = 0; i < n; i++)
{
a[i] = s.nextInt();
}
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
System.out.print("Ascending Order:");
for (int i = 0; i < n - 1; i++)
{
System.out.print(a[i] + ",");
}
System.out.print(a[n - 1]);
}
}
Output:
D:\Java pgm>java app
Enter no. of elements you want in array:5
Enter all the elements:
42
10
256
6
7
Ascending Order:6,7,10,42,256
Occurrence of each character:
import java.util.Scanner;
public class app {
private static Scanner scan = null;
public static void main(String[] args) {
scan = new Scanner(System.in);
String str = null;
System.out.println("Enter String : ");
str = scan.nextLine();
int[] count = new int[255];
int length = str.length();
for (int i = 0; i < length; i++) {
count[str.charAt(i)]++;
}
char[] ch = new char[str.length()];
for (int i = 0; i < length; i++) {
ch[i] = str.charAt(i);
int find = 0;
for (int j = 0; j <= i; j++) {
if (str.charAt(i) == ch[j])
find++;
}
if (find == 1) {
System.out.println("Number of Occurrence of " + str.charAt(i) + " letter is:" + count[str.charAt(i)]);
}
}
}
}
Output:
D:\Java pgm>java app
Enter String :
welcome to college
Number of Occurrence of w letter is:1
Number of Occurrence of e letter is:4
Number of Occurrence of l letter is:3
Number of Occurrence of c letter is:2
Number of Occurrence of o letter is:3
Number of Occurrence of m letter is:1
Number of Occurrence of letter is:2
Number of Occurrence of t letter is:1
Number of Occurrence of g letter is:1