Jump to content

Recommended Posts

Posted
//verifica daca un cuvant este sau nu palindrom

#include<iostream.h>

#include<conio.h>

typedef struct elem 

{

	char info[1];

	elem *st,*dr;}

*adr;

char n[25];


void creare(adr &cap,adr &sf)

{

	cout<<"Cuvantul este ";cin>>n;

	int i;

	i=0;

	if(n[i]!='\0')

	{

		adr l=new elem; //se aloca spatiu in Heap

		l->st=l->dr=NULL;

		l->info[0]=n[i];

		l->info[1]='\0';

		i++;

		cap=sf=l;

		while(n[i]!='\0')

		{

			l=new elem;

			l->info[0]=n[i];

			l->info[1]='\0';

			i++;

			l->dr=NULL;

			l->st=sf;

			sf->dr=l;

			sf=l;

		}

	}

}

//se porneste in ambele sensuri de parcurgere inceput--sfarsit, sfarsit--inceput

int palindrom(adr cap,adr sf)

{

	int stop=0;

	adr l,l1;

	for(l=cap,l1=sf;l && l1 && l!=l1 && !stop;l=l->dr,l1=l1->st)

		if(l->info[0]!=l1->info[0])

			stop=1;

	return(!stop);

}


void main()

{

	adr cap,sf;

	creare(cap,sf);

	if(palindrom(cap,sf)) cout<<n<<" este palindrom."<<endl;

	else cout<<n<<" nu este palindrom."<<endl;

	getch();

}                                  

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.