0% found this document useful (0 votes)
4 views2 pages

Java Examples

Uploaded by

jz0wv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Java Examples

Uploaded by

jz0wv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#######

public void reverseNumber(int number){


int reverseNum = 0;
while(number > 0){
int temp= number%10;
reverseNum =(reverseNum*10)+temp;
number = number/10;
}
System.out.println(reverseNum);
}
#######
public void reverseString(String str){
StringBuffer sb= new StringBuffer();

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


//System.out.print(str.charAt(i));
sb.append(str.charAt(i));
}
System.out.println(sb);
}
#######
public void reverseStringWithoutMethod(String str){
char[] strChar=str.toCharArray();
int top=strChar.length-1;

for(int i=0;i<=top;i++){
char temp = strChar[i];
strChar[i]=strChar[top];
strChar[top]= temp;
top--;
}
System.out.println(strChar);
}

########
//* function to check whether two strings are
anagram of each other */
static boolean areAnagram(char[] str1, char[] str2)
{
// Get lengths of both strings
int n1 = str1.length;
int n2 = str2.length;

// If length of both strings is not same,


// then they cannot be anagram
if (n1 != n2)
return false;

// Sort both strings


Arrays.sort(str1);
Arrays.sort(str2);

// Compare sorted strings


for (int i = 0; i < n1; i++)
if (str1[i] != str2[i])
return false;

return true;
}
#############
//Roman to int (ix -> 9)
public int romanToInt(String s) {
int ans =0, num = 0;
for(int p = s.length()-1; p>=0; p--){
switch(s.charAt(p)){
case 'I': num = 1; break;
case 'V': num = 5; break;
case 'X': num = 10; break;
case 'L': num = 50; break;
case 'C': num = 100; break;
case 'D': num = 500; break;
case 'M': num = 1000; break;
}

if(4 * num < ans){


ans-=num;
}else{
ans+=num;
}
}
return ans;

You might also like