Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reverse needs corrected.!!

Status
Not open for further replies.

Higher

IS-IT--Management
Sep 15, 2005
1
GB
Can anyone please fix my program. When I run it and for example : I type --> hello world , when it does the reverse it shows dello worl4. Please fix my problem. I would appreciate it very much. The reverse should say dlrow olleh
-----------------------------------------------------------
#include <iostream>
#include <string.h>
using namespace std ;
void strReverse ( char *pString ) ;
int main ()
{
charch[20] ;
cout<< &quot;Please enter the string you want to reverse: &quot; ;
cin.getline ( ch, 20 ) ;
strReverse( ch ) ;
cout<< &quot;Your string reversed is: &quot;
<< ch
<< '\n' ;
return 0 ;
}
//
//********************************************************************
**********
//****
// Routine:strReverse ()
// Programmer:Daniel Mulcahy
// Purpose:This routine reverses a string
// Parameters:pString - type char * : The string to be left trimmed
in
//place.
// Returns:Nothing.
//
//********************************************************************
**********
void strReverse (char *pString){
int i,k;
char temp;
for(i=0,k=strlen(pString)-1;i>=k;i++,k--)
temp = pString;
pString = pString[k];
pString[k] = temp;
}
 
OK...here it is. Have fun....
(ivarmeijer@hotmail.com)

#include <iostream.h>
#include <stdio.h>
#include <string.h>
//********************************************************************
void strReverse ( char *pString );

int main ()
{
char ch[20] ;
cout<< &quot;Please enter the string you want to reverse: &quot;;
cin.getline ( ch, 20 );
strReverse( ch );
cout<< &quot;Your string reversed is: &quot; << ch << '\n';
getchar();
return 0 ;
}
//********************************************************************
void strReverse(char *pString)
{
char temp;
char *firstChar, *lastChar;

firstChar = pString;
lastChar = pString + strlen( pString ) - 1;
while( firstChar < lastChar ) {
temp = *firstChar;
*firstChar = *lastChar;
*lastChar = temp;
firstChar++;
lastChar--;
}
}
 
I'm not positive, but I sort of recall seeing a &quot;str.....&quot; function that does this.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top