Jump to content

Tutorial Palindrom Pentru Sir De Caractere In C++


Gabriel™

Recommended Posts

1.

unsigned int palindrom (char s[100])
{ unsigned int i;
for (i=0; i<= strlen(s)/2-1; i++) if(s[i]!=s[strlen(s)-i-1] return 0;
return 11.
unsigned int palindrom (char s[100])
{ unsigned int i;
for (i=0; i<= strlen(s)/2-1; i++) if(s[i]!=s[strlen(s)-i-1] return 0;
return 1;}1.
unsigned int palindrom (char s[100])
{ unsigned int i;
for (i=0; i<= strlen(s)/2-1; i++) if(s[i]!=s[strlen(s)-i-1] return 0;
return 1;}

Cand va fi apelata functia "palindrom" va verifica daca sirul s este palindrom verificant codurile ASCII ale caracterelor de pe pozitii simetrice. Daca conditia din if se adevereste atunci va returna 0 (orice functie se opreste la prima instructiune return pe care o executa) sau 1 daca nu executa primul return.
In logica, 0 = fals si 1= adevarat. Acest subprogram poate fi folosit mai tarziu intr-o instructiune de tipul:

if(palindrom(s)==1) cout<<"ESTE PALINDROM";
else cout<<"NU ESTE PALINDROM";

2.

unsigned int palindrom (char s[100])
{unsigned int i, j, k;
i=0; j=strlen(s)-1; k=0'
while(i<j) { if(s[i]==s[j]) k=k+1;
i=i+1; j=j-1;}
if (k==(strlen(s)-1)/2) return 1 SAU cout<<"PALINDROM" SAU return k;
else return 0 SAU cout<<"NU ESTE PALINDROM";

Aici va verifica cate caractere de pe pozitii simetrice sunt identice. k ne arata cate caractere sunt identice. Daca toate sunt identice, k va fi egal cu jumate din lungimea sirului si ne arata ca e palindrom. (idee proprie).

 

 

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • 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.