Code:
// StrReversse.cpp : Entry point for this console app.
//Reversing a string.
/*The one and only core function swaps the first and last element of a char[],
then it swaps the second element with the second last element. Etc.*/
#include "stdafx.h"
#include <iostream.h>
#include <string.h>
//Reversing a string
//Forward declaration.
void reverse(char* s);
int main(int argc, char* argv[])
{
cout<<"enter a sentence"<<endl;
char szSentence[100];
cin.getline(szSentence,100);
reverse(szSentence);
cout<< "Reversed string: "<<szSentence<<endl;
return 0;
}
void reverse(char* s)
{
char temp; //help variable for "swapping" 2 characters.
int lenght = strlen(s);
int lastchar = lenght - 1;
for(int i=0;i<lenght/2; i++)
{
temp=s[i];
s[i]=s[lastchar-i];
s[lastchar-i]=temp;
}
}