Showing posts with label String. Show all posts
Showing posts with label String. Show all posts

Sunday, June 7, 2015

Java : Program to find the minimum and maximum number in a given array.


/**
* Find the minimum and maximum number in a given array.
*
* Approach: we can pick an element from array and compare with the min and max value
*
*
*/

public class MinMaxNumber {

public static void main(String[] args) {

int arr[] = {1100,10,3,5,8,1,5,9,23,56,0,90,54};
int min,max;
min=max= arr[0];
for(int i =1 ; i<arr.length;i++){

if (min > arr[i]){ // comparing the number for min value
min= arr[i];
}

if(max < arr[i]){ // comparing the number for max value
max=arr[i];
}
}
System.out.println("Min value = " + min);
System.out.println("Max value = " + max);

}

}

/* output
Min value = 0
Max value = 1100

Tuesday, June 2, 2015

Java : Program to swap two number without using any arithmetic operator


import java.util.Scanner;

/*
*
* Swap two number without using any arithmetic operator like + , - etc
 * we can achieve the solution using the XOR operator
*/


public class SwapTwoNumberWithoutUsingOperator {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
System.out.println("Enter number A : ");
int a = sc.nextInt();
System.out.println("Enter number B : ");
int b = sc.nextInt();


System.out.println("Number before swapped A and B respectively --> " + a +" " + b);

a = a^b;
b = a^b;
a = a^b;

System.out.println("Number after swapped A and B respectively --> " + a +" " + b);


}

}

/* output
Enter number A :
10
Enter number B :
30
Number before swapped A and B respectively --> 10 30
Number after swapped A and B respectively --> 30 10

*/

Java : Program to find the sum of each row and each column of n x m of 2D Matrix


package jun2;

/*
* Find the sum of each row and each column of n x m of 2D Matrix
*
*/

public class SumOf2DMatrixRowsAndColumn {


public static void main(String[] args) {

int [][] twoDMatrix = {{ 20, 18, 23, 20, 16 },
{ 30, 20, 18, 21, 20 },
{ 16, 19, 16, 53, 24 },
{ 25, 24, 22, 24, 25 }
};
outputArray(twoDMatrix);
}

public static void outputArray(int[][] array) {
int sum= 0;
int rowSize = array.length;

int[] colSum =new int[array[0].length];
for (int i = 0; i < array.length; i++){
for (int j = 0; j < array[i].length; j++){
sum += array[i][j];
colSum[j] += array[i][j];
}
System.out.println("sum of rows "+ i +" = " + sum);
}
System.out.println(" ");
for(int k=0;k<colSum.length;k++){
System.out.println("sum of columns "+ k +" = " + colSum[k]);
}


}

}

/*output:

sum of rows 0 = 97
sum of rows 1 = 206
sum of rows 2 = 334
sum of rows 3 = 454

sum of columns 0 = 91
sum of columns 1 = 81
sum of columns 2 = 79
sum of columns 3 = 118
sum of columns 4 = 85

*/

Thursday, May 14, 2015

Java : Program to find a occurrence/count of a particular pattern in a given string


/*
* Find occurrence/count of a particular pattern in a given string .
* For example : you have to search for a pattern ca#e or ca$e or ca&e or ca1e
* where 3 character could be any thing but first 2 chars and last
* should contain the c ,a and e respectively.
*
* Input = "carehappyca#eforyouca$ecccaseeeeca@ca$e"
* output = 5
*
* Condition : You don't have to use any inbuilt function like split() or regex() function etc.
*
*/

public class CountPatternInStringSequence {


public int countPatternInStringSequence(String str){

char[] ch = str.toCharArray(); // convert the given string to character array;
int i = 0;
int count = 0;

while(i < ch.length){

if(ch[i] == 'c'){
i++;
if(ch[i] == 'a'){
i= i+2;
if(ch[i] == 'e'){
count++;
}else{
i = i-2;
}
}else{

i = i-1;
}
}

i = i +1;

}
return count;
}

public static void main(String[] args) {

CountPatternInStringSequence fs = new CountPatternInStringSequence();
String str = "carehappyca#eforyouca$ecccaseeeeca@ca$e";
int totalCount = fs.countPatternInStringSequence(str);
System.out.println("Given String : " + str);
System.out.println("Total occurence of particular pattern found : " + totalCount);

}

}

/*
output:

Given String : carehappyca#eforyouca$ecccaseeeeca@ca$e
Total occurence of particular pattern found : 5

*/

Thursday, January 15, 2015

Java : A string consists of parenthesis and letters. Write a program to validate all the parenthesis.


import java.util.*;
/*
*
* A string consists of parenthesis and letters. Write a program to validate all the parenthesis.
* Ignore the letters.eg.
* ((alf)ls) – valid
* )(dkk)() – invalid
*/

public class StringI_1 {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
System.out.print("Enter string :- ");
String str = sc.nextLine();

System.out.println("Entered string is :- " + str);

int countOpen = 0 ;
int countClose =0 ;
int sizeOfString = str.length();

if(str == null || sizeOfString == 0){
System.out.println("String is Empty");
System.exit(0);
}

if (str.charAt(0) != '(' && str.charAt(sizeOfString - 1) != ')' ){
System.out.println("First : Invalid String");
System.exit(0);

}
/*else
System.out.println("valid");*/

for(int i = 0 ; i< sizeOfString ; i++){

if(str.charAt(i) == '(' && i!=(sizeOfString-1) ){
if( str.charAt(i+1) == ')' ){
System.out.println("Second :Invalid String ");
System.exit(0);
}else{
countOpen++;
System.out.println("countOpen :- " + countOpen +" "+ str.charAt(i));
}
}else {
if(str.charAt(i) == ')' ){ //&& str.charAt(i+1) !=')'
countClose++;
System.out.println("countClose :- " + countClose +" "+ str.charAt(i));
}
}
}
if(countOpen == countClose){
System.out.println("Valid string");
}else{
System.out.println("Third : Invalid String");
}
}

}

Saturday, August 23, 2014

Java : Program to reversing a string


public class ReverseString {

/**
* @param args
*/
public static void main(String[] args) {

//String[] name = new String[10];
String name , rev = "";

Scanner in = new Scanner(System.in);
System.out.println("Enter the string for reversal :- ");
name = in.nextLine();

int len = name.length();

for(int i = len-1;i>=0;i--){
rev = rev + name.charAt(i);
}
System.out.println(rev);
}


}

Java : Program swapping two numbers without using third variable


public class SwapTwoNo {

/**
* @param args
*/
public static void swap(int a , int b){

a = b+a;
b = a-b;
a = a-b;

System.out.println("Values of a and b after swap := " + a +" "+ b + " respectively");
}


public static void main(String[] args) {

int a = 5;
int b = 6;
System.out.println("Values of a and b before swap := " + a +" "+ b + " respectively");
swap(a,b);

}
}

Testing a New Product: Essential Information Software Testers Must Gather in the AI Era

    Learn what information software testers must gather before starting testing a new product, including AI, automation, compatibility, and ...