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?
 
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
 
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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
 
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
 
phoffricCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.