👤

Se citește de la tastatură un şir de cuvinte separate prin spații, şir care e
terminat cu caracterul EOF. Să se tipărească toate palindroamele care apar în
text. Un palindrom este un cuvânt care e identic citit de la început şi de la
sfârşit. Se cere să se afişeze palindroamele în ordine crescătoare. In limbajul C daca se poate:).


Răspuns :

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

int main()

{ char a[30][30],aux[30],s[30],x,*p;

  int n=0,i,j;

  int ind=scanf("%c",&x);

  while(ind!=EOF)

  {

    s[n++]=x;  ind=scanf("%c",&x);

  }

  s[n-1]='\0';

n=0;

  p=strtok(s," ");

  while(p)

  {

      strcpy(aux,p);

      strrev(aux);

      if(strcmp(aux,p)==0)

       strcpy(a[n++],p);

      p=strtok(NULL," ");

  }

  //am retinut palindroamele intr-o matrice pe linii diferite

  //le sortez

for(i=0;i<n;i++)

     for(j=i+1;j<n;j++)

     if(strcmp(a[i],a[j])>0)

           {

               strcpy(aux,a[i]);strcpy(a[i],a[j]);strcpy(a[j],aux);

           }

   //le afisez

   for(i=0;i<n;i++)

               printf("%s ",a[i]);

   return 0;

}


#include <ctype.h>


#include <stdlib.h>


#include <string.h>


#include <stdio.h>


#include <stdbool.h>



int main() {


   char str[100];  

   int i, j, temp,k=1;


   const char s[] = " \n";


   char *token;


   fgets(str, sizeof(str), stdin);


   token = strtok(str, s);


   while (token != NULL) {


   


       i = 0;


       j = strlen(token) - 1;


       bool palindrome = true;


       while (i < j) {


           if (tolower(token[i]) != tolower(token[j])) {


               palindrome = false;


               break;


           }


           i++;


           j--;


       }



       if (palindrome) {


           


           printf("\n%d) %s",k,token);


           k++;


       }



       token = strtok(NULL, s);


   }


   return 0;


}

Vezi imaginea SIRDUMITRU