TeachingBee

How to Reverse a String in C

How to Reverse a String in C-teachingbee

What is a String in C?

In C programming language, a string is a series of characters stored in contiguous memory locations. Unlike other programming languages, C represents a string as arrays of characters. Strings in C is terminated by a null character represented as “\0”, signifying the end of the string. In this post we will discuss various ways to reverse a string in c.

Code to declare and define a string in C:

Char str[] = “Sample String”;

Meaning of Reversing a String

Reversing a string is an important technique in C programming language. It is a technique that involves inversion of the given string i.e. change in the position of characters such tha the first character becomes the last character, second character becomes the second last, third becomes the third last and so on.

For example, if the inputted string is “TeachingBee” then its reversed string is “eeBgnihcaeT”.

Reverse a String in c

Methods to Reverse a String in C

There are multiple ways to write the program to reverse a string in C programming language:

  1. Reverse a string using the strrev() function
  2. Reverse a string without using the library function
  3. Reverse a string using for loop
  4. Reverse a string using while loop
  5. Reverse a string using pointers
  6. Reverse a string using the recursion function

Reverse a String Using the strrev() Function

Algorithm

  1. Declare a character array ‘str’ to store the string.
  2. Ask the user to enter the string.
  3. Reverse the string using strrev() function.
  4. Display the reversed string as output.

Source Code


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

int main() {
    char str[40]; 
    printf("\nEnter a string to be reversed: ");
    scanf("%s", str);
    strrev(str);

    printf("\nAfter the reverse of a string: %s", str);
    
    return 0;
}

Output

f

Reverse a String Without Using the Library Function

Algorithm

  1. Define a function reverseString().
  2. In reverseString() function, use a for loop with two indices (‘start’ and ‘end’) to swap characters from the beginning to the end of the string until ‘start’ is less than ‘end’.
  3. Declare a character array ‘mystring’ to store the string.
  4. Ask the user to enter the string.
  5. Call the function named reverseString().
  6. Display the reversed string as output.

Source Code

#include <stdio.h>

void reverseString(char str[]) {
    int length = 0;

    while (str[length] != '\0') {
        length++;
    }
    for (int start = 0, end = length - 1; start < end; start++, end--) {
        char temp = str[start];
        str[start] = str[end];
        str[end] = temp;
    }
}

int main() {
    char myString[40];
    printf("Enter a string to be reversed: ");
    scanf("%s", myString);
    reverseString(myString);
    printf("After the reverse of a string: %s\n", myString);

    return 0;
}

Output

e

Reverse a String Using for loop

Algorithm

  1. Declare a character array ‘mystring’ to store the string.
  2. Ask the user to enter the string.
  3. Calculate the length of the string.
  4. Use a for loop that will iterate half the length of input string to swap characters from the beginning with the characters at ‘length-1-i’ position.  
  5. Display the reversed string as output.

Source Code

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

int main() {
    char myString[100];

    printf("Enter a string: ");
    scanf("%s", myString);

    int length = strlen(myString);
    if (myString[length - 1] == '\n') {
        myString[length - 1] = '\0';
    }

    for (int i = 0; i < length / 2; i++) {
        char temp = myString[i];
        myString[i] = myString[length - 1 - i];
        myString[length - 1 - i] = temp;
    }

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

    return 0;
}

Output

d

Reverse a String Using while loop

Algorithm

  1. Declare a character array ‘mystring’ to store the string.
  2. Ask the user to enter the string.
  3. Calculate the length of the string.
  4. Use a while loop that will iterate half the length of input string to swap characters from the beginning with the characters at ‘length-1-i’ position.  
  5. Display the reversed string as output.

Source Code

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

int main() {
    char myString[100];

    printf("Enter a string: ");
    scanf("%s", myString);

    int length = strlen(myString);

    if (myString[length - 1] == '\n') {
        myString[length - 1] = '\0';
        length--; 
    }

    int i = 0;
    while (i < length / 2) {
        char temp = myString[i];
        myString[i] = myString[length - 1 - i];
        myString[length - 1 - i] = temp;

        i++;
    }

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

    return 0;
}

Output

c

Reverse a String Using Pointers

Algorithm

  1. Define a constant ‘MAX_LENGTH’ for the maximum length of the input string.
  2. Define a function reverseString().
  3. In reverseString() function, create two pointers ‘start’ and ‘end’ and then use a while loop that will iterate half the length of input string to swap characters from the beginning to the end of the string.
  4. In main() function, declare a character array ‘mystring’ to store the string.
  5. Ask the user to enter the string.
  6. Call the function named reverseString().
  7. Display the reversed string as output.

Source Code

#include <stdio.h>
#include <string.h>
#define MAX_LENGTH 100

void reverseString(char *str) {
    int length = strlen(str);

    char *start = str;
    char *end = str + length - 1;

    while (start < end) {
        char temp = *start;
        *start = *end;
        *end = temp;

        start++;
        end--;
    }
}

int main() {
    char myString[MAX_LENGTH];

    printf("Enter a string: ");
    scanf("%s", myString);
    reverseString(myString);
    printf("Reversed string: %s\n", myString);

    return 0;
}

Output

b

Reverse a String using Recursion

Algorithm

  1. Define a function reverseString().
  2. In reverseString() function, if ‘start’ is less than ‘end’, swap the characters at indices ‘start’ and ‘end’. And call the reverseString() function recursively until the condition i.e. (start<end) satisfies.
  3. In main() function, declare a character array ‘mystring’ to store the string.
  4. Ask the user to enter the string.
  5. Call the function named reverseString().
  6. Display the reversed string as output.

Source Code

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

void reverseString(char str[], int start, int end) {
    if (start < end) {
        char temp = str[start];
        str[start] = str[end];
        str[end] = temp;
        
        reverseString(str, start + 1, end - 1);
    }
}

int main() {
    char myString[100];
    printf("Enter a string: ");
    scanf("%s", myString);
    int length = strlen(myString);
    
    reverseString(myString, 0, length - 1);
    
    printf("Reversed string: %s\n", myString);

    return 0;
}

Output

a

Conclusion

In conclusion, string reversal in C is an important skill for every programmer. This blog has highlighted multiple methods, from traditional loops to modern concepts. Mastering this technique not only helps in problem-solving but also builds foundation for solving complex programming challenges. The knowledge gained in the blog is a stepping stone toward becoming a more versatile programmer. Happy learning!

Checkout more C Tutorials here.

I hope You liked the post 👍. For more such posts, 📫 subscribe to our newsletter. Try out our free resume checker service where our Industry Experts will help you by providing resume score based on the key criteria that recruiters and hiring managers are looking for.

FAQ

How do you reverse a string in C?

How do you reverse a string in C++?

How to reverse a string?

How to reverse a string using swap in C?

90% of Tech Recruiters Judge This In Seconds! 👩‍💻🔍

Don’t let your resume be the weak link. Discover how to make a strong first impression with our free technical resume review!

Related Articles

Subtraction of Two Numbers Program in C

In this blog post, we will checkout the Subtraction of Two Numbers Program in C. How to Write Subtraction Program in C? To create a C program to subtract two

Add Two Numbers In C

In this blog post, we will checkout the how to add two numbers in C. Algorithm to Add Two Numbers in C To create a C program to add two

Why Aren’t You Getting Interview Calls? 📞❌

It might just be your resume. Let us pinpoint the problem for free and supercharge your job search. 

Newsletter

Don’t miss out! Subscribe now

Log In