Having trouble with homework lesson 16 (ah.. pointers..)

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
Clodi
Posts: 175
Joined: November 18th, 2012, 7:47 pm

Having trouble with homework lesson 16 (ah.. pointers..)

Post by Clodi » June 10th, 2013, 2:41 am

I am trying to solve the bit where we are supposed to reverse a string a print it on screen.
Here is my approach:

This is O.K.

Code: Select all

void putString( const char* pChar )
{
	for ( ; *pChar != 0 ; pChar ++ )
	{
		putchar( *pChar );
	}
}
This is also O.K.

Code: Select all

void getString( char* pChar, int MaxLength )
{
	char c = 0;
	char index = 0;

	for ( ; index < MaxLength && c != '\n' ; index ++ )
	{
		c = getchar();
		*( pChar + index ) = c;
	}
	*( pChar + index ) = '\0';
}
This is where the problem is..

Code: Select all

void revString( char* pChar )
{
	//create a copy of the buffer somewhere else in memory
	char Buffer[127];

	//point to the first guy
	char* pBuff = &Buffer[0];

	//copy first buffer (in main) to buffer in this function
	for ( char index = 0; *( pChar + index ) != 0; index ++ )
	{
		Buffer[ index ] = *( pChar + index );
	}

	//point to the last guy
	for ( ; *pBuff != 0; pBuff ++ );


	//run through (main) buffer and replace each letter with the reverse
	//that is achieved by accessing second (function) buffer other way round
	for ( ; *pChar != 0; pChar ++ )
	{
		for ( char index = 0; *( pBuff + index ) < 127 && *( pBuff + index ) > (-128) ; index -- )
		{
			*pChar = *( pBuff + index );
		}
	}
}

and this is the main.. again, the issue is above

Code: Select all

void main()
{
	putString( "Welcome to America\nType in some shit: " );
	char Buffer[127];
	getString( &Buffer[0],127 );

	putString( "Here's your string: " );
	putString( &Buffer[0] );

	putString( "And now reversed: " );
	revString( &Buffer[0] );
	putString( &Buffer[0] );
}

User avatar
LuisR14
Posts: 1248
Joined: May 23rd, 2013, 3:52 pm
Location: USA
Contact:

Re: Having trouble with homework lesson 16 (ah.. pointers..)

Post by LuisR14 » June 11th, 2013, 2:00 am

ok here's your problem

1. you're checking that the value pointed by *(pBuff + index) is less than 127 and greater than -128

Code: Select all

	for ( char index = 0; *( pBuff + index ) < 127 && *( pBuff + index ) > (-128) ; index -- )
when it should be checking that index is greater than -128
but since the index into the pointer below can't be negative the above would have to start from 127 to 0

Code: Select all

		*pChar = *( pBuff + index );
2. you're looping thru the string and setting the same last value into the reversed pChar string

Code: Select all

	for ( ; *pChar != 0; pChar ++ )
	{
		for ( char index = 0; *( pBuff + index ) < 127 && *( pBuff + index ) > (-128) ; index -- )
		{
			*pChar = *( pBuff + index );
		}
	}
but since problem 1. is happening you're actually putting in some undefined value (or garbage lol)
always available, always on, about ~10 years c/c++, java[script], win32/directx api, [x]html/css/php/some asp/sql experience. (all self taught)
Knows English, Spanish and Japanese.
[url=irc://irc.freenode.net/#pchili]irc://irc.freenode.net/#pchili[/url] [url=irc://luisr14.no-ip.org/#pchili]alt[/url] -- join up if ever want real-time help or to just chat :mrgreen: --

Clodi
Posts: 175
Joined: November 18th, 2012, 7:47 pm

Re: Having trouble with homework lesson 16 (ah.. pointers..)

Post by Clodi » June 13th, 2013, 2:56 am

Thank you so much,

But why is this one not working?? :cry:

Code: Select all

void revstring( int* p )
{
	int index;
	for ( index = 0; *( p + index ) != 0; index ++ );
	int* pEnd = ( p + index );

	while (pEnd >= p )
	{
		int a = 0;
		a = *p;
		*p = *pEnd;
		*pEnd = a;
		p ++;
		pEnd --;
	}
}

User avatar
LuisR14
Posts: 1248
Joined: May 23rd, 2013, 3:52 pm
Location: USA
Contact:

Re: Having trouble with homework lesson 16 (ah.. pointers..)

Post by LuisR14 » June 13th, 2013, 1:14 pm

i believe it's because you're using int* instead of char* o.o
what you have there would work fine with char* :P

edit: actually, it's something else that's causing it to not work xP
you're copying over the null character from the end of the string to the beginning

Code: Select all

	for ( index = 0; *( p + index ) != 0; index ++ );
	int* pEnd = ( p + index );
simple solution tho (let's see if you figure it out tho :P)
always available, always on, about ~10 years c/c++, java[script], win32/directx api, [x]html/css/php/some asp/sql experience. (all self taught)
Knows English, Spanish and Japanese.
[url=irc://irc.freenode.net/#pchili]irc://irc.freenode.net/#pchili[/url] [url=irc://luisr14.no-ip.org/#pchili]alt[/url] -- join up if ever want real-time help or to just chat :mrgreen: --

Clodi
Posts: 175
Joined: November 18th, 2012, 7:47 pm

Re: Having trouble with homework lesson 16 (ah.. pointers..)

Post by Clodi » June 16th, 2013, 8:40 am

thank you so much : )

tonight i ll look into it. I really want to get it to work !!

Post Reply