C Program to Reverse a String: Code with Explanation

If you're learning C programming, reversing a string is one of those basic but useful problems that helps you understand how strings and arrays work. It's a common question in exams and interviews too.

In this article, we’ll go through a few easy ways to reverse a string in C. By the end, you’ll have a clear understanding of how the logic works. Also, if you're thinking about taking your coding skills further, there’s a suggestion for you at the end.

Blogging Illustration

C Program to Reverse a String: Code with Explanation

image

What Does Reversing a String Mean?

It simply means changing the order of characters in a string so that the first character becomes the last, and so on.

Example:

  • Original:hello
  • Reversed:olleh

Now let’s look at how you can write a program to do this in C.

Method 1: Using a Temporary Array

This is the most straightforward way. We copy the characters from the original string into another array in reverse order.

Code:

                        #include 
                        #include 

                        int main() {
                            char str[100], rev[100];
                            int len, i, j;

                            printf("Enter a string: ");
                            gets(str);  // Note: gets() is outdated, use fgets() in real programs

                            len = strlen(str);
                            j = 0;

                            for(i = len - 1; i >= 0; i--) {
                                rev[j++] = str[i];
                            }

                            rev[j] = '\0';  // End the string properly

                            printf("Reversed string: %s\n", rev);

                            return 0;
                        }

                    

What’s Happening Here:

  • We find the length of the string.
  • Then we copy characters one by one from the end of the original string to the beginning of the new one.
  • Finally, we print the reversed string.

Method 2: Reverse the String Without Using Another Array

This method saves memory by doing everything in the original string itself.

Code:

                        #include 
                        #include 

                        int main() {
                            char str[100];
                            int i, len;
                            char temp;
                            printf("Enter a string: ");
                            gets(str);

                            len = strlen(str);

                            for(i = 0; i < len / 2; i++) {
                                temp = str[i];
                                str[i] = str[len - i - 1];
                                str[len - i - 1] = temp;
                            }

                            printf("Reversed string: %s\n", str);

                            return 0;
                        }


                    

Why This Works:

  • We use a temporary variable to swap characters from both ends of the string.
  • The loop runs till the middle of the string.

This is faster and uses less memory than the first method.

Method 3: Using Pointers

If you're comfortable with pointers, this is a cool way to reverse strings.

Code:

                        #include 
                        #include 

                        void reverseString(char *str) {
                            char *start, *end, temp;
                            start = str;
                            end = str + strlen(str) - 1;

                            while(start < end) {
                                temp = *start;
                                *start = *end;
                                *end = temp;
                                start++;
                                end--;
                            }
                        }

                        int main() {
                            char str[100];

                            printf("Enter a string: ");
                            gets(str);

                            reverseString(str);
                            printf("Reversed string: %s\n", str);

                            return 0;
                        }



                    

Simple Explanation:We use two pointers, one at the start and one at the end.

  • Swap characters while moving both pointers toward the center.

Some Tips

  • Avoid using gets() in real projects—it's not safe. Use fgets() instead.
  • Make sure to add the null character \0 at the end of strings when needed.
  • Practice with different inputs to understand edge cases like empty strings or very short ones.

Want to Do More Than Just C?

If you enjoy programming and want to build full websites or applications, learningFull Stack Development is a great next step.

AFull Stack Developerknows both:

  • Frontend (what users see, like websites)
  • Backend (the server and database that powers it)

You’ll learn things like:

  • HTML, CSS, JavaScript
  • React, Node.js
  • Databases like MongoDB or MySQL
  • How everything connects together

Why Learn Full Stack Development?

  • It’s in high demand.
  • You can build your own apps and websites.
  • You open up more job opportunities.

There are many online Full Stack Developer courses that are beginner-friendly and include projects and certifications. If you're already good with C, you'll find learning web development even easier.

Final Words

Reversing a string in C might be simple, but it helps you learn the basics of loops, arrays, and pointers. Try all three methods and see which one makes the most sense to you.

And if you're serious about programming, don’t stop at C. Learn how to build complete applications and explore full stack development. It’s a great way to grow your skills and open up career opportunities.

Placed Students

Our Clients

Partners

Uncodemy Learning Platform

Uncodemy Free Premium Features

Popular Courses