{"id":26448,"date":"2017-05-31T17:48:47","date_gmt":"2017-05-31T12:18:47","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26448"},"modified":"2017-05-31T17:48:47","modified_gmt":"2017-05-31T12:18:47","slug":"reverse-string-in-c","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/reverse-string-in-c\/","title":{"rendered":"Reverse string in C"},"content":{"rendered":"<p>This program Reverse string in C entered by the user. For example if a user enters a string &#8220;<a href=\"https:\/\/www.wikitechy.com\/technology\/write-a-program-to-calculate-pow-xn\/\">reverse <\/a>me&#8221; then on reversing the string will be &#8220;em esrever&#8221;. We show you four different methods to reverse string the first one uses strrev library function of string.h header file, second without using strrev and in third we make our own function to reverse string using pointers, reverse string using recursion and Reverse words in string. If you are using first method then you must include string.h in your program.<\/p>\n[ad type=&#8221;banner&#8221;]\n<h2 id=\"c-programming-code\">C programming code<\/h2>\n<p>\/* String reverse in c*\/<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">#include &lt;stdio.h&gt;<br\/>#include &lt;string.h&gt;<br\/> <br\/>int main()<br\/>{<br\/>   char arr[100];<br\/> <br\/>   printf(&quot;Enter a string to reverse\\n&quot;);<br\/>   gets(arr);<br\/> <br\/>   strrev(arr);<br\/> <br\/>   printf(&quot;Reverse of entered string is \\n%s\\n&quot;,arr);<br\/> <br\/>   return 0;<br\/>}<\/code><\/pre> <\/div>\n<p><strong>Output of program:<\/strong><\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-full wp-image-26499\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/reverse-string-c.png\" alt=\"Reverse string in C\" width=\"429\" height=\"129\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/reverse-string-c.png 429w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/reverse-string-c-300x90.png 300w\" sizes=\"(max-width: 429px) 100vw, 429px\" \/><\/p>\n<h2 id=\"reverse-string-without-function\"><span id=\"c-program-to-reverse-string-without-using-function\">C program to reverse string without using function<\/span><\/h2>\n<p>Below code does not uses strrev library function to Reverse string in C. First we calculate the length of string using strlen function and then assigns characters of input string in reverse order to create a new string using a for loop. You can also calculate length of string without using strlen. We use comma operator in for loop to initialize multiple variables and increment\/decrement variables.<\/p>\n[ad type=&#8221;banner&#8221;]\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">#include &lt;stdio.h&gt;<br\/>#include &lt;string.h&gt;<br\/> <br\/>int main()<br\/>{<br\/>   char s[100], r[100];<br\/>   int n, c, d;<br\/> <br\/>   printf(&quot;Input a string\\n&quot;);<br\/>   gets(s);<br\/> <br\/>   n = strlen(s);<br\/> <br\/>   for (c = n - 1, d = 0; c &gt;= 0; c--, d++)<br\/>      r[d] = s[c];<br\/> <br\/>   r[d] = &#039;\\0&#039;;<br\/> <br\/>   printf(&quot;%s\\n&quot;, r);<br\/> <br\/>   return 0;<br\/>}<\/code><\/pre> <\/div>\n<h2 id=\"reverse-string-using-pointers\"><span id=\"c-program-to-reverse-a-string-using-pointers\">C program to reverse a string using pointers<\/span><\/h2>\n<p>Now we will invert string using pointers or without using library function strrev.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">#include&lt;stdio.h&gt;<br\/> <br\/>int string_length(char*);<br\/>void reverse(char*);<br\/> <br\/>main() <br\/>{<br\/>   char string[100];<br\/> <br\/>   printf(&quot;Enter a string\\n&quot;);<br\/>   gets(string);<br\/> <br\/>   reverse(string);<br\/> <br\/>   printf(&quot;Reverse of entered string is \\&quot;%s\\&quot;.\\n&quot;, string);<br\/> <br\/>   return 0;<br\/>}<br\/> <br\/>void reverse(char *string) <br\/>{<br\/>   int length, c;<br\/>   char *begin, *end, temp;<br\/> <br\/>   length = string_length(string);<br\/>   begin  = string;<br\/>   end    = string;<br\/> <br\/>   for (c = 0; c &lt; length - 1; c++)<br\/>      end++;<br\/> <br\/>   for (c = 0; c &lt; length\/2; c++)<br\/>   {        <br\/>      temp   = *end;<br\/>      *end   = *begin;<br\/>      *begin = temp;<br\/> <br\/>      begin++;<br\/>      end--;<br\/>   }<br\/>}<br\/> <br\/>int string_length(char *pointer)<br\/>{<br\/>   int c = 0;<br\/> <br\/>   while( *(pointer + c) != &#039;\\0&#039; )<br\/>      c++;<br\/> <br\/>   return c;<br\/>}<\/code><\/pre> <\/div>\n<h2 id=\"reverse-string-using-recursion\"><span id=\"program-to-reverse-string-in-c-using-recursion\">program to reverse string in C using recursion<\/span><\/h2>\n[ad type=&#8221;banner&#8221;]\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">#include &lt;stdio.h&gt;<br\/>#include &lt;string.h&gt;<br\/> <br\/>void reverse(char*, int, int);<br\/> <br\/>int main()<br\/>{<br\/>   char a[100];<br\/> <br\/>   gets(a);<br\/> <br\/>   reverse(a, 0, strlen(a)-1);<br\/> <br\/>   printf(&quot;%s\\n&quot;,a);<br\/> <br\/>   return 0;<br\/>}<br\/> <br\/>void reverse(char *x, int begin, int end)<br\/>{<br\/>   char c;<br\/> <br\/>   if (begin &gt;= end)<br\/>      return;<br\/> <br\/>   c          = *(x+begin);<br\/>   *(x+begin) = *(x+end);<br\/>   *(x+end)   = c;<br\/> <br\/>   reverse(x, ++begin, --end);<br\/>}<\/code><\/pre> <\/div>\n<p>In recursion method we swap characters at the begin and at the end and then move towards the middle of the string. This method is inefficient due to repeated function calls but useful in practicing recursion.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Reverse string in C &#8211; C Programming &#8211; We show you four different methods to reverse string the first one uses strrev library function of string.h <\/p>\n","protected":false},"author":2,"featured_media":26509,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69866],"tags":[71954,78988,79004,79005,78981,78982,78983,79007,79006,79001,78998,78985,78996,78977,79003,78990,78984,78978,74742,78989,79009,79002,78997,78994,79000,78987,79008,78991,78992,78995,78986,78999,79010,78993,78979,78980],"class_list":["post-26448","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-c-program-reverse-string","tag-c-reverse-string","tag-code-reverse-string","tag-excel-reverse-string","tag-how-to-reverse-a-string-in-java-without-using-reverse-function","tag-how-to-reverse-a-string-in-java-word-by-word","tag-how-to-reverse-a-string-python","tag-java-program-reverse-string","tag-java-program-to-reverse-string","tag-java-reverse-string","tag-java-reverse-string-program","tag-javascript-reverse-string","tag-online-reverse-string","tag-program-to-reverse-a-string-in-c","tag-python-program-for-reverse-string","tag-python-reverse-string","tag-reverse-a-number-in-java","tag-reverse-a-string-in-c","tag-reverse-string","tag-reverse-string-c","tag-reverse-string-c-code","tag-reverse-string-code","tag-reverse-string-function","tag-reverse-string-in-c","tag-reverse-string-in-java","tag-reverse-string-java","tag-reverse-string-java-program","tag-reverse-string-javascript","tag-reverse-string-online","tag-reverse-string-program","tag-reverse-string-python","tag-reverse-string-sql","tag-reverse-string-word-by-word","tag-sql-reverse-string","tag-string-reverse-function-in-java","tag-string-reverse-in-c"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26448","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=26448"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26448\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media\/26509"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26448"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26448"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26448"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}