{"id":4444,"date":"2026-11-22T11:56:14","date_gmt":"2026-11-22T11:56:14","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=4444"},"modified":"2026-11-22T11:56:14","modified_gmt":"2026-11-22T11:56:14","slug":"reverse-a-string-in-c-using-strrev-function","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/reverse-a-string-in-c-using-strrev-function\/","title":{"rendered":"Reverse a String in C Using strrev() (and Without It)"},"content":{"rendered":"<p>To reverse a string in C, many textbooks use the <code>strrev()<\/code> function from <code>&lt;string.h&gt;<\/code>. It is quick, but there is an important catch: <strong><code>strrev()<\/code> is not part of standard C<\/strong>. It works on Turbo C and on Microsoft&#8217;s compiler, but GCC on Linux and most online compilers do not have it. This page shows the <code>strrev()<\/code> method and a portable method that works everywhere.<\/p>\n<h2>Reverse a String Using strrev()<\/h2>\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n#include &lt;string.h&gt;\n\nint main(void) {\n    char str[] = \"GyaniPandit\";\n\n    strrev(str);\n    printf(\"Reversed string: %sn\", str);\n    return 0;\n}<\/code><\/pre>\n<p><strong>Output<\/strong> (on compilers that provide <code>strrev<\/code>):<\/p>\n<pre class=\"wp-block-code\"><code>Reversed string: tidnaPinayG<\/code><\/pre>\n<p><code>strrev()<\/code> reverses the string <em>in place<\/em>, meaning it changes the original array. It also returns a pointer to the same string.<\/p>\n<p>On GCC (Linux, macOS, most online compilers) this program does not build. You will see a warning about an implicit declaration of <code>strrev<\/code> followed by a linker error such as &#8220;undefined reference to `strrev`&#8221;. On Microsoft&#8217;s compiler the function is also available as <code>_strrev()<\/code>.<\/p>\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\">\n<div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Reverse a String in C Using Strrev Function\" width=\"1200\" height=\"675\" src=\"https:\/\/www.youtube.com\/embed\/OUMERVNBZ20?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe>\n<\/div>\n<\/figure>\n<h2>Reverse a String Without strrev() (Works Everywhere)<\/h2>\n<p>Swap the first and last characters, then the second and second-last, and so on until you reach the middle:<\/p>\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n#include &lt;string.h&gt;\n\nvoid reverse(char *s) {\n    int i = 0;\n    int j = (int)strlen(s) - 1;\n\n    while (i &lt; j) {\n        char temp = s[i];\n        s[i] = s[j];\n        s[j] = temp;\n        i++;\n        j--;\n    }\n}\n\nint main(void) {\n    char str[] = \"GyaniPandit\";\n\n    reverse(str);\n    printf(\"Reversed string: %sn\", str);\n    return 0;\n}<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>Reversed string: tidnaPinayG<\/code><\/pre>\n<h3>How it works<\/h3>\n<ol>\n<li><code>i<\/code> starts at the first character and <code>j<\/code> at the last one (<code>strlen(s) - 1<\/code>).<\/li>\n<li>The two characters are swapped using a temporary variable.<\/li>\n<li><code>i<\/code> moves right and <code>j<\/code> moves left. The loop stops when they meet in the middle, so each pair is swapped only once.<\/li>\n<\/ol>\n<p>For a string of length <em>n<\/em>, this does about <em>n<\/em>\/2 swaps, so it runs in O(<em>n<\/em>) time and needs no extra array.<\/p>\n<h2>Reverse a String Entered by the User<\/h2>\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n#include &lt;string.h&gt;\n\nvoid reverse(char *s) {\n    int i = 0;\n    int j = (int)strlen(s) - 1;\n    while (i &lt; j) {\n        char temp = s[i];\n        s[i++] = s[j];\n        s[j--] = temp;\n    }\n}\n\nint main(void) {\n    char str[100];\n\n    printf(\"Enter a string: \");\n    fgets(str, sizeof str, stdin);\n    str[strcspn(str, \"n\")] = '\u0000';   \/* remove the newline fgets keeps *\/\n\n    reverse(str);\n    printf(\"Reversed string: %sn\", str);\n    return 0;\n}<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>Enter a string: hello world\nReversed string: dlrow olleh<\/code><\/pre>\n<p><code>fgets()<\/code> is used instead of <code>gets()<\/code> because <code>gets()<\/code> cannot limit the input size and was removed from the C standard in C11.<\/p>\n<h2>Use It: Check Whether a String Is a Palindrome<\/h2>\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n#include &lt;string.h&gt;\n\nint main(void) {\n    char word[] = \"madam\";\n    char copy[sizeof word];\n\n    strcpy(copy, word);\n    \/* reverse the copy *\/\n    for (int i = 0, j = (int)strlen(copy) - 1; i &lt; j; i++, j--) {\n        char t = copy[i];\n        copy[i] = copy[j];\n        copy[j] = t;\n    }\n\n    if (strcmp(word, copy) == 0)\n        printf(\"%s is a palindromen\", word);\n    else\n        printf(\"%s is not a palindromen\", word);\n    return 0;\n}<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>madam is a palindrome<\/code><\/pre>\n<h2>Common Mistakes<\/h2>\n<ul>\n<li><strong>Reversing a string literal through a pointer:<\/strong> <code>char *s = \"hello\"; strrev(s);<\/code> tries to modify read-only memory and can crash. Use an array: <code>char s[] = \"hello\";<\/code><\/li>\n<li><strong>Including the <code>'\u0000'<\/code> in the swap:<\/strong> start <code>j<\/code> at <code>strlen(s) - 1<\/code>, not <code>strlen(s)<\/code>, or the terminator moves to the front and the string looks empty.<\/li>\n<li><strong>Forgetting the newline from <code>fgets()<\/code>:<\/strong> it would become the first character after reversing.<\/li>\n<\/ul>\n<h2>Frequently Asked Questions<\/h2>\n<h3>Is strrev() a standard C function?<\/h3>\n<p>No. It is a non-standard extension found in Turbo C and Microsoft&#8217;s C library (as <code>_strrev<\/code>). Standard C and GCC do not provide it.<\/p>\n<h3>Which header file has strrev()?<\/h3>\n<p><code>&lt;string.h&gt;<\/code>, on the compilers that support it.<\/p>\n<h3>How do I reverse a string in C without any library function?<\/h3>\n<p>Find the length with a loop, then swap characters from both ends towards the middle, as in the <code>reverse()<\/code> function above.<\/p>\n<p>Related: <a href=\"https:\/\/gyanipandit.com\/programming\/how-to-find-ascii-value-in-c\/\">How to find ASCII value in C<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>To reverse a string in C, many textbooks use the strrev() function from &lt;string.h&gt;. It is quick, but there is an important catch: strrev() is not part of standard C. It works on Turbo C and on Microsoft&#8217;s compiler, but GCC on Linux and most online compilers do not have it. This page shows the &#8230; <a title=\"Reverse a String in C Using strrev() (and Without It)\" class=\"read-more\" href=\"https:\/\/gyanipandit.com\/programming\/reverse-a-string-in-c-using-strrev-function\/\" aria-label=\"Read more about Reverse a String in C Using strrev() (and Without It)\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":4446,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30],"tags":[29,76],"class_list":["post-4444","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-program","tag-c-program","tag-reverse-a-string-in-c-using-strrev-function"],"_links":{"self":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/4444","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/comments?post=4444"}],"version-history":[{"count":2,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/4444\/revisions"}],"predecessor-version":[{"id":6153,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/4444\/revisions\/6153"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media\/4446"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=4444"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=4444"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=4444"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}