Mar 31, 2012

Reverse the words of a string

Given a string My name is Antonio Gonsalves. You have to reverse all the letters of the words in the string so that the resultant string looks like yM eman si oinotnA sevlasnoG

int main()
{
    char str[] = "My name is Antonio Gonsalves", 
         *startPtr = str,
         *endPtr = str,
         *spacePtr;

    while(*spacePtr != '\0')
    {
        while(*endPtr != ' ' && *endPtr != '\0')
            ++endPtr;

        spacePtr = endPtr;
        endPtr = spacePtr - 1;
       
        char temp;
        while(startPtr < endPtr)
        {
            temp = *endPtr;
            *endPtr-- = *startPtr;
            *startPtr++ = temp;
        }
        
        startPtr = spacePtr + 1;
        endPtr = spacePtr + 1;
    }
    printf("%s\n", str);
    return 0;
}
$ ./a.out 
yM eman si oinotnA sevlasnoG

Read more ...

Difference between a char [] and char *

There is an important difference between the following two definitions:

char amessage[] = "Hello World"; /* an array */
char *pmessage  = "Hello World"; /* a pointer */

1. amessage is just an array, big enough to hold the sequence of characters and '\0'
2. amessage refers to the same memory location and cannot be changed
3. Individual characters within amessage can be changed
#include <stdio.h>
int main()
{
    char amessage[] = "Hello World from the C Program";

    amessage[0] = 'P';
    printf("%s\n", amessage);
    return 0;
}

1. pmessage is a pointer pointing to a string constant
2. The string constant "Hello World" is stored in a read only memory location and cannot be modified
3. pmessage can be changed to point to some other memory location
#include <stdio.h>
int main()
{
    char *pmessage = "Hello World from the C Program";

    pmessage[0] = 'P'; //throws segmentation fault
    printf("%s\n", pmessage);
    return 0;
}

Read more ...

Mar 25, 2012

Vertical Sum of a Binary Tree

The binary tree above can be represented as the following to calculate the vertical sum of the nodes

Read more ...

Mar 24, 2012

Remove a node from Binary Search Tree

C++
A node can be removed from a Binary Search Tree using two approaches.
1. Double Pointer Approach
2. Single Pointer Approach

Read more ...

Insert a node in Binary Search Tree

C++
A node can be inserted in a binary search tree using two approaches
Double pointer approach
void insert(BinaryTreeNode **node, int data)
   {
      if(*node == NULL)
      {
        *node = getNewNode(data);
      }
      
      if(data == (*node)->data)
      {
        //do  nothing
      }
      else if(data < (*node)->data)
      {
        insert(&(*node)->left, data);
      }
      else
      {
        insert(&(*node)->right, data);
      }
   }

Single pointer approach
BinaryTreeNode* insert(BinaryTreeNode *node, int data)
    {
      if(node == NULL)
      {
        node = getNewNode(data);
        return node;
      }
      
      if(data == node->data)
      {
        //do  nothing
      }
      else if(data < node->data)
      {
        node->left = insert(node->left, data);
      }
      else
      {
        node->right = insert(node->right, data);
      }

      return node;
   }

Read more ...

Mar 23, 2012

Configuring CMIS Consumer in Sharepoint 2010 server

In our previous post, we have configured SharePoint 2010 Server with CMIS Connector Services Producer. This post talks about adding and configuring CMIS Connector Services Consumer Web Part to a Web Page. We will also be testing the CMIS Consumer service by listing down all the documents uploaded to a particular sharepoint document repository.

Read more ...

Configuring CMIS in Sharepoint 2010 server

Content Management Interoperability Services a.k.a CMIS is a standard defined for Enterprise Content Management (ECM) systems such as SharePoint server, Documentum, Alfresco and others. The standard defines a domain model plus Web Services and Restful AtomPub bindings that can be used by other applications.

Installing the SharePoint CMIS Connector
1. Install Microsoft SharePoint Server 2010

2. Complete the SharePoint 2010 Products configuration wizard
3. Download Microsoft SharePoint 2010 Administration Toolkit from here and save the file to hard disk
SharePoint 2010 Administration Toolkit installs two components:
CMIS Producer Services
This allows CMIS client applications to interact with the SharePoint document libraries by using interfaces defined in the CMIS standard

CMIS Consumer Services Web Part
The Consumer Web part can be added to any SharePoint page and allows users to connect with any CMIS compliant repository
4. Double click SharePoint2010AdministrationToolkit.exe file to start the installation.
5. Accept the license agreement and click next. Make sure CMIS connectors are selected for the install. Click Next
6. Use the default installation directory. Click Next and Finish. CMIS Connector is successfully installed


Read more ...

Mar 17, 2012

Configuring password less access to remote systems using ssh-keygen utility

When we say password less authentication, we mean that the client authentication is carried out using public and private keys.

Generate public/private key pair
The ssh-keygen utility is used to generate the public/private key pair. The keys generated are stored under .ssh directory in the user home directory. The private key is never shared and stored in the local machine whereas the public key is distributed to the machines you want to login to.

Read more ...

Mar 16, 2012

Installing And Configuring SSH server on Windows

We will be making use of Cygwin utilities to configure and run ssh as service on windows machine. Installing Cygwin on a windows machine is pretty straight forward. Download the latest Cygwin installation setup.exe from the Cygwin site and follow the below instructions.

1. Installing Cygwin
Step 1. Double click setup.exe


Step 2. Choose the download source

Step 3. Select the root directory of the Cygwin. This directory is synonymous to / in linux

Step 4. Select the directory where you want to keep the installation files. You can save this directory and use at a later point to install Cygwin on any windows machine using this directory.

Click 'OK' if prompted to create the directory if it does not exists

Step 5. Select the type of connection you are using to connect to internet.

Step 6. Choose a download site.

Step 7. Clicking next opens up the 'Select Packages' screen.

Step 8. Select the Open SSH server and client programs from the 'Select Packages' screen.

Step 9. Click next to start the installation.

This will install the following utilities in your Cygwin's /usr/bin directory
ssh-add.exe
ssh-agent.exe
ssh-host-config
ssh-keygen.exe
ssh-keyscan.exe
ssh-user-config
ssh.exe

2. Configuring ssh as Windows service

Run ssh-host-config utility to configure sshd server on windows. Select 'no' when prompted for 'Should privilege separation be used? (yes/no)'. Select 'yes' when prompted for 'Do you want to install sshd as service?'. Choose default options for other options.
The above will install CYGWIN sshd service on Windows. To start the service execute
net start sshd

3. Connecting using Cygwin ssh client (ssh.exe)
ssh.exe user@ssh-server

4. Connecting through putty

Add the server's host key to registry. This will add an entry into the ~/.ssh/known_hosts file

Login using windows user and password

Read more ...