INPUT : STRING-> Battle of the Vowels:Hawaii
REMOVE-> AEIOU
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;
No comments:
Post a Comment