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

 

Reverse Characters inside a string

This examples reverses character and basically writes a sentence backwards.
Code
reverse_characters.zip
File Size: 2843 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 ReverseWordsNoBuffer ()
{
wcout <<  endl << "*** Reverse String No Buffer ***" << endl;
wcout << "*************************************************" << endl;
char Src[] = "Testing reverse";
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;
ReverseWordsNoBuffer();
ReverseWordsNoBuffer();
cin >> x;
 }