Monday, March 1, 2010

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;
}
By AVINASH
#include "stdio.h"
#include "stdlib.h"
#include "string"

void myReverse(char *pSrc,char *pDest,int iWordLen);

int main()
{
       char *pBuffIn,*pBuffOut,*pTempOut,*pTempIn;
       int iCount=0,iWordLen=0;
       pBuffIn = (char *)malloc(40);
       memset(pBuffIn, '\0', 40);
       printf("ENTER:");
       fgets(pBuffIn, 40, stdin);
       pBuffOut = (char *)malloc(strlen(pBuffIn)+1);
       memset(pBuffOut, '\0', 40);
       pTempIn = pBuffIn;//Dummy variables
       pTempOut = pBuffOut;//dummy variables
      
       //For loop To track the END of the Statement
       for( iCount=0; *pBuffIn != NULL; pTempIn++,iCount++,pTempOut++)
       {
             iWordLen++;
             if(*pTempIn == 32 || *(pTempIn+1)== NULL)//Condition 2 check SPACE r NULL
             {
                   
                    myReverse(pTempIn-1, pTempOut-iWordLen+1,iWordLen-1);//-1 to pTempin and iWordlen to remove SPACE Char
                 if(*pTempIn == 32)
                           {     
                                 *pTempOut=32;   //Adding space CHAR
                                 iWordLen = 0;//initializin wordlen for next word.
                           }
                    else
                           {
                                 *pTempOut = '\0';
                                 break;
                        }
             }
       }
      
       printf("\n%s",pBuffOut);
    return 0;
}

void myReverse(char *pSrc,char *pDest, int iWordLen)
{     
      
       while((iWordLen-- != 0) && (*(pSrc+1)!= NULL))
       {
             *(pDest++) = *(pSrc--);
       }
}

No comments:

counter