Print All Substrings of A Given String - JAVA
Print All Substrings of A Given String - JAVA
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
if (j == M)
return i;
}
return -1;
}
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.*;
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;
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