palindrome program

4
Untitled http://www.mycfiles.com/2011/02/palindrome-program-in-c.html //Program for palindrome #include<stdio.h> #include<conio.h> #include<string.h> void main() { char s[20]; int i,j,l; clrscr(); printf("Enter name\n\n"); gets(s); l=strlen(s); for (i=0,j=l-1;i<j;i++,j--) { if (s[i]!=s[j]) { printf("\nNot a Palindrome"); break; } else printf("\nPalindrome"); } getch(); } ------------------------------- http://www.programmingsimplified.com/c-program-find-palindrome C program for palindrome #include<stdio.h> #include<conio.h> #include<string.h> main() { char a[100], b[100]; printf("Enter the string to check if it is a palindrome\n"); gets(a); strcpy(b,a); strrev(b); if( strcmp(a,b) == 0 ) printf("Entered string is a palindrome.\n"); else printf("Entered string is not a pailndrome.\n"); getch(); return 0; } Page 1

Upload: vaibhav-baluni

Post on 05-Feb-2016

6 views

Category:

Documents


0 download

DESCRIPTION

program

TRANSCRIPT

Page 1: Palindrome program

Untitledhttp://www.mycfiles.com/2011/02/palindrome-program-in-c.html

//Program for palindrome#include<stdio.h>#include<conio.h>#include<string.h>void main(){char s[20];int i,j,l;clrscr();printf("Enter name\n\n");gets(s);l=strlen(s);for (i=0,j=l-1;i<j;i++,j--){if (s[i]!=s[j]){printf("\nNot a Palindrome");break;}elseprintf("\nPalindrome");}getch();}

-------------------------------http://www.programmingsimplified.com/c-program-find-palindrome

C program for palindrome

#include<stdio.h>#include<conio.h>#include<string.h> main(){ char a[100], b[100]; printf("Enter the string to check if it is a palindrome\n"); gets(a); strcpy(b,a); strrev(b); if( strcmp(a,b) == 0 ) printf("Entered string is a palindrome.\n"); else printf("Entered string is not a pailndrome.\n"); getch(); return 0;}

Page 1

Page 2: Palindrome program

Untitled

Palindrome number in c

#include<stdio.h> main(){ int n, reverse = 0, temp; printf("Enter a number to check if it is a palindrome or not\n"); scanf("%d",&n); temp = n; while( temp != 0 ) { reverse = reverse * 10; reverse = reverse + temp%10; temp = temp/10; } if ( n == reverse ) printf("%d is a palindrome number.\n", n); else printf("%d is not a palindrome number.\n", n); return 0;}

C program check palindrome

#include<stdio.h> int is_palindrome(char*);void copy_string(char*, char*);void reverse_string(char*);int string_length(char*);int compare_string(char*, char*); main(){ char string[100]; int result; printf("Enter a string\n"); gets(string); result = is_palindrome(string);

Page 2

Page 3: Palindrome program

Untitled if ( result == 1 ) printf("\"%s\" is a palindrome string.\n", string); else printf("\"%s\" is not a palindrome string.\n", string); return 0;} int is_palindrome(char *string){ int check, length; char *reverse; length = string_length(string); reverse = (char*)malloc(length+1); copy_string(reverse, string); reverse_string(reverse); check = compare_string(string, reverse); free(reverse); if ( check == 0 ) return 1; else return 0;} int string_length(char *string){ int length = 0; while(*string) { length++; string++; } return length;} void copy_string(char *target, char *source){ while(*source) { *target = *source; source++; target++; } *target = '\0';} void reverse_string(char *string) { int length, c;

Page 3

Page 4: Palindrome program

Untitled char *begin, *end, temp; length = string_length(string); begin = string; end = string; for ( c = 0 ; c < ( length - 1 ) ; c++ ) end++; for ( c = 0 ; c < length/2 ; c++ ) { temp = *end; *end = *begin; *begin = temp; begin++; end--; }} int compare_string(char *first, char *second){ while(*first) { if (*first == *second) { first++; second++; } else return -1; } return 0; }

--------------------------------------

Page 4