You are here

C program to reverse a string

Reverse a string in C: This program reverses a string that a user inputs. For example, if a user enters a string "hello" then on reversing it will be "olleh". C program to reverse a string using strrev, without using strrev, recursion and pointers. A string which remains the same on reversal is a palindrome string.

Reverse a string in C using strrev

  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int main()
  5. {
  6.    char arr[100];
  7.  
  8.    printf("Enter a string to reverse\n");
  9.    gets(arr);
  10.  
  11.    strrev(arr);
  12.  
  13.    printf("Reverse of the string is \n%s\n", arr);
  14.  
  15.    return 0;
  16. }

C reverse string program output:
Reverse string C program output

Download Reverse string program.

C program to reverse words in a string

String reversal without strrev function

First we calculate length of the string without using strlen function and then copy its characters in reverse order (from end to beginning) to a new string using a for loop.

  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.    char s[1000], r[1000];
  6.    int begin, end, count = 0;
  7.  
  8.    printf("Input a string\n");
  9.    gets(s);
  10.  
  11.    // Calculating string length
  12.  
  13.    while (s[count] != '\0')
  14.       count++;
  15.  
  16.    end = count - 1;
  17.  
  18.    for (begin = 0; begin < count; begin++) {
  19.       r[begin] = s[end];
  20.       end--;
  21.    }
  22.  
  23.    r[begin] = '\0';
  24.  
  25.    printf("%s\n", r);
  26.  
  27.    return 0;
  28. }

C program to reverse a string using recursion

  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. void reverse(char*, int, int);
  5.  
  6. int main()
  7. {
  8.    char a[100];
  9.  
  10.    gets(a);
  11.  
  12.    reverse(a, 0, strlen(a)-1);
  13.  
  14.    printf("%s\n", a);
  15.  
  16.    return 0;
  17. }
  18.  
  19. void reverse(char *x, int begin, int end)
  20. {
  21.    char c;
  22.  
  23.    if (begin >= end)
  24.       return;
  25.  
  26.    c          = *(x+begin);
  27.    *(x+begin) = *(x+end);
  28.    *(x+end)   = c;
  29.  
  30.    reverse(x, ++begin, --end);
  31. }

In the recursive method, we swap characters at the beginning and the end of the string and then move towards the middle of the string. This way is inefficient due to repeated function calls but useful in practicing recursion.

C program to reverse a string using pointers

Now we will invert a string using pointers or without using the library function strrev.

  1. #include<stdio.h>
  2.  
  3. int string_length(char*);
  4. void reverse(char*);
  5.  
  6. main()
  7. {
  8.    char s[100];
  9.  
  10.    printf("Enter a string\n");
  11.    gets(s);
  12.  
  13.    reverse(s);
  14.  
  15.    printf("Reverse of the string is \"%s\".\n", s);
  16.  
  17.    return 0;
  18. }
  19.  
  20. void reverse(char *s)
  21. {
  22.    int length, c;
  23.    char *begin, *end, temp;
  24.  
  25.    length = string_length(s);
  26.    begin  = s;
  27.    end    = s;
  28.  
  29.    for (c = 0; c < length - 1; c++)
  30.       end++;
  31.  
  32.    for (c = 0; c < length/2; c++)
  33.    {        
  34.       temp   = *end;
  35.       *end   = *begin;
  36.       *begin = temp;
  37.  
  38.       begin++;
  39.       end--;
  40.    }
  41. }
  42.  
  43. int string_length(char *pointer)
  44. {
  45.    int c = 0;
  46.  
  47.    while( *(pointer + c) != '\0' )
  48.       c++;
  49.  
  50.    return c;
  51. }