A Program to Reverse String

In this example, we are giving a program to reverse a string without the strrev() function.


Program to to reverse a string:

  1. #include <stdio.h>
  2. int main() {
  3. char str[1000], rev[1000];
  4. int start, end, count = 0;
  5. printf("Input a string: ");
  6. gets(str);
  7. // calculating string length
  8. while(str[count] != '\0')
  9. count++;
  10. end = count - 1;
  11. for(start=0; start<count; start++) {
  12. rev[start] = str[end];
  13. end--;
  14. }
  15. rev[start] = '\0';
  16. printf("Reverse of the string: %s\n",rev);
  17. return 0;
  18. }

Output:

Input a string: abc
Reverse of the string: cba