Learn Computer Science programming - C# and C++ Algorithms

 

Remove characters from String

Removes specified characters from a given string.
Code
removecharsfromstring.zip
File Size: 4188 kb
File Type: zip
Download File

#include <iostream>
#include <string.h>
#include <windows.h>
using namespace std;

void Reverse(char* a, int Start, int End)
{
while (Start < End)
{
char Temp = a[Start];
a[Start++] = a[End];
a[End--] = Temp;
}
}


void RemoveCharsFromString()
{
wcout <<  endl << "*** Remove characters from String  ***" << endl;
wcout << "*************************************************" << endl;
char a[] = "Battle of the Vowels:Hawaii vs. Bahamas";
char b[] = "aeiou";
cout << endl << "The original string: " << a << endl;
cout << endl << "Remove the following characers: " << b << endl;
int src, RemovedArray[256];
ZeroMemory(RemovedArray, sizeof(RemovedArray));
src=0;
while(b[src])
{
RemovedArray[b[src]] = 1;
src++;
}
int dest;
dest = 0;
src=0;
do //do .. while will copy NULL at the end
{
if(!RemovedArray[a[src]])
{
a[dest++] = a[src];
}
}while(a[src++]);
cout << endl << "The new string: " << a << endl;
}

void FlipWords ()
{
wcout <<  endl << "*** Flip words  ***" << endl;
wcout << "*************************************************" << endl;

char a[] = "Tom Hanks";
cout << endl << "a= " << a << endl;
char* b = new char[strlen(a)+1];
int ReadPos, WordEnd, WordStart, WritePos = 0;
ReadPos = WordEnd = strlen(a) -1;

while (ReadPos >= 0)
{
while((a[ReadPos] != ' ') && (ReadPos >= 0))
ReadPos--;


WordStart=ReadPos +1;

while(WordStart <= WordEnd)
b[WritePos++] = a[WordStart++];

// Add space
if (ReadPos >= 0) 
b[WritePos++] = a[ReadPos--];

WordEnd = ReadPos;
}

b[WritePos] = '%%CONTENT%%';
strcpy(a, b);
delete b;
cout << endl << "a= " << a << endl; // Tom Hanks
}

void ReverseWordsNoBuffer ()
{
wcout <<  endl << "*** Reverse String No Buffer ***" << endl;
wcout << "*************************************************" << endl;
char Src[] = "Island Hawaii";
int RStart, REnd, ReadToken;
RStart=0;
REnd = strlen(Src) -1;
Reverse(Src, RStart, REnd);
cout << endl << Src << endl;
ReadToken = REnd;
RStart=ReadToken;
while (ReadToken >= 0)
{
while ((ReadToken>=0) && (Src[ReadToken] != ' '))
{
ReadToken--;
}
RStart = ReadToken+1;
Reverse(Src, RStart, REnd);
ReadToken--;
REnd = ReadToken;
}
cout << endl << Src << endl;
}

void main()
{
int x;
wcout <<  endl << "*** Words String manipulation ***" << endl;
wcout << "*************************************************" << endl;
RemoveCharsFromString();
FlipWords ();
ReverseWordsNoBuffer();
cin >> x;
 }