English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

program in c code plz

2007-03-01 00:53:04 · 4 answers · asked by Aritra Paul 1 in Computers & Internet Programming & Design

4 answers

Using built-in functions :
**************************
#include
#include
#define size 26

void main()
{
char strsrc[size];
char strtmp[size];

clrscr();
printf("\n Enter String:= "); gets(strsrc);

strcpy(strtmp,strsrc);
strrev(strtmp);

if(strcmp(strsrc,strtmp)==0)
printf("\n Entered string \"%s\" ispalindrome",strsrc);
else
printf("\n Entered string \"%s\" is not
palindrome",strsrc);
getch();
}

********************
Without using built-in (Write for string length)

**************
main()
{
char original[20],reversed[20];
int length.flag=0,i;

Printf("Enter string");

gets(original);


length=strlen(original);
// or write code to find length

for (i=0;i<=length-1;i++)
{
reversed[length-1-i]=original[i];
}

reversed[length]='\0';

for (i=0;i<=length/2;i++)
{
if(reversed[i]!=original[i])
{
flag=1;
break;
}
}

if(flag==1)
printf("NO");
else
printf("YES");

}

2007-03-01 01:26:37 · answer #1 · answered by PC 4 · 0 0

you set up an array, fill the array, and output the array backwards into another array. then, inside a loop, compare each unit in the array. pretty simple


yahoo answers will not do your homework for you. we can help you understand it though.

2007-03-01 09:00:22 · answer #2 · answered by The_Amish 5 · 0 0

#include
#define MAX 50
void main()
{
char a[MAX],b[MAX];
int i,len=0,n,j=0;
printf("enter the number of characters you want to print:");
scanf("%d",&n);
printf("enter the string:");
for(i=0;i {
scanf("%c",&a[i]);
len=len+1;
}
for(i=len;i>0;i--)
{

b[j] =a[i];
j++;
}
for(i=0;i {
if(b[i]==a[i])
{
printf("the string is a palindrome");
}
else
printf("string is not a palindrome");
}
}

2007-03-01 09:37:42 · answer #3 · answered by mokariya n 1 · 0 0

//EFFICIENT WAY OF CHECKING FOR PALINDROME
//USING STACK DATA STRUCTURES

#include
#include
#include
#include
typedef struct stack
{
char c;
struct stack *link;
}S;
static int length;

void push(S** top,char ch)
{
S* temp=(S*)malloc(sizeof(S));
temp->c=ch;temp->link=NULL;
if(*top==NULL)
(*top)=temp;

else
{
temp->link=(*top);
(*top)=temp;
}
length++;
}

char pop(S** top)
{
if((*top)==NULL)
{
printf("STack empty");
return NULL;
}
S* temp=(*top);
char ch=temp->c;
(*top)=(*top)->link;
free(temp);
return ch;
}

void inserttostack(S** top,char str[])
{
for(int i=0;i push(top,str[i]);
}
void isitpalindrome(S** top,char str[])
{
for(int i=0;i if(str[i]!=pop(top))
{
printf("\n%s:Not Palindrome",str);
return;
}
if(length%2==0)
printf("\n%s: Even Palindrome",str);
else
printf("\n%s: Odd palindrome",str);
}
void display(S* top)
{
while(top!=NULL)
{
printf("%c",top->c);
top=top->link;
}
}

void main()
{
S *top=NULL; char str[20];
printf("Enter a string:");
gets(str);
inserttostack(&top,str);
printf("\n\nstack length: %d\nSTACK: ",length);display(top);
isitpalindrome(&top,str);
}

//ORDINARY CHILDISH WAY

#include
#include
#include

void main()
{
char str[20]; clrscr();
printf("\n\nEnter a string:");
gets(str);
if(strcmp(str,strrev(str))==0 && strlen(str)%2==0) //checking for the termination condition
printf("\n%s:Even Palindrome",str);
else if(strcmp(str,strrev(str))==0 && strlen(str)%2==1)
printf("\n%s:Odd Palindrome",str);
else
printf("\n%s:Not Palindrome",str);
}

2007-03-01 13:27:38 · answer #4 · answered by KillingJoke 3 · 0 0

fedest.com, questions and answers