Java Programs for SDET Interview

 

Reverse a string in java 


public class ReverseString {

public static void main(String[] args) {

String value = "selenium";

String newvalue ="";

//convert string to character

char val[]= value.toCharArray();

for(int i = val.length-1; i>=0;i--)

{

System.out.print(val[i]);

// convert character to string again

newvalue = newvalue.concat(Character.toString(val[i]));

}

System.out.println("");

System.out.println(newvalue);

}

}


Get unique charcters from a string in java 

public static void main(String args[])
{
String value = "selenium";

//get the lenght of the string 

int stringlength = value.length();

//Create a set of characters

HashSet<Character> set=new HashSet();  

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

// convert string to array of characters and add in set interface

set.add(value.toCharArray()[i]);

}
System.out.println(set);
}


To get the count of charcters from a string in java 


public static void main(String[] args) {
String temp = "interview prepration for sdet";

// array of index

int[] index = new int[temp.length()];  

// declare two variable for index values and count

int i, j;

// array of characters

char characterarray[] = temp.toCharArray();  
for( i =0; i<temp.length();i++)
{

// store index values for the characters

index[i] =1;

for ( j =i+1 ;j<temp.length(); j++)

{
if(characterarray[i]==characterarray[j])

{

index[i]++;

characterarray[j] = '0'; 
}
}
 
}

System.out.println("Characters and their corresponding frequencies");  

for(i = 0; i <index.length; i++) {  

        if(characterarray[i] != ' ' && characterarray[i] != '0')  

        System.out.println(characterarray[i] + "-" + index[i]); 

}
}

Post a Comment