JAVA CLASS
1.REVERSE A STRING
public class demo1 {
public static void main(String args[]) {
demo1 d = new demo1();
System.out.println(d.rev("hello"));
}
public String rev(String s ) {
if(s == null || s.length()<1) return "null";
String s1 ="";
for(int i = s.length()-1; i>=0 ; i--)
s1=s1+s.charAt(i);
return s1;
2.consider only the alphabets and give the number of vowels and consonants present in the given string
no special characters should be considered
public class demo1 {
public static void main(String args[]) {
demo1 d = new demo1();
d.num("he#$^^$4llo");
}
public void num(String s ) {
if(s==null) {
System.out.println("no of vowels = 0 and consonents =0");
return; // comes out of the method
}
int v = 0;
int c=0;
// convert it to lower case to make it easy for coading
s = s.toLowerCase();
for(int i = s.length()-1; i>=0 ; i--) {
char ch = s.charAt(i); // directly storing in the char
if(s.charAt(i)=='a' ||s.charAt(i)=='e'||s.charAt(i)=='i'||
s.charAt(i)=='o'||s.charAt(i)=='u') {
v++;
// if vowel executes and leaves
}
else if(ch>='a' && ch <= 'z') c++; // no need of
giving the conditon again for vowels
System.out.println( v + " " + c );
3.to remove the bad / banned / prohibited words
public class demo1 {
public static void main(String args[]) {
demo1 d = new demo1();
// System.out.println(d.num(" hydrabad"));
System.out.println(d.b(" hydrabad "));
}
// wihout array
public boolean num(String s ) {
// array of banned words
if (s.contains("hatefull") || s.contains("bad") ||
s.contains("ugly")) {
return true;
}
else return false;
}
// using array
public boolean b(String s) {
if (s==null) return false;
String[] w = { "bad" , "ugly" , "hate"};
s=s.toLowerCase();
for(String i : w) {
if(s.contains(i)) return true;
}
return false;
}
}
4.palindrome of a string
public class demo1 {
public static void main(String args[]) {
demo1 d = new demo1();
System.out.println(d.palindrome("ab c cba"));
public String palindrome(String s ) {
if(s==null) return "null";
s=s.toLowerCase();
//cleaned string
String str = "";
int l=str.length()
; for(int i=0; i<s.length(); i++) {
if(s.charAt(i)>='a' && s.charAt(i)<='z') {
str=str+s.charAt(i);
}}
System.out.println(str);
String res ="";
for(int i = str.length()-1 ; i>=0 ; i--) {
res=res+str.charAt(i); }
System.out.println(res);
if(str.equals(res)) return "palindrome" ;
else return "not a palindrome:";
}}
5.allows only numbers in the user input
public class demo1 {
public static void main(String args[]) {
demo1 d = new demo1();
System.out.println(d.otp("123"));
}
public boolean otp(String s) {
if(s==null || s.length()<1 )return false;
boolean flag=false;
for(int i =0 ; i<s.length() ; i++) {
if(s.charAt(i)>=48 && s.charAt(i)<=57) {
flag=true;
}
else {
flag=false;
break;
}
}
return flag;
}}
Method 2
public class demo1 {
public static void main(String args[]) {
demo1 d = new demo1();
System.out.println(d.otp("123"));
}
public boolean otp(String s) {
if( s == null || s.length()<1) return false;
for(int i =0 ; i<s.length(); i++) {
//give it in single quotes to represent the character or
else you will get wrong output
if(s.charAt(i)<'0' || s.charAt(i) >'9') return false;
return true;
}}
*************************************************************************************
*************************************************************************************