Sunday, May 4, 2014

Reverse the String in different ways


String is a sequence of  characters. It is a java object to store String type of values.String is a immutable class means if we create once String object with value, cannot be change its value. Any java programmer will attend interview,If interviewer asked 10 questions, in that one question is on String related. String is playing vital role in Java language.Recently one of my friend attend one interview, in that his interviewer asked question about String related.What's that question is "In how many ways we can reverse the String?". As per his java String knowledge he told like this "we can reverse the String in 3 different ways.By using String API we can reverse the String in two ways and without using String API we can reverse the String in two ways".He told like this then interviewer asked him explain that 3 ways .He explain all this 3 ways to interviewer.Then he selected in that interview without asking any further question by interviewer.why i should explain my friends interview process here is, how much Java String playing important role in Interviews.Now i will explain that 3 ways with examples.Please see below

1)Generally we can do reverse of String  by using String API method toCharArray(). we call this method on String object then it will return char Array.
                 String stringVar = "Hai how are you man";
            // Here we calling toCharArray() method on  String object  stringVar then arrayForOriginal           // contains array of characters of that String
                 char arrayForOriginal[] = stringVar.toCharArray();
                 int originalArrLen = arrayForOriginal.length;
                 char arrayForReverse[] = new char[originalArrLen];
                 int reverseIndex = 0;
                 for(int indexVal = originalArrLen - 1; indexVal  >= 0; indexVal--) {
                        arrayForReverse[ reverseIndex] =   arrayForOriginal[indexVal] ;
                         reverseIndex++;
                }
               System.out.println( "Reverse of original String is: "+arrayForReverse);  



Output:


         Reverse of original String is:  nam uoy era woh iah 



2)The second way to reverse the String, we can do this little bit different. Means we can perform this way using charAt() method String API. Here we get each character from String and store that character to Another array.After convert that array  to String.
              String stringVar = "Hai how are you man";
           /** Here we calling toCharArray() method on  String object  stringVar then arrayForOriginal          * contains array of characters of that String
         */
                

 int originalArrLen =  stringVar.length;
             String reverseString = "";
                 int reverseIndex = 0;
                for(int i = originalArrLen - 1;i >= 0;i--){
 reverseString  reverseString+stringVar.charAt(i);
}
               System.out.println( "Reverse of original String is: "+ reverseString);  

Output:
            
           Reverse of original String is:  nam uoy era woh iaH 

3) In this way we can do reverse of String without using String API. Means by using StringBuffer Class.Creating StringBuffer object as passing String value as argument to StringBuffer constructor. After calling reverse method of StringBuffer class on StringBuffer object.How we can do this programmatically, see below

           String originalVal = "What are you doing now";

           StringBuffer stringBufferVal = new StringBuffer(originalVal);
          System.out.prinln(stringBufferVal.reverse());
Output:
            
           Reverse of original String is:  nam uoy era woh iaH



See also this posts:


octal to decimal in java

add two numbers each number has more than 1000 numbers
prime number


Sunday, February 9, 2014

Prime Number


            
The Prime Number term is actually used in mathematics.  Definition of Prime Number is "The number Which is not divisible by any number except itself and 1". For example 2,3,5,7,11,........... Generally we can find a number is prime or not in mathematics but how we can find prime numbers in C,C++,Java,............etc.Now a days in most interviews,interviewer will asking one question that is "Write a program for given number is prime or not?". Recently one of my friend attend C language interview last two days back. He shared his interview experience with me. My friend said to me which type of questions they asked to him, from that discussion i am posting one important question that is "Write a program for given number is Prime or not"?. We can solve this type question in general way but when we attempting interview we can little bit different way. 

Program to find given number is  Prime Number  or not in C Language:
This is a basic program to find given number is prime or not.


#include  <stdio.h>
#include <conio.h>

int main(void){
      int a = 0;//this variable is used to store given number
      int count = 0;//this variable is used to know how many times given number is divisible.
      println("Enter a number: ");
      scanf("%d",&a);//taking entered number to a variable
      
      for(int i = 0;i <= a;i++){
          if(a%i == 0){
               count++;
          }
      }
      if(count == 2){
          printf("Given number %d is Prime Number",a);
     }


Output :

Enter a number : 19

Given number 19 is Prime Number



In another way, we can find the given number is prime or not.In Maths one short cut formula is available.The formula is If you want to find given number is prime or not.
Step 1:
           First we find the square root of given number.
           Ex: Given number is 19 . Square Root of 19 is 4.358898.
Step 2:
           If given number is divisible by less than or equal to 4. Means number is divisible by 1 or 2 or 3 or 4.
Step 3:
           Number is not divisible by 1 or 2 or 3 or 4 numbers.So 19 is Prime Number.



Now we can write the above steps convert to C Language.

#include<stdio.h>

#include<conio.h>
#include<math.h>

int main(void){

   int inputNumber = 0;
   int count = 0;
   printf("Enter a number");
   scanf("%d", &inputNumber);
   
   int  sqrtValue = sqrt(inputNumber);
   for(int i = 2;i <= sqrtValue;i++){
        if(inputNumber%i == 0){
         count++;
        }
   }
   if(count == 1){
     printf("Given number %d is Prime Number",inputNumber);
   }else{
     printf("Given number %d is not a Prime Number",inputNumber);
   }
   return 0;
}


In above program, for loop is started from 2 value not 1 because Every number is divisible by 1.That's why for loop is start from 2.


See this related posts:

write program to sort integer number
How many ways are there to register jdbc driver
Floyd's triangle