View Single Post
Old 07-12-2003, 11:37 PM   #3 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
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;
	}

}
Valmont is offline   Reply With Quote