reverse string function

hello, my reverse string function doesn't work and I can't understand why..

this function prints a string to console
void putString( const char* pChar )
{
	for ( ; *pChar != 0; pChar ++ )
	{
		putchar( *pChar );
	}
}


this function simply gets a string from user
void getString( char* pBuf,int maxLen )
{
	char c = 0;
	int index = 0;

	for ( ; index < maxLen && c != '\n'; index ++ )
	{
		c = getchar();
		*( pBuf + index ) = c;
	}

	*( pBuf + index ) = 0;
}


this is where the problem is
void reverseString( char* pBuf )
{
	//get size of string
	int index = 0;
	for ( ; *pBuf != 0; index ++ );
	index --;

	//create internal buffer
	char Buffer2[ 100 ];
	for ( int a = 0; a < 100; a ++ )
	{
		Buffer2[ a ] = 0;
	}

	//store reverse into internal buffer
	char size = index;
	for ( ; index != 0; index -- )
	{
		Buffer2[ size - index ] = *( pBuf + index );
	}

	//copy internal buffer into main buffer
	for ( char i = 0; i < 100 && Buffer2[ i ] != 0; i ++ )
	{
		*( pBuf + i ) = Buffer2[ i ];
	}
}


this is the main

include <stdio>

void main()

{
	char Buffer[ 100 ];

	getString( &Buffer[ 0 ],100 );

	putString( "\nthis is what you have just written: " );
	putString( &Buffer[ 0 ] );
	putString( "\nand this is the reverse of it: " );
	reverseString( &Buffer[ 0 ] );
	putString( &Buffer[ 0 ] );
}
Last edited on
Are you getting errors? Incorrect output? Please explain the problem.
yes,

the program asks for some input as it's supposed to and then it prints off the string.

then, instead of printing the reverse of it, it pauses (?)

..the error is in the reverse function..
The first for loop inside the function doesn't modify pBuf, so if execution enters that loop it will loop forever.
oh dear... thank you so much.
Topic archived. No new replies allowed.