Wednesday, March 10, 2010

Sorting Strings

Implement a function which will sort the strings. Donot use any c library functions related to strings.

I have used Bubblesort for my Implementation:
Avinash
#include "stdio.h"
#include "stdlib.h"
void StringBubbleSort(char **aStringNames);
void DisplayArray(char **aArrayPtr);
void InputArray(char **aArrayPtr);
int iTotalNames;
void MyStrCpy(char *Src ,const char *Dest);
int  MyStrcmp(char *string1 , char *string2);
void main()
{
   char SZTemp[50];
   char **aArrayPtr = NULL;
   printf("ENTER THE TOTAL NO OF NAMES TO BE ENTERED:");
   scanf("%d",&iTotalNames);
   char a = getchar();
   aArrayPtr = (char **)malloc(sizeof(int)*iTotalNames);
   InputArray(aArrayPtr);
   StringBubbleSort(aArrayPtr );
   DisplayArray(aArrayPtr);
  
}
  
  
  
  
   void InputArray(char **aArrayPtr)
   {
       int iCount = 0;
      
       for(iCount=0; iCount
            aArrayPtr[iCount] = (char *)malloc(sizeof(char)* 20);
       printf("\nEnter the Names: \n");
       for(iCount=0; iCount
            fgets(aArrayPtr[iCount], 20, stdin);
   }
  
  

   void DisplayArray(char **aArrayPtr)
   {
       int iCount = 0;
       for (iCount=0; iCount
       {
            fputs(aArrayPtr[iCount], stdout);
       }
       printf("\n");
   }
   void StringBubbleSort(char **aStringNames)
   {
      int iPass,iCompare;
      char temp[20];
      for(iPass = 1; iPass
      {
          for(iCompare = 0; iCompare
          {
              if((MyStrcmp(aStringNames[iCompare],aStringNames[iCompare+1])) == 1)
              {
                 MyStrCpy(temp,aStringNames[iCompare+1]);
                 MyStrCpy(aStringNames[iCompare+1],aStringNames[iCompare]);
                 MyStrCpy(aStringNames[iCompare],temp);
              }
          }
      }
   }


  int  MyStrcmp(char *string1 , char *string2)
   {
        while((*string1 == *string2) && (*string1 != NULL))
        {
            string1++;
            string2++;
        }
        if(*string1 == *string2)
            return 0;
        else if(*string1 > *string2)   
           return 1;
        else
           return -1;
  }
       
           
    void MyStrCpy(char *Dest ,const char *Src)
    {
        while(*Src !=0 )
        {
            *Dest = *Src;
            Dest++;
            Src++;
        }
        *Dest = '\0';
    }
 

Monday, March 8, 2010

find duplicate in array

Given an array of size N in which every number is between 1 and N, determine if there are any duplicates in it.

Tuesday, March 2, 2010

DECIMAL TO BINARY

INPUT       : 7
OUTPUT   : 0000000000000111(16bit)
                    0000000000000000000000000000111(32bit)

Poster         :Avinash



Solutioions:


Monday, March 1, 2010

Sorting

WAP to perform ascending order sort without using bubble sort mechanism for sorting!!(i.e; with min overhead!!)



SOLUTIONS:

BY AVINASH

As per my knowledge sorting with less overhead can vary from algorithm and many dependencies on algorith so i am posting Quick sort which is also a sorting algorithm with fastest.

#include"stdio.h"
#include"stdlib.h"
#define MAX 10

void MyQuickSort(int iLow, int iHigh, int aNumbers[]);
int MySortDivision(int iLow, int iHigh, int aNumbers[]);

int main()
{
    int aNumbers[MAX];
    int iCount = 0;
    for(iCount =0; iCount < MAX; iCount++)
        //aNumbers[iCount] = rand()%100;
        scanf("%d",&aNumbers[iCount]);
    printf("NUMBERS BEFORE SORTING:");
    for(iCount =0; iCount < MAX; iCount++)
        printf("%d ",aNumbers[iCount]);
    printf("\n");
    MyQuickSort(0, MAX-1,aNumbers);
    printf("NUMBERS AFTER SORTING:");
    for(iCount =0; iCount < MAX; iCount++)
        printf("%d ",aNumbers[iCount]);
    return 0;
}

void MyQuickSort(int iLow, int iHigh, int aNumbers[])
{
    int iPivotElementIndex = 0;
    if(iLow < iHigh)
    {
        iPivotElementIndex = MySortDivision(iLow, iHigh, aNumbers);
        MyQuickSort(iLow, iPivotElementIndex - 1 ,aNumbers);
        MyQuickSort(iPivotElementIndex + 1, iHigh, aNumbers);
    }
    return;
}


int MySortDivision(int iLow, int iHigh, int aNumbers[])
{
    int iInternalLow = iLow;
    int iInternalHigh = iHigh;
    int iPivot = aNumbers[iLow];
    int iLowValue,iHighValue;
    while(iInternalLow < iInternalHigh)
    {
        iHighValue = aNumbers[iInternalHigh];
        while(iPivot <= iHighValue)//changed
        {
            if(iInternalHigh <= iInternalLow)
                break;
            iInternalHigh--;
            iHighValue = aNumbers[iInternalHigh];
        }
        aNumbers[iInternalLow] = iHighValue;
        iLowValue = aNumbers[iInternalLow];
        while(iPivot > iLowValue)
        {
            if(iInternalLow >= iInternalHigh)
                break;
            iInternalLow++;
            iLowValue = aNumbers[iInternalLow];
        }
        aNumbers[iInternalHigh] = iLowValue;
    }
    aNumbers[iInternalLow] = iPivot;
    return iInternalLow;
}

SIZEOF OPERATOR

CODE FOR USER DEFINED OPERATOR


Solutions :

BY Santosh
#define MYSIZEOP(x) ((char*)(&x+1)-(char*)(&x) )


By Avinash

#define MYSIZEOP(x) ((char*)(&x+1)-(char*)(&x) )
Works only for variables LIMITATION :does not support   For data type alone. like  sizeof(int);


 

PALINDROME

INPUT            : ANY STRING

OUTPUT         : SHOULD CHECK IF IT IS A PALINDROME ARE NOT

         POSTER       :  PUJA
PAlindrome ->string when reverse also provides same kind example :MALAYALAM solutions:

BY SANTOSH

#include "stdafx.h"
#include
#include "stdlib.h"
#include "string"

int CheckPalindrome(char *);

int _tmain(int argc, _TCHAR* argv[])
{
       char *pString = NULL;
      
       bool chkStatus = FALSE;
      
       pString = (char *)malloc(10*sizeof(char));

       if(!pString)
       {
              printf("Memory Allocation Failed\n");
              exit(EXIT_FAILURE);
       }
       printf("Enter the string to be checked:\n");
       scanf("%s",pString);

       chkStatus = CheckPalindrome(pString);

       if(chkStatus)
              printf("The entered string is Palindrome\n");
       else
              printf("The entered string is not Palindrome\n");

       free(pString);

       return 0;
}

int CheckPalindrome(char *pSrc)                        //Function works irrespective of case senstivity
{
       int chkCnt1,chkCnt2,strLen;

       chkCnt1 = chkCnt2 = 0;

       strLen = strlen(pSrc);

       for(chkCnt1 = 0,chkCnt2 = strLen - 1;(chkCnt1!=chkCnt2) && (chkCnt1 < chkCnt2);chkCnt1++,chkCnt2--)
       {
              if((*(pSrc + chkCnt1) != *(pSrc + chkCnt2)) && (*(pSrc + chkCnt1) != *(pSrc +chkCnt2) - 32) && (*(pSrc + chkCnt1) != *(pSrc + chkCnt2) + 32))
                     return FALSE;
       }

       return TRUE;

}

CHARCTEREXTRACTOR

     INPUT          : STRING-> Battle of the Vowels:Hawaii
                         REMOVE-> AEIOU

OUTPUT         : BTTL F TH VWLS:HW

POSTER          : Santosh
Solutions:

BY EZHILMANI
#include
#include
int RemoveChars(char *str, char *Substring)
{
     
      char *startaddr=NULL;// Pointer which has  the starting address 4 reference
      char *indexidentifyptr=NULL;// ptr vch identifies index 4 searching subchar
      startaddr =str;        // initializing with starting address
     
      if(*Substring!=NULL)
      {
            while(*Substring!='\0')    // condition to check for end of substring
            {
                  str=startaddr;
                  while(*str!='\0')     //  condition to check for end of string
                  {
                        if(*str == *Substring) // comparing string and substring
                        {    
                              indexidentifyptr = str; // storing the address for refernce so for next time the string is searched from this point
                              for(;*str!='\0';str++)  // for adjusting the position of cahracter in the string
                                    *str=*(str+1);
                              *(str-1)='\0'// placing null at the end of string
                              str =indexidentifyptr;
                              indexidentifyptr =0;
                        }
                        else
                              str++;   // if not matched just increment the adress
                  }
                  Substring++;           
            }
            printf("%s%s",startaddr,Substring);
      }
      else
      {
            printf("substring not present");
            return -1;
      }
            return 0;
}
int main()
{
      char str[]={"Battle of the Vowels:Hawaii"};
      char Substring[]={"aeiou"};
      if(RemoveChars(str,Substring)== -1)
            printf("enter substring\n");
      return 0;
}


BY AVINASH:

#include"stdio.h"
#include "string"

bool MyStrExtractor(char *,char*);

int main()
{
       char *pszInput,*pszExtractChar;
       pszInput       = (char *)malloc(30);
       pszExtractChar = (char *)malloc(10);
       memset(pszExtractChar,0X00,10);
       memset(pszInput,0X00,30);                       
       printf("Enter the String:");
       fgets(pszInput, 30, stdin); //MY NAME IS KHAN      
       printf("\nEnter the Extractor variables:");
       fgets(pszExtractChar, 10,stdin);//AEIK
       if(true != MyStrExtractor(pszInput,pszExtractChar))
              printf("\n MYSTREXTRACTOR FAILED");
       else
             printf("\n%s",pszInput);

       free(pszInput);
       free(pszExtractChar);
       return 0;
}

bool MyStrExtractor(char *pszIn,char *pszExtract)
{     
       char *pszTempExtract = pszExtract;
       char *pszTempIn = pszIn;
       while(NULL != *pszIn )//practicing  this way to avoid mistake of EQUAL '=' instead of '=='(as null is a constant it cannot be assigned and generates a error)
       {
             while(NULL != *(pszTempExtract+1))
             {
                    if(*pszTempExtract == *pszIn)
                    {
                           pszTempIn = pszIn;
                           while(NULL != *(pszTempIn+1))//Afterextracting the char shifting entire chars to overwrite the other chars
                           {
                                 *pszTempIn = *(pszTempIn+1);
                                 pszTempIn++;
                           }
                           *(pszTempIn+1)='\0';
                           pszIn--;
                           break;
                    }
                   
                    pszTempExtract++;
             }

             pszTempExtract = pszExtract;
             pszIn++;

       }
       return true;
} BY SANTOSH:
#include "stdafx.h"
#include
#include
#include "string"

char * RemoveChar(char *,char *);


int _tmain(int argc, _TCHAR* argv[])
{
       char *pSrc,*pDest,*pRef;
       int inpStrLen1,inpStrLen2;

       pDest = NULL;

       printf("Enter the approx Input String Length\n");                                                             //Enter your approx string lengths so tat u can allocate
       scanf("%d",&inpStrLen1);                                                                                                   //memory excatly required

       pSrc = (char *)malloc(inpStrLen1*sizeof(char));
       memset(pSrc,0X00,inpStrLen1);

       printf("Enter the approx Removal Character Set String Length\n");                              
       scanf("%d",&inpStrLen2);

       pRef = (char *)malloc(inpStrLen2*sizeof(char));
       memset(pRef,0x00,inpStrLen2);

       fflush(stdin);

       printf("Enter the Input String:\n");                                                                                 //Enter the source string and string that has to be compared.
       fgets(pSrc,inpStrLen1,stdin);

       fflush(stdin);

       printf("Enter the Referenc String:\n");
       fgets(pRef,inpStrLen2,stdin);

       pDest = RemoveChar(pSrc,pRef);                                                                                       //Calling the for extraction which return pointer to extracted
                                                                                                                            //string
       printf("The Extracted String is: %s\n",pDest);

       return 0;
}

char * RemoveChar(char *pInput,char *pCharSet)
{
       int inCnt,outCnt,loopCnt1,loopCnt2;

       char *pOutput;

       bool status = FALSE;

       inCnt = outCnt = loopCnt1 = loopCnt2 =0;

       for(loopCnt1 = 0;*(pInput + loopCnt1) != NULL;loopCnt1++)                                              //Loop for counting extracted characters and
       {                                                                                                      //depending on the count, allocate memory
              for(loopCnt2 = 0;*(pCharSet + loopCnt2) != NULL;loopCnt2++)
              {
                     if(*(pInput + loopCnt1) == *(pCharSet + loopCnt2))
                     {
                           status = TRUE;
                           break;
                     }
              }
              if (!status)
              {
                     outCnt++;
              }
              else
                     status = FALSE;
       }

       pOutput = (char *)malloc(outCnt*sizeof(char));
       memset(pOutput,0x00,outCnt);

       for(loopCnt1 = 0;*(pInput + loopCnt1) != NULL;loopCnt1++)                                                     //Loop for transfering the extracted characters to pOutput
       {
              for(loopCnt2 = 0;*(pCharSet + loopCnt2) != NULL;loopCnt2++)
              {
                     if(*(pInput + loopCnt1) == *(pCharSet + loopCnt2))
                     {
                           status = TRUE;
                           break;
                     }
              }
              if (!status)
              {
                     *(pOutput + inCnt) = *(pInput + loopCnt1);
                     inCnt++;
              }
              else
                     status = FALSE;
       }

       *(pOutput +inCnt) = NULL;

       return pOutput;
}

Stringreverse in place

INPUT        : Welcome to India
OUTPUT     : emocleW ot aidnI.
          POSTER      :VILAS
 solutions :



by MANI
#include
void stringreverse( char * ptr)
{
      int i=0,k,j=0;
      char * a =ptr; // storing the starting address of the string;
      while(*ptr!=NULL)// traversing till end of string
      {
           
            while((*ptr)!=32&&(*ptr!=NULL)) // condition for blank space and NULL
            {
                  ++i;  // having a refernce for counting the character length
                  ++ptr;// moving the pointer  to the end of first string
            }
            --ptr;//placing pointer to the last character position ofthe string
            k=i/2;
            for(;i>k;i--,j++)  // swapping the characters of the string
            {
                 
                  char temp;
                  temp = *(ptr-j);
                  *(ptr-j) = *(ptr-(i-1));
                  *(ptr-(i-1)) = temp;
                 
            }
            i=0,j=0;               // reinitializing the variables
            ptr++;
            for(;*ptr==32;ptr++);  // moving to the next string in the sentence
           
      }
      printf("%s",a);
}
int main()
{
      char a[] = "welcome to india"; // string to be reversed
      stringreverse(a); //passing the string to the function
      return 0;
}

by SHRUTHI
#include "stdafx.h"
char * reverse(int a,int b,char src[20])
{
      char des[20]={0,};
      int i=0;
      for(;b != 0;b--,a--,i++)
            des[i]=src[a-1];
      des[i]= '\0';
      return des;
}

int _tmain(int argc, _TCHAR* argv[])
{
      char source[20],dest[20],temp[20];
      int i=0,count =0; 
      printf("ENTER THE STRING\n");
      gets(source);
      while(source[i]!='\0')
      {
            if(source[i]!=' ')
            {
i++;
                  count++;
            }
            else
            {
                  sprintf(temp,"%s",reverse(i,count,source));
                  strcat(dest,temp,i);
                  dest[i]=' ';
                  count = 0;
                  i++;
            }
}    
      dest[i]='\0';
      printf("%s",dest);
      return 0;
}

counter