{"id":26517,"date":"2017-05-31T18:06:19","date_gmt":"2017-05-31T12:36:19","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26517"},"modified":"2017-05-31T18:06:19","modified_gmt":"2017-05-31T12:36:19","slug":"c-program-for-palindrome","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-program-for-palindrome\/","title":{"rendered":"C program for palindrome"},"content":{"rendered":"<p>C program for palindrome or palindrome in c programming: <a href=\"https:\/\/www.wikitechy.com\/technology\/closest-pair-of-points\/\">palindrome <\/a>program in c language, c code to check if a string is a palindrome or not and for palindrome number. This program works as follows :- at first we copy the entered string into a new string, and then we reverse the new string and then compares it with original string. If both of them have same sequence of characters i.e. they are identical then the entered string is a palindrome otherwise not. To perform copy, reverse and compare operations we use strcpy, strrev and strcmp functions of string.h respectively, if you do not wish to use these functions see c programming code for palindrome without using string functions. Some palindrome strings examples are &#8220;a&#8221;, dad&#8221;, &#8220;radar&#8221;, &#8220;madam&#8221;, &#8220;abcba&#8221; etc.<\/p>\n[ad type=&#8221;banner&#8221;]\n<h2 id=\"c-program-for-palindrome\">C program for palindrome<\/h2>\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 a[100], b[100];<br\/> <br\/>   printf(&quot;Enter the string to check if it is a palindrome\\n&quot;);<br\/>   gets(a);<br\/> <br\/>   strcpy(b,a);<br\/>   strrev(b);<br\/> <br\/>   if (strcmp(a,b) == 0)<br\/>      printf(&quot;Entered string is a palindrome.\\n&quot;);<br\/>   else<br\/>      printf(&quot;Entered string is not a palindrome.\\n&quot;);<br\/> <br\/>   return 0;<br\/>}<\/code><\/pre> <\/div>\n<p><strong>Output of program:<\/strong><\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-26522\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/palindrome-c.png\" alt=\"C program for palindrome\" width=\"421\" height=\"115\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/palindrome-c.png 421w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/palindrome-c-300x82.png 300w\" sizes=\"(max-width: 421px) 100vw, 421px\" \/><\/p>\n<h2 id=\"palindrome_number\"><span id=\"palindrome-number-in-c\">Palindrome number in c<\/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\/> <br\/>main()<br\/>{<br\/>   int n, reverse = 0, temp;<br\/> <br\/>   printf(&quot;Enter a number to check if it is a palindrome or not\\n&quot;);<br\/>   scanf(&quot;%d&quot;,&amp;n);<br\/> <br\/>   temp = n;<br\/> <br\/>   while (temp != 0)<br\/>   {<br\/>      reverse = reverse * 10;<br\/>      reverse = reverse + temp%10;<br\/>      temp    = temp\/10;<br\/>   }<br\/> <br\/>   if (n == reverse)<br\/>      printf(&quot;%d is a palindrome number.\\n&quot;, n);<br\/>   else<br\/>      printf(&quot;%d is not a palindrome number.\\n&quot;, n);<br\/> <br\/>   return 0;<br\/>}<\/code><\/pre> <\/div>\n<h2 id=\"without-string-functions\"><span id=\"c-program-for-palindrome-without-using-string-functions\">C program for palindrome without using string functions<\/span><\/h2>\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 text[100];<br\/>   int begin, middle, end, length = 0;<br\/> <br\/>   gets(text);<br\/> <br\/>   while (text[length] != &#039;\\0&#039;)<br\/>      length++;<br\/> <br\/>   end = length - 1;<br\/>   middle = length\/2;<br\/> <br\/>   for (begin = 0; begin &lt; middle; begin++)<br\/>   {<br\/>      if (text[begin] != text[end])<br\/>      {<br\/>         printf(&quot;Not a palindrome.\\n&quot;);<br\/>         break;<br\/>      }<br\/>      end--;<br\/>   }<br\/>   if (begin == middle)<br\/>      printf(&quot;Palindrome.\\n&quot;);<br\/> <br\/>   return 0;<br\/>}<\/code><\/pre> <\/div>\n<h2 id=\"c-program-check-palindrome\">C program check palindrome<\/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\/> <br\/>int is_palindrome(char*);<br\/>void copy_string(char*, char*);<br\/>void reverse_string(char*);<br\/>int string_length(char*);<br\/>int compare_string(char*, char*);<br\/> <br\/>int main()<br\/>{<br\/>   char string[100];<br\/>   int result;<br\/> <br\/>   printf(&quot;Input a string\\n&quot;);<br\/>   gets(string);<br\/> <br\/>   result = is_palindrome(string);<br\/> <br\/>   if ( result == 1 )<br\/>      printf(&quot;\\&quot;%s\\&quot; is a palindrome string.\\n&quot;, string);<br\/>   else<br\/>      printf(&quot;\\&quot;%s\\&quot; is not a palindrome string.\\n&quot;, string); <br\/> <br\/>   return 0;<br\/>}<br\/> <br\/>int is_palindrome(char *string)<br\/>{<br\/>   int check, length;<br\/>   char *reverse;<br\/> <br\/>   length = string_length(string);    <br\/>   reverse = (char*)malloc(length+1);    <br\/> <br\/>   copy_string(reverse, string);<br\/>   reverse_string(reverse);<br\/> <br\/>   check = compare_string(string, reverse);<br\/> <br\/>   free(reverse);<br\/> <br\/>   if ( check == 0 )<br\/>      return 1;<br\/>   else<br\/>      return 0;<br\/>}<br\/> <br\/>int string_length(char *string)<br\/>{<br\/>   int length = 0;  <br\/> <br\/>   while(*string)<br\/>   {<br\/>      length++;<br\/>      string++;<br\/>   }<br\/> <br\/>   return length;<br\/>}<br\/> <br\/>void copy_string(char *target, char *source)<br\/>{<br\/>   while(*source)<br\/>   {<br\/>      *target = *source;<br\/>      source++;<br\/>      target++;<br\/>   }<br\/>   *target = &#039;\\0&#039;;<br\/>}<br\/> <br\/>void reverse_string(char *string) <br\/>{<br\/>   int length, c;<br\/>   char *begin, *end, temp;<br\/> <br\/>   length = string_length(string);<br\/> <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 compare_string(char *first, char *second)<br\/>{<br\/>   while(*first==*second)<br\/>   {<br\/>      if ( *first == &#039;\\0&#039; || *second == &#039;\\0&#039; )<br\/>         break;<br\/> <br\/>      first++;<br\/>      second++;<br\/>   }<br\/>   if( *first == &#039;\\0&#039; &amp;&amp; *second == &#039;\\0&#039; )<br\/>      return 0;<br\/>   else<br\/>      return -1;<br\/>}<\/code><\/pre> <\/div>\n<p>Pointers are used in functions, you can develop your code without using pointers.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>C program for palindrome &#8211; C PRogramming &#8211; palindrome program in c language, c code to check if a string is a palindrome or not and for palindrome number. <\/p>\n","protected":false},"author":2,"featured_media":26532,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69866],"tags":[72269,72112,71969,71982,79053,79051,79049,71981,79052,79054,79047,79046,79045,79041,79044,79043,79042,79050,79048],"class_list":["post-26517","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-c-program-for-armstrong-number","tag-c-program-for-palindrome","tag-c-program-for-palindrome-number","tag-c-program-for-palindrome-number-using-for-loop","tag-c-program-for-palindrome-number-using-functions","tag-c-program-for-palindrome-or-not","tag-c-program-for-palindrome-string","tag-c-program-for-palindrome-using-for-loop","tag-c-program-for-palindrome-with-explanation","tag-c-program-for-palindrome-word","tag-c-program-to-check-palindrome-number-using-functions","tag-palindrome-program-in-c-using-for-loop","tag-palindrome-program-in-c-with-explanation","tag-palindrome-program-in-c-without-using-string-functions","tag-palindrome-program-in-c","tag-palindrome-string-program-in-c-using-for-loop","tag-program-for-palindrome-in-java","tag-simple-c-program-for-palindrome","tag-write-ac-program-for-palindrome"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26517","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=26517"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26517\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media\/26532"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26517"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26517"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26517"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}