Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 990187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:54:34+00:00 2026-05-16T05:54:34+00:00

I am trying to reverse a character string in C Here is what I

  • 0

I am trying to reverse a character string in C

Here is what I have

void reverse(char str[]) {
    int i = 0;

    int length;
    // Get string length
    for (i = 0; str[i] != '\0' ; ++i) {
        length = i;
    }

    char reversed[1000];

    int j;
    j = 0;
    // Reverse it
    for (j = 0; j < length ; ++j) {
        reversed[j] = str[length - j];
    }

}

I know that reversed contains the string reversed, but I’m not sure how to modify the original str without throwing away data I need.

I also don’t know how to set strto reversed without looping again.

Would it be acceptable to do another…

    int m;
    m = 0;

    for (m = 0; m < length ; ++m) {
        str[j] = reversed[j];
    }

Usually I’d say this many loops smells, but I’m also still quite unfamiliar with the language so I’m not sure…

Update

Thanks for all the answers guys, and I appreciate the edits too!

I ended up going with this…

int main() {

 char str[] = "Reverse me!";

 int length;

 for (length = 0; str[length] != '\0'; length++) {

 }

 printf("length => %d chars\n", length);

 int j, k;
 char c;

 for (j = 0, k = length - 1; j < k; j++, k--) {
  c = str[k];    
  str[k] = str[j];
  str[j] = c;
 } 

 printf("reversed => %s\n", str);

 return 0;
}

Some things I now know…

  • There is a strlen() like in PHP. However, it has not been discussed in the book yet, plus I need to get familiar with null terminated strings.
  • A for loop can assign and do multiple things that are comma separated. I never knew this!

So asking was worthwhile 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T05:54:34+00:00Added an answer on May 16, 2026 at 5:54 am

    Comments on your code:

    void reverse(char str[]) {
        int i = 0;
    
        int length;
        // Get string length
        for (i = 0; str[i] != '\0' ; ++i) {
            length = i;
        }
    

    Rather than copying the i to length every time you could just wait until the end.

    size_t len = 0; // size_t is an unsigned integer that is large enough to hold the sizes
                    // of the biggest things you can have (32 bits on 32 bit computer,
                    // 64 bits on a 64 bit computer)
    char * s = str;
    while (*s) {
        len++;
        s++;
    }
    

    Though the compiler would probably be able to make this optimization for you.

    You should know, though, that there is a standard string function strlen ( #include <string.h> )which will measure the length of a char string using the same general algorithm (look for the end) but is usually optimized for the target processor.

    len = strlen(str);
    

    Your code again:

        char reversed[1000];
    

    Using big arrays are good for learning and simple examples, but you can also allocate memory dynamically based on the size you now know you need. The standard function for doing this is malloc which is in stdlib.h (also in malloc.h). Memory allocated with this function should also be freed, though.

    int * p = malloc( 8 * sizeof(int) ); // allocate an array of 8 ints
    /* ... work on p ... */
    free(p);
    /* ... don't access the memory pointed to by p anymore ... */
    p = 0;
    

    There are also other functions in the malloc family. There’s calloc, which allocates memory and clears sets it to 0. There is also a function called strdup (which isn’t in standard C, but is very widely available in string.h) which takes a string and allocates a duplicate of it. It is really just:

    char * strdup(const char * str) {
        size_t len = strlen(str);
        char * s = malloc(len+1);
        if (!s) {
            return s;
        }
        return strcpy(s,str); // This could have been memcpy since you know the size
                              // and memcpy might have been faster on many processors
    }
    

    Another useful memory allocation function is alloca (not in the C standard, but widely available and similar functionality is available with variable length arrays in C99). It is great, but works differently from malloc. It allocates memory that is only useable until the current function returns because this memory is allocated in the same way as memory for local variables (from the stack).

    More of your code:

        int j;
        j = 0;
        // Reverse it
        for (j = 0; j < length ; ++j) {
            reversed[j] = str[length - j];
        }
    

    The code:

    void reverse_in_place(char * str, size_t len) {
       size_t i, j;
       for (i = 0, j = len - 1; i < j ; i++, j--) {
            char a = str[i];
            char z = str[j];
            str[i] = z;
            str[j] = a;
       }
    }
    

    should swap the order of the string without making a copy of it. For strings with odd length it won’t try to swap the middle char with itself.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.