Skip to content

Commit

Permalink
change
Browse files Browse the repository at this point in the history
  • Loading branch information
varun06 committed Mar 26, 2012
1 parent b8aa214 commit 21fb359
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 0 deletions.
Binary file added Enigma.class
Binary file not shown.
14 changes: 14 additions & 0 deletions Enigma.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
public class Enigma{
public static void enigma(int x){
if(x == 0){
return;
}else{
enigma(x/2); //recursively calling enigma
}
System.out.print(x%2);
}
public static void main(String[] args) {
enigma(5);
System.out.println("");
}
}
Binary file added Palindrome.class
Binary file not shown.
61 changes: 61 additions & 0 deletions Palindrome.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import java.util.Scanner;

public class Palindrome{
public static char first(String s){
return s.charAt(0);
}

public static char last(String s){
int length = s.length();
return s.charAt(length - 1);
}

public static String middle(String s){
int length = s.length();
return s.substring(1,length - 1);
}

public static boolean isPalindrome(String s)
{
if(s.length() == 0 || s.length() == 1)
return true;//if length =0 OR 1 then it is
if(first(s) == last(s))//check for first and last char of String, if they are same then do the same thing for a substring with first and last char removed. and carry on this until you string completes or condition fails
return isPalindrome(middle(s)); //if its not the case than string is not.
return false;
}

public static boolean isPalindromeIter(String s){
int length = s.length(); //get the length of the string
int mid = length/2; // get the middle point of the string
if(length == 0 || length == 1) //return if the string is empty or 1 chracter
return true;
for(int i = 0; i < mid; i++){
if (s.charAt(i) != s.charAt(length-i-1)){ //iterate and return if the characters are not matching
return false;
}
}
return true;
}

public static boolean isPalindromewhile(String s) {
int left = 0; // index of leftmost unchecked char
int right = s.length() -1; // index of the rightmost

while (left < right) { // continue until they reach center
if (s.charAt(left) != s.charAt(right)) {
return false; // if chars are different, finished
}
left++; // move left index toward the center
right--; // move right index toward the center
}
return true; // if finished, all chars were same
}

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);
String word = scan.nextLine();
System.out.println(isPalindromeIter("word"));
System.out.println(isPalindromewhile("word"));
}
}
Binary file added abecedarian.class
Binary file not shown.
32 changes: 32 additions & 0 deletions abecedarian.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
public class abecedarian {
public static boolean isAbecedarian(String s) {
int index = 0;
char c = 'a';
while (index < s.length()) {
if (c > s.charAt(index)) {
return false;
}
c = s.charAt(index);
index = index + 1;

}
return true;
}

public static boolean isAbecedarianrec(String s){
char first = s.charAt(0);
char second = first ++;
System.out.println(first);
System.out.println(second);
if (s.charAt(0)<second){
return isAbecedarianrec(s);
}

return false;
}

public static void main(String[] args) {
//System.out.println(isAbecedarian("abcde"));
System.out.println(isAbecedarianrec("abcde"));
}
}
Binary file added reverseDigit.class
Binary file not shown.
11 changes: 11 additions & 0 deletions reverseDigit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class reverseDigit{
public static void main(String[] args) {
reversedigit(81);
}
public static void reversedigit(int n){
int lastDigit = n%10;
int firstDigit = n/10;
System.out.print(lastDigit);
System.out.println(firstDigit);
}
}

0 comments on commit 21fb359

Please sign in to comment.