C program for palindrome or palindrome in c programming: palindrome 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 “a”, dad”, “radar”, “madam”, “abcba” etc.

[ad type=”banner”]

C program for palindrome

[pastacode lang=”c” manual=”%23include%20%3Cstdio.h%3E%0A%23include%20%3Cstring.h%3E%0A%20%0Aint%20main()%0A%7B%0A%20%20%20char%20a%5B100%5D%2C%20b%5B100%5D%3B%0A%20%0A%20%20%20printf(%22Enter%20the%20string%20to%20check%20if%20it%20is%20a%20palindrome%5Cn%22)%3B%0A%20%20%20gets(a)%3B%0A%20%0A%20%20%20strcpy(b%2Ca)%3B%0A%20%20%20strrev(b)%3B%0A%20%0A%20%20%20if%20(strcmp(a%2Cb)%20%3D%3D%200)%0A%20%20%20%20%20%20printf(%22Entered%20string%20is%20a%20palindrome.%5Cn%22)%3B%0A%20%20%20else%0A%20%20%20%20%20%20printf(%22Entered%20string%20is%20not%20a%20palindrome.%5Cn%22)%3B%0A%20%0A%20%20%20return%200%3B%0A%7D” message=”” highlight=”” provider=”manual”/]

Output of program:

C program for palindrome

Palindrome number in c

[ad type=”banner”] [pastacode lang=”c” manual=”%23include%20%3Cstdio.h%3E%0A%20%0Amain()%0A%7B%0A%20%20%20int%20n%2C%20reverse%20%3D%200%2C%20temp%3B%0A%20%0A%20%20%20printf(%22Enter%20a%20number%20to%20check%20if%20it%20is%20a%20palindrome%20or%20not%5Cn%22)%3B%0A%20%20%20scanf(%22%25d%22%2C%26n)%3B%0A%20%0A%20%20%20temp%20%3D%20n%3B%0A%20%0A%20%20%20while%20(temp%20!%3D%200)%0A%20%20%20%7B%0A%20%20%20%20%20%20reverse%20%3D%20reverse%20*%2010%3B%0A%20%20%20%20%20%20reverse%20%3D%20reverse%20%2B%20temp%2510%3B%0A%20%20%20%20%20%20temp%20%20%20%20%3D%20temp%2F10%3B%0A%20%20%20%7D%0A%20%0A%20%20%20if%20(n%20%3D%3D%20reverse)%0A%20%20%20%20%20%20printf(%22%25d%20is%20a%20palindrome%20number.%5Cn%22%2C%20n)%3B%0A%20%20%20else%0A%20%20%20%20%20%20printf(%22%25d%20is%20not%20a%20palindrome%20number.%5Cn%22%2C%20n)%3B%0A%20%0A%20%20%20return%200%3B%0A%7D” message=”” highlight=”” provider=”manual”/]

C program for palindrome without using string functions

[pastacode lang=”c” manual=”%23include%20%3Cstdio.h%3E%0A%23include%20%3Cstring.h%3E%0A%20%0Aint%20main()%0A%7B%0A%20%20%20char%20text%5B100%5D%3B%0A%20%20%20int%20begin%2C%20middle%2C%20end%2C%20length%20%3D%200%3B%0A%20%0A%20%20%20gets(text)%3B%0A%20%0A%20%20%20while%20(text%5Blength%5D%20!%3D%20’%5C0′)%0A%20%20%20%20%20%20length%2B%2B%3B%0A%20%0A%20%20%20end%20%3D%20length%20-%201%3B%0A%20%20%20middle%20%3D%20length%2F2%3B%0A%20%0A%20%20%20for%20(begin%20%3D%200%3B%20begin%20%3C%20middle%3B%20begin%2B%2B)%0A%20%20%20%7B%0A%20%20%20%20%20%20if%20(text%5Bbegin%5D%20!%3D%20text%5Bend%5D)%0A%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20printf(%22Not%20a%20palindrome.%5Cn%22)%3B%0A%20%20%20%20%20%20%20%20%20break%3B%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20end–%3B%0A%20%20%20%7D%0A%20%20%20if%20(begin%20%3D%3D%20middle)%0A%20%20%20%20%20%20printf(%22Palindrome.%5Cn%22)%3B%0A%20%0A%20%20%20return%200%3B%0A%7D” message=”” highlight=”” provider=”manual”/]

C program check palindrome

[ad type=”banner”] [pastacode lang=”c” manual=”%23include%20%3Cstdio.h%3E%0A%20%0Aint%20is_palindrome(char*)%3B%0Avoid%20copy_string(char*%2C%20char*)%3B%0Avoid%20reverse_string(char*)%3B%0Aint%20string_length(char*)%3B%0Aint%20compare_string(char*%2C%20char*)%3B%0A%20%0Aint%20main()%0A%7B%0A%20%20%20char%20string%5B100%5D%3B%0A%20%20%20int%20result%3B%0A%20%0A%20%20%20printf(%22Input%20a%20string%5Cn%22)%3B%0A%20%20%20gets(string)%3B%0A%20%0A%20%20%20result%20%3D%20is_palindrome(string)%3B%0A%20%0A%20%20%20if%20(%20result%20%3D%3D%201%20)%0A%20%20%20%20%20%20printf(%22%5C%22%25s%5C%22%20is%20a%20palindrome%20string.%5Cn%22%2C%20string)%3B%0A%20%20%20else%0A%20%20%20%20%20%20printf(%22%5C%22%25s%5C%22%20is%20not%20a%20palindrome%20string.%5Cn%22%2C%20string)%3B%20%0A%20%0A%20%20%20return%200%3B%0A%7D%0A%20%0Aint%20is_palindrome(char%20*string)%0A%7B%0A%20%20%20int%20check%2C%20length%3B%0A%20%20%20char%20*reverse%3B%0A%20%0A%20%20%20length%20%3D%20string_length(string)%3B%20%20%20%20%0A%20%20%20reverse%20%3D%20(char*)malloc(length%2B1)%3B%20%20%20%20%0A%20%0A%20%20%20copy_string(reverse%2C%20string)%3B%0A%20%20%20reverse_string(reverse)%3B%0A%20%0A%20%20%20check%20%3D%20compare_string(string%2C%20reverse)%3B%0A%20%0A%20%20%20free(reverse)%3B%0A%20%0A%20%20%20if%20(%20check%20%3D%3D%200%20)%0A%20%20%20%20%20%20return%201%3B%0A%20%20%20else%0A%20%20%20%20%20%20return%200%3B%0A%7D%0A%20%0Aint%20string_length(char%20*string)%0A%7B%0A%20%20%20int%20length%20%3D%200%3B%20%20%0A%20%0A%20%20%20while(*string)%0A%20%20%20%7B%0A%20%20%20%20%20%20length%2B%2B%3B%0A%20%20%20%20%20%20string%2B%2B%3B%0A%20%20%20%7D%0A%20%0A%20%20%20return%20length%3B%0A%7D%0A%20%0Avoid%20copy_string(char%20*target%2C%20char%20*source)%0A%7B%0A%20%20%20while(*source)%0A%20%20%20%7B%0A%20%20%20%20%20%20*target%20%3D%20*source%3B%0A%20%20%20%20%20%20source%2B%2B%3B%0A%20%20%20%20%20%20target%2B%2B%3B%0A%20%20%20%7D%0A%20%20%20*target%20%3D%20’%5C0’%3B%0A%7D%0A%20%0Avoid%20reverse_string(char%20*string)%20%0A%7B%0A%20%20%20int%20length%2C%20c%3B%0A%20%20%20char%20*begin%2C%20*end%2C%20temp%3B%0A%20%0A%20%20%20length%20%3D%20string_length(string)%3B%0A%20%0A%20%20%20begin%20%3D%20string%3B%0A%20%20%20end%20%3D%20string%3B%0A%20%0A%20%20%20for%20(%20c%20%3D%200%20%3B%20c%20%3C%20(%20length%20-%201%20)%20%3B%20c%2B%2B%20)%0A%20%20%20%20%20%20%20end%2B%2B%3B%0A%20%0A%20%20%20for%20(%20c%20%3D%200%20%3B%20c%20%3C%20length%2F2%20%3B%20c%2B%2B%20)%20%0A%20%20%20%7B%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20temp%20%3D%20*end%3B%0A%20%20%20%20%20%20*end%20%3D%20*begin%3B%0A%20%20%20%20%20%20*begin%20%3D%20temp%3B%0A%20%0A%20%20%20%20%20%20begin%2B%2B%3B%0A%20%20%20%20%20%20end–%3B%0A%20%20%20%7D%0A%7D%0A%20%0Aint%20compare_string(char%20*first%2C%20char%20*second)%0A%7B%0A%20%20%20while(*first%3D%3D*second)%0A%20%20%20%7B%0A%20%20%20%20%20%20if%20(%20*first%20%3D%3D%20’%5C0’%20%7C%7C%20*second%20%3D%3D%20’%5C0’%20)%0A%20%20%20%20%20%20%20%20%20break%3B%0A%20%0A%20%20%20%20%20%20first%2B%2B%3B%0A%20%20%20%20%20%20second%2B%2B%3B%0A%20%20%20%7D%0A%20%20%20if(%20*first%20%3D%3D%20’%5C0’%20%26%26%20*second%20%3D%3D%20’%5C0’%20)%0A%20%20%20%20%20%20return%200%3B%0A%20%20%20else%0A%20%20%20%20%20%20return%20-1%3B%0A%7D” message=”” highlight=”” provider=”manual”/]

Pointers are used in functions, you can develop your code without using pointers.

Categorized in: