Reverse A String In C

Reverse A String In C: In this post, we will discuss how to reverse a string in C. There are various ways in which we can reverse a string in c.

Reverse A String In C

Reverse a string in C using strrev

#include <stdio.h>  
#include <string.h>  
int main()  
{  
    char str[10]; 
    printf("Enter a string which you need to reverse: ");  
    scanf("%s", str);  
    printf("After the reverse of string: %s", strrev(str));  
    return 0;  
} 
Output:

Enter a string which you need to reverse: 12
After the reverse of string: 21

Reverse of a string without using the library function

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

void revstr(char *str1)  
{  
    int i, len, temp;  
    len = strlen(str1); 
    for (i = 0; i < len/2; i++)  
    {  
        temp = str1[i];  
        str1[i] = str1[len - i - 1];  
        str1[len - i - 1] = temp;  
    }  
}  
      
    int main()  
    {  
        char str[50];
        printf ("Enter the string: ");  
        gets(str);

        revstr(str);  
        printf ("After reversing the string: %s", str);  
    }  
Output:

Enter the string: Hello
After reversing the string: olleH

Reverse a string using recursion

#include<stdio.h>
#include<string.h>
int main(void)
{

char mystr[10];
int len, i;

printf("Please insert a string you want to reverse: ");

scanf( "%s", mystr);

len = strlen(mystr);

for(i = len - 1; i >= 0; i--) {
     printf("%c", mystr[i]);

}

return 0;
}
Output:

Please insert a string you want to reverse: Hello
olleH

Reverse a string using pointers

#include <stdio.h>  
#include <string.h>  
  
int str_len( char *st);  
void revstr( char *st);  
int main()  
{  
    char st[50];  
    printf (" Enter a string to be reversed: ");  
    scanf( "%s", st);  
      
    revstr(st);   
    
    printf (" After reversing the string is: %s", st);  
    return 0;  
}  
  
void revstr (char *st)  
{  
    int len, i;  
    char *start, *end, temp;  
      
    len = str_len (st);  
    start = st;  
    end = st;  
      
    for (i = 0; i < len - 1; i++)  
    end++;  
      
    for (i = 0; i < len/2; i++)  
    {  
        temp = *end;  
        *end = *start;  
        *start = temp;  
          
        start++;  
        end--;  
    }  
}  
  
int str_len (char *ptr)  
{  
    int i = 0;  
    while ( *(ptr + i) != '\0')  
    i++;  
    return i;  
}
Output:

Enter a string to be reversed: Nkcoderz
After reversing the string is: zredockN

Reverse a string using swapping

#include <stdio.h>  
#include <string.h>  
void main()  
{  
    char str[50], temp;  
    int i, left, right, len;  
    printf ("Enter a string to reverse order: ");  
    scanf( "%s", &str);  
    len = strlen(str); 
    left = 0; 
    right = len - 1; 
    for (i = left; i <right; i++)  
    {  
        temp = str[i];  
        str[i] = str[right];  
        str[right] = temp;  
        right--;  
    }  
    printf ("After reversing the string is: %s ",  str);   
}
Output:

Enter a string to reverse order: Hello
After reversing the string is: olleH

Reverse A String In C: In this post we have learnt that there are various ways in which we can reverse a string, whether it is swapping or using strrev function. One thing to note here is that you should know each solution to perform well in exams or interview. Thanks!


If You Like This Page Then Make Sure To Follow Us on Facebook, G News and Subscribe Our YouTube Channel. We will provide you updates daily.