Wednesday, October 28, 2015

Reverse String in Java (Without using StringBuffer reverse() function)

Here is the code to print the reverse of the entered string in Java without using the predefined function of StringBuffer class. The Output is shown below and followed by the code:




import java.util.Scanner;
public class ReverseString {
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Program to Print Reverse of a string without using any API");
        System.out.println("----SPACES NOT ALLOWED----");
        System.out.print("Enter the String:- "); //taking string from user
        String s = sc.next();
        int i,j,k;
        int n = s.length();
        char[] c = new char[n]; //converting into character array
        c = s.toCharArray();
        char[] c1 = new char[n];
        //traversing from back to front of string and assigning it to new array
        for(i=n-1,j=0;i>=0;j++,i--) 
        {
            c1[j] = c[i];
        }
        String rev = new String(c1);
        System.out.println("The Reverse of String is:- \t"+rev); //Printing the reverse String
    }
}

Saturday, April 25, 2015

Saturday, April 18, 2015

Replacing Consonants

Code for Replacing Consonant With the Next Consonant in the Provided String.


import java.util.Scanner; //Scanner Class
public class StringMapOne{
    static String alterString(String s) //Method to Alter the String
    {
        int n = s.length();
        char[] ar = new char[n];
        ar = s.toCharArray();
        for(int i=0;i<n;i++)
        {
            switch(ar[i])
            {
                case 'b':
                case 'c':
                case 'd':
                case 'f':
                case 'g':
                case 'h':
                case 'j':
                case 'k':
                case 'l':
                case 'm':
                case 'n':
                case 'p':
                case 'q':
                case 'r':
                case 's':
                case 't':
                case 'v':
                case 'w':
                case 'x':
                case 'y':
                case 'z': int c = (int)ar[i]; //Printing the Next Letter to Consonants
                          c = c+1;
                          char d = (char)c;
                          ar[i] = d;
                          break;
                default : ar[i] = ar[i];
                          break;
            }
        }
        String obj = new String(ar);
        return obj.toUpperCase(); //Printing in Upper Case
    }
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Program to Perform Opeation on String");
        System.out.print("Enter the String:- \t");
        String z = sc.next().toLowerCase();
        String b = alterString(z); //Calling the Function
        System.out.println("The Output is:- \t"+b);
    }
}

Positive Strings

Code to Check Whether the Entered String is Positive or Not.

A string is considered a positive string, if on moving from left to right each character in the String comes after the previous characters in the Alphabetical order.  For Example  ANT is a positive String (Since T comes after N and N comes after A)  APPLE is not positive since L comes before P in the alphabetical order.






Dupledrome Checking


Code to Check Whether the entered String is Dupledrome or Not.

Dupledrome means that every element present in the String should be present twice i.e., "rattar" is a dupledrome string whereas "rasrat" is not a Dupledrome String.








import java.util.Scanner; //Scanner Class
public class DupledromeChecking {
    static boolean isDupledrome(String ap)
    {
        int N = ap.length();
        char[] ar = new char[N]; //Char Array
        ar = ap.toCharArray();
        int i,k=0,q=0,r=0,j;
        for(i=0;i<N;i++)
        {
            for(j=0;j<N;j++)
            {
                if(ar[i]==ar[j]) //When Values are Equal
                {
                    r++; //Increment The Count
                }
            }
        }
        /*If The Counted Value is Equal to Twice the Lenght 
        then it is Said that every element present 
        exactly twice */
        if(r==(N*2))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in); //Scanner Object
        System.out.println("Program to Check Whether the Entered String is Dupledrome or Not");
        System.out.print("Enter the String:- \t");
        String s1 = sc.next();
        boolean result = isDupledrome(s1); //Calling Function
        System.out.println("Dupledrome String:- \t"+result);
    }
}


Display Strings in Between * Lines

Here is the code to display the string in the * column using loop for any string provided by user.



import java.util.Scanner; //Scanner Class
public class StringDisplay {
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the String:- \t");
        String s = sc.next(); //Getting String Input from user
        int n = s.length(); //Finding Length of String
        for(int i=1;i<n+5;i++) //Running Loop
        {
            System.out.print("*");
        }
        System.out.println();
        System.out.println("**"+s+"**");
        for(int i=1;i<n+5;i++)
        {
            System.out.print("*");
        }
        System.out.println();
    }
}