next


Number Of Vowels In the given String


import java.util.Scanner;

public class NumberOfVowels {
	public static void main(String args[]) {
		int output1 = 0; // Number of vowels
		String input1; // Input String

		System.out.println("Enter the String : ");
		Scanner in = new Scanner(System.in);
		input1 = in.nextLine();

		input1 = input1.toLowerCase(); // Converts into lower case
		for (int i = 0; i < input1.length(); i++) {
			char a = input1.charAt(i);
			if (a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u') {
				output1++;
			}
		}
		System.out.println("Number of Vowels is " + output1);
	}
}


							
							

Delete Repeated Elements in the Given String

import java.util.Scanner;

public class DeleteRepeatedElements {
	public static void main(String args[]) {
		String output = "";
		Scanner in = new Scanner(System.in);
		System.out.println("Enter the Input String : ");
		String input1 = in.next();

		StringBuffer sb = new StringBuffer(input1);

		for (int i = 0; i < sb.length(); i++) {
			for (int j = i + 1; j < sb.length(); j++) {
				if (sb.charAt(i) == sb.charAt(j)) {
					sb = sb.deleteCharAt(j);
				}
			}
		}
		output = sb.toString();
		System.out.println("Output String is " + output);
	}
}

Count Strings Prefix

/** 
 * String[] input={"100","111","10100","10","1111"}
 * output=2;
 * 
 * String[] input={"01","01010","1000","10","011"}
 * output=3;
 * 
 * Count the strings having prefix"10","01" but "10","01" not included
 */



import java.util.Scanner;

public class CountStringsPrefix {
	public static void main(String args[]) {
		Scanner in = new Scanner(System.in);
		System.out.println("Enter the size of the Array : ");
		int size = in.nextInt();

		String[] input = new String[size];
		System.out.println("Enter the Array Elements : ");
		for (int i = 0; i < input.length; i++) {
			input[i] = in.next();
		}
		int count = 0;
		for (int i = 0; i < input.length; i++) {
			if (input[i].length() > 2 && input[i] != "10" && input[i] != "01"
					&& (input[i].startsWith("01") || input[i].startsWith("10"))) {
				count++;
			}
		}

		System.out.println("Count is " + count);

	}
}

ArrayListSortToString

/**
 * ArrayList input={"a","d","c","b"};
 * String[] output={"a","b","c","d"}; 
 */

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ArrayListSortToString {
	public static void main(String args[]) {

		List input = new ArrayList();
		input.add("a");
		input.add("d");
		input.add("c");
		input.add("b");

		Collections.sort(input);

		String[] output = input.toArray(new String[input.size()]);

		for (int i = 0; i < output.length; i++) {
			System.out.println(output[i]);
		}

	}
}

CalculateEscape13

/**
 * input1=1,inoput2=2,onput3=3 --- output=6;
 * input1=1,inoput2=13,onput3=3 --- output=1;
 * input1=13,inoput2=2,onput3=8 --- output=8;
 * if value equal to 13,escape the 13 value as well as next value to 13.sum the remaining values
 */



import java.util.Scanner;

public class CalculateEscape13 {
	public static void main(String args[]) {

		Scanner in = new Scanner(System.in);

		System.out.println("Enter the Inputs : ");
		int input1 = in.nextInt();
		int input2 = in.nextInt();
		int input3 = in.nextInt();

		int[] a = { input1, input2, input3 };

		int output = 0;
		for (int i = 0; i < a.length; i++) {
			if (a[i] == 13) {
				i = i + 1;
			} else {
				output = output + a[i];
			}
		}
		System.out.println("Sum is " + output);
	}
}


AlternativePositionsOfString

/**
 * input="hello"
 * output="hlo"; Alternative positions
 */



import java.util.Scanner;

public class AlternativePositionsOfString {
	public static void main(String args[]) {
		Scanner in = new Scanner(System.in);
		System.out.println("Enter the String : ");
		String input = in.next();
		String output = "";

		for (int i = 0; i < input.length(); i++) {
			if ((2 * i) < input.length()) {
				output = output + input.charAt(2 * i);
			}
		}
		System.out.println("Output String is " + output);
	}
}

OUTPUT: PostDecrement Is 10

SumOfOddNumbersWithinRange

/**
 * Sum of odd number within the input range:
 * input=9;
 * output=1+3+5+7+9=25
 */



import java.util.Scanner;

public class SumOfOddNumbersWithinRange {
	public static void main(String args[]) {
		Scanner in = new Scanner(System.in);
		System.out.println("Enter the Range : ");
		int input = in.nextInt();
		int output = 0;
		for (int i = 1; i <= input; i++) {
			if (i % 2 != 0) {
				output = output + i;
			}
		}
		System.out.println("Sum of Odd Numbers is " + output);
	}
}

CamelCaseString

/**
 * Input1=”I love my country”
 * Output- “I Love My Country” 
 */



import java.util.Scanner;
import java.util.StringTokenizer;

public class CamelCaseString {
	public static void main(String args[]) {
		Scanner in = new Scanner(System.in);
		System.out.println("Enter the Input String : ");
		String input = in.nextLine();
		String output = "";

		StringTokenizer st = new StringTokenizer(input);

		while (st.hasMoreTokens()) {
			String token = st.nextToken();
			token = token.substring(0, 1).toUpperCase()
					+ token.substring(1).toLowerCase();
			output = output + token + " ";
		}
		output = output.trim().toString();

		System.out.println(output);
	}
}

SwapFirstandLastChar


/**
 * Input1=”Hello World”;
 * Output = “dello WorlH”
 */



import java.util.Scanner;

public class SwapFirstandLastChar {
	public static void main(String args[]) {
		Scanner in = new Scanner(System.in);
		System.out.println("Enter the Input String : ");
		String input = in.nextLine();
		String output = "";

		char[] a = input.toCharArray();
		char first = a[0];
		char last = a[a.length - 1];

		a[0] = last;
		a[a.length - 1] = first;

		for (int i = 0; i < a.length; i++) {
			output = output + a[i];
		}
		output = output.toString();

		System.out.println("Output String is " + output);
	}

}
    

ListDifferenceToIntArray

/**Collect no’s  from  list1 which is not present in list2 & Collect no’s  from  list2 which is not present in list1 and store
 * input1={1,2,3,4}; 
 * input2={1,2,3,5};
 * int[] output={4,5};
 */

import java.util.ArrayList;
import java.util.List;

public class ListDifferenceToIntArray {
	public static void main(String args[]) {
		List a = new ArrayList();
		a.add(1);
		a.add(2);
		a.add(3);
		a.add(4);

		List b = new ArrayList();
		b.add(1);
		b.add(2);
		b.add(3);
		b.add(5);

		List c = new ArrayList(a);
		List d = new ArrayList(b);

		// A-B
		c.removeAll(b);
		System.out.println(c);

		// B-A
		d.removeAll(a);
		System.out.println(d);

		// C+D
		c.addAll(d);

		Integer[] output = new Integer[c.size()];
		c.toArray(output);
		// Wrapper classes should be used when converting from collections

		System.out.println("Array Elements are : ");
		for (int i = 0; i < output.length; i++) {
			System.out.println(output[i]);
		}
	}
}

							
							

CountSubstringInStringArrays

/**
 * String array will be given. 
 * If a string is Prefix of an any other string in that array means count 
 */



import java.util.Scanner;

public class CountSubstringInStringArrays {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		System.out.println("Enter the Size : ");
		int size = in.nextInt();

		String[] input = new String[size];
		System.out.println("Enter the Array Values : ");
		for (int i = 0; i < input.length; i++) {
			input[i] = in.next();
		}

		int output = 0;
		int count;
		for (int i = 0; i < input.length; i++) {
			count = 0;
			for (int j = 0; j < input.length; j++) {

				if (input[j].contains(input[i])) {
					count++;
				}
			}
			if (count > 1) {
				output++;
			}
		}
		System.out.println("Count is " + output);

	}
}

CountNumberOfWords

/** count the number of words in the string
 * Input string="i work in cognizant"
 * output=4
 */


import java.util.Scanner;
import java.util.StringTokenizer;

public class CountNumberOfWords {
	public static void main(String[] args) {
		String input;
		int output;

		Scanner in = new Scanner(System.in);
		System.out.println("Enter the String : ");
		input = in.nextLine();

		input = input.replace(',', ' ');

		StringTokenizer st = new StringTokenizer(input);
		output = st.countTokens();

		System.out.println(output);

	}
}

FindDayofDate

/**
 * Given input="24/07/1990";
 * String output=tuesday;
 * output should be the day in the particular month and it should be in lowercases
 */


import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class FindDayofDate {
	public static void main(String args[]) throws ParseException {
		Scanner in = new Scanner(System.in);

		System.out.println("Enter the Date in DD/MM/YYYY : ");
		String input = in.next();

		DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
		Date newdate = dateFormat.parse(input);

		dateFormat = new SimpleDateFormat("EEEE");
		String output = dateFormat.format(newdate);
		output = output.toLowerCase();

		System.out.println("Day is " + output);
	}
}

Sequenceof123

/**
 * int[] input={2,1,4,1,2,3,6};
 * check whether the input is the sequence of 1,2,3
 * if so-output=true;
 * int [] input={12,1,3,4,5,6};
 * output=false;
 */

import java.util.Scanner;

public class Sequenceof123 {
	public static void main(String args[]) {

		Scanner in = new Scanner(System.in);
		boolean output = true;

		System.out.println("Enter the Size : ");
		int size = in.nextInt();

		int[] input = new int[size];
		System.out.println("Enter the Array Values : ");

		for (int i = 0; i < input.length; i++) {
			input[i] = in.nextInt();
		}

		int count = 0;
		for (int i = 0; i < input.length - 2; i++) {
			if (input[i] == 1 && input[i + 1] == 2 && input[i + 2] == 3) {
				count++;
			}
		}

		if (count >= 1) {
			output = true;
		} else {
			output = false;
		}

		System.out.println("Output Status is " + output);

	}
}

FirstCharacterIsNumberOrNot

/**
 * If first char of string is a number then false else true.
 * 12DFGR=false;
 * SDFG=True;
 */


import java.util.Scanner;

public class FirstCharacterIsNumberOrNot {
	public static void main(String args[]) {
		Scanner in = new Scanner(System.in);
		System.out.println("Enter the String : ");
		String input = in.next();
		boolean output = false;

		char first = input.charAt(0);

		if (Character.isDigit(first)) {
			output = true;
		} else {
			output = false;
		}
		System.out.println("Output Status is : " + output);

	}
}

RemoveSlashStoreOutputArray

/**
 * String input1="AAA/abb/CCC"
 * char   input2='/'
 * output-- String[] output;
 * output[]={"aaa","bba","ccc"};
 * operation-- get the strings from input1using stringtokenizer reverse each string then to lower case finally store it in output1[] string array  
 */


import java.util.Scanner;
import java.util.StringTokenizer;

public class RemoveSlashStoreOutputArray {
	public static void main(String args[]) {
		Scanner in = new Scanner(System.in);
		System.out.println("Enter the Input String : ");
		String input = in.next();
		String input2 = "/";

		int size = 0;
		String token = "";

		StringTokenizer st = new StringTokenizer(input, input2);
		size = st.countTokens();

		String[] output = new String[size];
		int i = 0;
		while (st.hasMoreTokens()) {
			token = (st.nextToken()).toLowerCase();
			output[i] = token;
			i++;
		}

		StringBuffer sb;

		for (int j = 0; j < output.length; j++) {
			sb = new StringBuffer();
			sb.append(output[j]);
			output[j] = sb.reverse().toString();
		}

		for (int k = 0; k < output.length; k++) {
			System.out.println(output[k]);
		}
	}
}

AppendStringsNTimes

/**
 * Input-- String input1="Lily"
 * int input2=2
 * output-- String output1="LilyLily"
 */


import java.util.Scanner;

public class AppendStringsNTimes {
	public static void main(String args[]) {
		Scanner in = new Scanner(System.in);
		System.out.println("Enter the Input String ");
		String input = in.next();
		String output = "";

		System.out.println("Enter the Input2 ");
		int input2 = in.nextInt();

		for (int i = 0; i < input2; i++) {
			output = output.concat(input);
		}

		System.out.println("Output String is " + output);
	}

}

PrintDifferentOrSum

/**
 * Input---input1=1;
 * input2=4;
 * input3=1;
 * output1=4;
 * input---input1=1;
 * input2=2;
 * input3=3;
 * output1=6;
 * Operation--- print the element which is not repeated
 * if all the inputs r different sum all inputs 
 */

import java.util.Scanner;

public class PrintDifferentOrSum {
	public static void main(String args[]) {
		Scanner in = new Scanner(System.in);
		System.out.println("Enter the Inputs 1,2 and 3 : ");
		int input1 = in.nextInt();
		int input2 = in.nextInt();
		int input3 = in.nextInt();
		int output = 0;

		int[] a = { input1, input2, input3 };

		for (int i = 0; i < a.length; i++) {
			for (int j = i + 1; j < a.length; j++) {
				if (a[i] == a[j]) {
					a[i] = 0;
					a[j] = 0;
				}
			}
		}

		for (int i = 0; i < a.length; i++) {
			output = output + a[i];
		}
		System.out.println("Output is " + output);
	}
}

RemoveFromListStartingWithCharAndStoreInStringArray

/**
 * input1-List1-{apple,orange,grapes}
 * input2-List2-{melon,apple,mango}
 * output={mango,orange}
 * operation-- In 1st list remove strings starting with 'a' or 'g'
 * In 2nd list remove strings ending with 'm' or 'e'
 * Ignore case
 * return in string array
 */

package programs;

import java.util.ArrayList;
import java.util.List;

public class RemoveFromListStartingWithCharAndStoreInStringArray {
	public static void main(String args[]) {
		List list1 = new ArrayList();
		list1.add("Apple");
		list1.add("grapes");
		list1.add("orange");
		list1.add("grapes");
		System.out.println(list1);

		List list2 = new ArrayList();
		list2.add("Melon");
		list2.add("apple");
		list2.add("mango");
		System.out.println(list2);

		// List 1
		for (int i = 0; i < list1.size(); i++) {
			if (list1.get(i).startsWith("A") || list1.get(i).startsWith("a")
					|| list1.get(i).startsWith("G")
					|| list1.get(i).startsWith("g")) {
				list1.remove(i);
				i--;
			}
		}
		System.out.println(list1);

		// List 2
		for (int i = 0; i < list2.size(); i++) {
			if (list2.get(i).endsWith("M") || list2.get(i).endsWith("m")
					|| list2.get(i).endsWith("E") || list2.get(i).endsWith("e")) {
				list2.remove(i);
				i--;
			}
		}
		System.out.println(list2);

		// Storing both the list elements in String array
		list1.addAll(list2);
		String[] output = new String[list1.size()];
		list1.toArray(output);

		System.out.println("Array Elements are : ");
		for (int i = 0; i < output.length; i++) {
			System.out.println(output[i]);
		}
	}
}

CompareCharacterBeforeStarAndReturnBoolean

/**
 * input1- Hello*world
 * output-- boolean(true or false)
 * operation-- if the character before and after * are same return true else false
 * if there in no star in the string return false
 * Ignore case
 */

import java.util.Scanner;

public class CompareCharacterBeforeStarAndReturnBoolean {
	public static void main(String args[]) {
		Scanner in = new Scanner(System.in);
		System.out.println("Enter the String : ");
		String input = in.next();
		boolean output = false;

		if (input.contains("*")) {
			int index = input.indexOf('*');
			String before = "" + input.charAt(index - 1);
			String next = "" + input.charAt(index + 1);
			if (before.equalsIgnoreCase(next)) {
				output = true;
			} else {
				output = false;
			}

		}
		System.out.println("Status is " + output);
	}
}

RemovexAndAppendAtTheEnd

/**
 * Input--String input1="xaXafxsd"
 * String output1="aXafsdxx"
 * operation-- remove the character "x"(only lower case) from string and place at the end 
 */



import java.util.Scanner;

public class RemovexAndAppendAtTheEnd {
	public static void main(String args[]) {
		Scanner in = new Scanner(System.in);
		System.out.println("Enter the Input String : ");
		String input = in.next();
		String output = "";
		int count = 0;
		char c;

		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < input.length(); i++) {
			c = input.charAt(i);
			if (c != 'x') {
				sb.append(c);
			} else {
				count++;
			}
		}

		for (int i = 0; i < count; i++) {
			sb.append('x');
		}
		output = sb.toString();

		System.out.println("Output String is " + output);
	}
}

AgeValidation

/**
 * Validate the age
 * String  input="26"
 * boolean output
 * --1st character in input1 should be digit
 * --input should be greater than zero
 * --age should be between 21 and 45 (inclusive 21 and 45)
 */

import java.util.Scanner;

public class AgeValidation {
	public static void main(String args[]) {
		Scanner in = new Scanner(System.in);
		System.out.println("Enter the String : ");
		String input = in.next();

		boolean output = false;

		if (Character.isDigit(input.charAt(0))) {
			int age = Integer.parseInt(input);
			if (age >= 21 && age <= 45) {
				output = true;
			}
		}
		System.out.println("Age Status is " + output);
	}
}

							
							

CompareValuesInHashMap

/**
 * HashMap h1={“abc”:50,”efg”:70};
 * if the mark is less than 60 then put the output is the
 * HashMap h2={“abc”:”fail”,”efg”:”pass”} * 
 */



import java.util.HashMap;
import java.util.Map;

public class CompareValuesInHashMap {
	public static void main(String[] args) {
		Map map1 = new HashMap();
		map1.put("abc", 50);
		map1.put("efg", 70);
		System.out.println(map1);

		Map map2 = new HashMap();

		for (String i : map1.keySet()) {
			if (map1.get(i) > 60) {
				map2.put(i, "pass");
			} else {
				map2.put(i, "fail");
			}
		}

		System.out.println(map2);

	}
}

NumberOfCharactersAndDigitPresence

/**
 * Check if the No of characters in a string greater than 3 and if there are digits output=false;
 * check  if length of word is greater than 3 and there are no digits then = true 
 */

import java.util.Scanner;

public class NumberOfCharactersAndDigitPresence {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		System.out.println("Enter the String : ");
		String input = in.next();

		boolean output = false;
		int count = 0;
		if (input.length() > 3) {
			for (int i = 0; i < input.length(); i++) {
				if (Character.isDigit(input.charAt(i))) {
					count++;
				}
			}
			if (count == 0) {
				output = true;
			} else {
				output = false;
			}
		}

		System.out.println("Output Status is " + output);
	}
}

ArrayContains50OrNot

/**
 * If given array contains 9 then O/p is true;
 * 
 */

package programs;

import java.util.Scanner;

public class ArrayContains50OrNot {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		System.out.println("Enter the Array Size : ");
		int size = in.nextInt();
		int[] input = new int[size];
		boolean output = false;

		System.out.println("Enter Array Values : ");
		for (int i = 0; i < input.length; i++) {
			input[i] = in.nextInt();
		}

		for (int i = 0; i < input.length; i++) {
			if (input[i] == 9) {
				output = true;
			}
		}
		System.out.println("Output Status is " + output);

	}

}