Thursday, 30 April 2015

Program to rotate a 2-D square matrix by 90 degree

say you have a 4x4 two dimensional array, 
write a function that rotates it 90 degrees 
[1][2][3][4]
[5][6][7][8]
[9][0][1][2]
[3][4][5][6]
Becomes:
[3][9][5][1]
[4][0][6][2]
[5][1][7][3]
[6][2][8][4] 
 
This is working code in C, which accepts the size 
of square matrix and matrix elements and prints
the rotated matrix.
#include<stdio.h>
#include<stdlib.h>

int main()
{
 int s, **arr, **arr2;
 int i, j, c, r;
 printf("\n Enter the size of square matrix : ");
 scanf("%d",&s);
 arr=(int *)malloc(s*sizeof(int));
 arr2=(int *)malloc(s*sizeof(int));
 printf("\n Enter the array elements :");
 for(i=0;i<s;i++)
 {
  arr[i]=(int *)malloc(s*sizeof(int));
  arr2[i]=(int *)malloc(s*sizeof(int));
 }
 for(i=0;i<s;i++)
  {
  printf("\n Enter the %d the row : ", i);
  for(j=0;j<s;j++)
   {
    c=s-1-i;
    r=j;
    scanf("%d",&arr[i][j]);
    arr2[r][c]=arr[i][j];
   }
 }

 for(i=0;i<s;i++)
 {
  printf("\n");
  for(j=0;j<s;j++)
  {
   printf("%d ", arr2[i][j]);
  }
 }
} 
 

Monday, 27 April 2015

JAVA : Handling Variable Arguments

Varargs was added in Java 5 and the syntax includes three dots … (also called ellipses).
Following is a program showing how to handle Variable arguments in Java :
public class testingVarargs {
public static void main(String args[]) {
   testFunction(1234, "India", "Delhi");
   testFunction(2345, "United States", "Mumbai", "kolkata");
    } 
public static void testFunction(int some, String... args) {
   System.out.print("\n" + some);
   for(String arg: args) {
    System.out.print(", " + arg);
  }
 }
}

Tuesday, 21 April 2015

Program to Upload files to Server using JSP & Servlets

This is a program to accept the file from user using JSP and then  uploads the file into the target server. It uses the jsch api.

You need to download the JSch api and include in the jars.

Upload.jsp :

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<center>
<H1> APPLICATION TO PUSH FILES [ SAMS ]</H1>
        <form method="post" action="singleFileUpload" enctype="multipart/form-data">
          <br>  
            Select file to upload:
            <input type="file" name="uploadFile" />
            <br/><br/>
            <input type="submit" value="Upload" />
        </form>
    </center>
</body>
</html>

singleFileUpload.java

import java.io.BufferedReader;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.File;
import java.io.IOException;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FilenameUtils;


/**
 * Servlet implementation class singleFileUpload
 */
@WebServlet("/singleFileUpload")
public class singleFileUpload extends HttpServlet {
private static final long serialVersionUID = 1L;
 
    public singleFileUpload() {
        super();
       }

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String user = "user";
   String password = "xyz123";
   String host = "xx.yy.zz.qq";
   int port=22;
   String targetDir = "/user/doc";
   String lfile = null;
   String fileName = null;

        if(ServletFileUpload.isMultipartContent(request))
        {
            try {
 List<FileItem> multiparts = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
           
               for(FileItem item : multiparts)
               {
                   if(!item.isFormField())
                   {
lfile = new File(item.getName()).getName();
                      
                        System.out.println("file to upload : " + lfile);
                      
                        String fieldName = item.getFieldName();
                        fileName = FilenameUtils.getName(item.getName());
                        InputStream fileContent = item.getInputStream();
                        System.out.println("File Content : " + fileContent.toString());
                        System.out.println("files to be uploaded : " + fileName);
                    
                        String fileNameDest=targetDir+"/"+fileName;
                        System.out.println(fileNameDest);
                        try
                     {
                         JSch jsch = new JSch();
                         Session session = jsch.getSession(user, host, port);
                         session.setPassword(password);
                         session.setConfig("StrictHostKeyChecking", "no");
                         System.out.println("Establishing Connection...");
                         session.connect();
                         System.out.println("Connection established.");
                         System.out.println("Crating SFTP Channel.");
                         ChannelSftp sftpChannel = (ChannelSftp) session.openChannel("sftp");
                         sftpChannel.connect();
                         System.out.println("SFTP Channel created.");
                       
                         sftpChannel.put(fileContent, fileNameDest);
                        
                       
                         sftpChannel.disconnect();
                         System.out.println("Disconnecting the sftp Chnannel");
                       
                         session.disconnect();
                         System.out.println("Discontinuing the session");
                   
                   }
                                     
                   catch( Exception e)
                   {
                   System.out.println(e);
                   }
               
                    }
                     
               
                }
             
            }
            catch (FileUploadException e)
            {
e.printStackTrace();
}        
        }
   }

}

All the best !!!

Saturday, 11 January 2014

Passwordless Connection to a Remote Server


Passwordless connection to a remote server can be done in TWO steps :


STEP #1 : Setup the Public key and send it to the server that is to be connected

i) cd ~/.ssh : ssh contains all the private and public keys
ii) ssh -keygen -t rsa [ do not enter password when asked ] : This will create two keys (a) private key (b)public key (.pub extension)
iii) scp ~/.ssh/id_rsa.pub [user]@[server]:~/ : This step will copy the public key to the target server

STEP #2 : Changing the permission of the copied key and storing it in authorized_keys folder

i) open a terminal on [target server] and mkdir ~/.ssh
ii) chmod 700 ~/.ssh
iii) touch ~/.ssh/authorized_keys
iv) cat ~/id_rsa.pub >> ~/.ssh/authorized_keys
v) chmod 600 ~/.ssh/authorized_keys
vi) rm ~/id_rsa.pub
Now you can connect with the target server with command

ssh ip_adddess[xx.yy.zz.pp]

: This will take you to the target server without prompting for the password.

Friday, 21 December 2012

How to install wordpress on server machines ?

Installing the wordpress is the famed 5 minutes process .
You just have to download the wordpress from the wordpress official site.
(wordpress.org/download/)
 This is the zip file,now extract its content.
On the server machine create a database with the user and password for the wordpress.
Upload the wordpress extracted folder content to the html public folder of the server machine
Now change the name of the file wp-config-sample.php with wp-config.php
and make the entry of database, user and password in the wp-config.php.
In some machines this may not work, in such cases, these setting can be done directly through the wordpress.
After loading the contents of the extracted wordpress folder to the server machine, open the browser and enter the url of the site. There will come the wordpress starting page. From here you can follow the link to update database, user and password.
Then you can set the wordpress admin account and password.

This is the famous 5 mins installation of wordpress.

For further clarification you can visit: codex.wordpress.org/Installing_WordPress

Wednesday, 12 December 2012

virtues or qualities of a programmer..

When coming to the features or traits that make a good programmer, one can't come to the fixed or well defined features or virtues that good programmers should have.
According to me what the most important thing a programmer should have is he should be lazy. The word lazy symbolizes the quality of a programmer because a lazy person will look for the easiest or efficient method to solve the problem.
The next in line comes learn and unlearn capability as in IT world the life span of a technology is very short and one has to learn the next big tech to stay afloat in the trade.
The third quality a programmer should have is  never change attitude .... This is the most required quality a programmer should possesses as unless he can't question the changes in the technology he can't understand it. If you want to be good programmer then you should start asking questions about why this why can't that? and so on.

The above three quality combined with the eager to succeed in the programming field will make  a quality programmer..

Best of luck for your programming journey..

Friday, 14 September 2012

how to check whether your ISP(Internet Service Provider) support PHP or not?

This article is a simple illustration to check whether your site hosting server
supports PHP or not ?

Steps :
1 Create a checkphp.php file
  
     <?php
        phpinfo();
           ?>
2   upload this file in your web hosting directory and run the page through                 browser
3  If your ISP supports PHP then it will show php configurations and other stuff
   

 go 'n' test the code ...

best of luck