Write a function in C that reverses a string without allocating any extra storage.

void ReverseString(char* String)
{
   for (i = 0; i < n/2 ; i++){
      swap ( a[i], a[n-1-i]);
   }
}
cpursley1979Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Infinity08Commented:
What's the question ?
0
cpursley1979Author Commented:
I am a bit rusty wit C. could you help me develop this algorithm to reverse an arbitrary string?
0
Infinity08Commented:
apart from the fact that your variables aren't used correctly, the algorithm is ok.
void reverseString(char* str) {
    size_t n = strlen(str);
    size_t i = 0;
    for (i = 0; i < n/2 ; i++) {
        swap(str[i], str[n - 1 - i]);
    }
}

Open in new window

0
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

Infinity08Commented:
That assumes that swap is a macro btw. If it is a function, you'll need to pass addresses, rather than the characters.
0
phoffric\Commented:
This still has temporary storage for n and i.
void ReverseString(char* a)
{ int n = strlen(a);
   for (int i = 0; i < n/2 ; i++){
      a[i] ^= (a[n-1-i] ^= (a[i] ^= a[n-1-i]) ) ;
   }
}

Open in new window

0
cpursley1979Author Commented:
#include <stdio.h>

int main ()
{
    //ReverseString function
    char* sx1 = "hello";
    //ReverseString(sx1);
    printf(ReverseString(sx1));
    return 0;
}

void ReverseString(char* str) {
    size_t n = strlen(str);
    size_t i = 0;
    for (i = 0; i < n/2 ; i++) {
        swap(str[i], str[n - 1 - i]);
    }
}
Compiled with error:
$ gcc -Wall dmp.c
dmp.c: In function `main':
dmp.c:21: warning: implicit declaration of function `ReverseString'
dmp.c:22: warning: passing arg 1 of `printf' makes pointer from integer without
a cast
dmp.c: At top level:
dmp.c:49: error: conflicting types for 'ReverseString'
dmp.c:21: error: previous implicit declaration of 'ReverseString' was here
dmp.c: In function `ReverseString':
dmp.c:50: warning: implicit declaration of function `strlen'
dmp.c:53: warning: implicit declaration of function `swap'
0
Infinity08Commented:
>>     char* sx1 = "hello";

"hello" is a string literal, and its type is const char*, not char*. You cannot modify it.

You'll have to copy the string literal into a modifiable string, eg. :

        char sx1[6] = "hello";


>>     printf(ReverseString(sx1));

the ReverseString function returns nothing (void), so you cannot pass the return value to printf as parameter. If you want the ReverseString function to return a pointer to the reversed string, then you need to do so :

        char* reverseString(char* str) {
            /* ... */
            return str;
        }


>>         swap(str[i], str[n - 1 - i]);

You still need to implement the swap macro, for example like this :

        #define swap(a, b) do { char tmp = a; a = b; b = tmp; } while(0)



Finally, you also need to have a forward declaration of the ReverseString function, so it can be used in main :

        char* reverseString(char* str);

and you'll need the proper include for the strlen function :

        #include <string.h>
#include <stdio.h>
#include <string.h>

#define swap(a, b) do { char tmp = a; a = b; b = tmp; } while(0)

char* reverseString(char* str);

int main(void) {
    char sx1[] = "hello";
    printf(reverseString(sx1));
    return 0;
}

char* reverseString(char* str) {
    size_t n = strlen(str);
    size_t i = 0;
    for (i = 0; i < n/2 ; i++) {
        swap(str[i], str[n - 1 - i]);
    }
    return str;
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Algorithms

From novice to tech pro — start learning today.