Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.
Code:
    char*   lhs   = &string[0];                  
    char*   rhs   = &string[strlen(string)-1];   
    ...                                
    do {                                            
       ...
    } while ( ++lhs < --rhs )
This sets the pointers within the confines of the string memory location and the increment/decrement is done at the bottom of the loop. Not a big difference, but clearer for the novice.

"" might give you some trouble. With your changes, underflow. Lloyddean's works. Might be less clear, but it's more right.

-Lee
 
Does anyone remember that one line string reversal?

It was something like:

while( a line of code here);

And that would do the whole reversal. I dont remember the code, but when I saw it, I was very impressed.
 
I'll take this challenge, but I'm not guaranteeing any replies to the thread with the answer, so don't hold your breath. :eek:

Hint: Work out how to find the start and end of the first word. Then work out how you would get to the start and end of the next word.
 
Hint: Work out how to find the start and end of the first word. Then work out how you would get to the start and end of the next word.

Code:
#include <stdio.h>

main () {
	char string[51];
	int length;
	int counter;
	int lettercounter = 0;
	int spacecounter = 0;
	
	printf("Enter some text: ");
	scanf("[^\n]", string);
	
	length = strlen(string);
	printf("%d", length);

	for (counter = 0 ; counter <= length ; counter++) {
		if (string[counter] == 32)
		{
			spacecounter++;
			for ( ; lettercounter != 0 ; lettercounter--)
				printf("%c", string[lettercounter]);
		}	
		else
		{
			for ( ; spacecounter != 0 ; spacecounter--)
				printf("\32");
			lettercounter++;
		}
	}
}

I think I've almost got it! I can still see a problem with this bit:

Code:
for ( ; lettercounter != 0 ; lettercounter--)
//and also this bit
for ( ; spacecounter != 0 ; spacecounter--)

This'll never allow the program to get to the second word. But I can't think of a way to fix it. Just when I think I've wrapped my mind around it, it eludes me. The only way I can think of involves introducing two more variables, and even then, I still haven't fully got the solution worked out.
 
Last edited:
One of many possible solutions for those who have thought about but weren't quite willing to make the effort.

To those that have made the effort but haven't quite got it it's been a while now so I'm posting this and you may wish to look away.

Code:
#include <assert.h>
#include <ctype.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BUFFER_MAX      (49)

static void memrev(char* const pLHS, char* const pRHS)
{
    assert(pLHS < pRHS);

    char*   lhs = pLHS - 1; 
    char*   rhs = pRHS + 1; 
    char    t;
    while ( ++lhs < --rhs )
    {
        t       = *lhs;
        *lhs    = *rhs;
        *rhs    = t;
    }
}

static char* strrev(char* const string)
{
    memrev(string, &string[strlen(string) - 1]);

    return string;
}

int main(void)
{
    char    string[BUFFER_MAX + 1];

    char*   pRHS;
    char*   pLHS;

    // WARINING: The use of 'gets' is problematic in that 'gets' has no way of
    // knowing the length of the buufer 'string'
    
    printf("Enter some text (max %d characters): ", BUFFER_MAX);
    gets(string);

    printf("%s \n", string);
    printf("%s \n", strrev(string));
    strrev(string);

    fflush(stdin);

    pLHS = pRHS = string;

    while ( *pRHS )
    {
        pLHS    = pRHS;
        while ( isspace(*pLHS++) )          { ; }
        
        pRHS    = --pLHS;
        while ( *++pRHS && !isspace(*pRHS) ){ ; }

        memrev(pLHS, pRHS - 1);
    }

    printf("%s \n", string);

    return EXIT_SUCCESS;
}
 
My contribution... recursion!

Code:
#include <stdio.h>

void reverse(char *rev)
{
        if (*rev)
        {
                reverse(rev+1);
                printf("%c", *rev);
        }

}

int main(int argc, char *argv[])
{
        char thestr[128];

        printf("Enter string: ");
        scanf("%s", thestr);
        reverse(thestr);
        printf("\n");

        return 0;
}
 
Does anyone remember that one line string reversal?

It was something like:

while( a line of code here);

And that would do the whole reversal. I dont remember the code, but when I saw it, I was very impressed.
Here's an example, but it's not minimal:
http://stackoverflow.com/questions/6788230/string-reversal-using-a-single-line-of-code-in-c

Found by googling: one line string reverse c

Personally, I am no longer impressed by obtuse C code. I've had to debug and de-obtusify far too much of it.
 
Code:
static void memrev(char* const pLHS, char* const pRHS)
{
    assert(pLHS < pRHS);

    char*   lhs = pLHS - 1; 
    char*   rhs = pRHS + 1; 

    while ( ++lhs < --rhs )
    {
        *lhs -= *rhs += *lhs -= *rhs = -*rhs;
    }
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.