Function to check palindrome in C

Asked

Viewed 85 times

-2

I’m trying to do a function with only these two libraries and without changing the main function check that the word is palindromic.

She must have the following exit:

radar eh palindromo!
rada nao eh palindromo!

But I tried some things and I can not understand the logic. Below the code:

#include <stdio.h>
#include <stdlib.h>

int palindromo(char* texto, int n) {

  */completar aqui/*

  return 1;
}

int main() {
  char* p1 = (char*) malloc(sizeof(char)*5);
  p1[0] = 'r';
  p1[1] = 'a';
  p1[2] = 'd';
  p1[3] = 'a';
  p1[4] = 'r';

  if(palindromo(p1,5)){
    printf("radar eh palindromo!\n");
  }else{
    printf("radar nao eh palindromo!\n");
  }

  if(palindromo(p1,4)){
    printf("rada eh palindromo!\n");
  }else{
    printf("rada nao eh palindromo!\n");
  }

  
  return 0;
}

If anyone can help me, I’d really appreciate it.

  • 3

    At the end you just wrote a main to test a function you haven’t written and are asking for help. Sorry, but Porfavornosajude to help you. Follow a site search where several people have asked about palindromes with the tag [tag:c].

  • I wrote yes, it just didn’t work, so I left the "complete here" to be clearer where I should move. I’m tired of asking here on this site and seeing people like you who would rather lecture than teach to do, just like the comment from below did and was very professional and helpful. Not everyone already has the vast knowledge of programming that most people here do. @fernandosavio

  • 2

    I really understand that you must be tired of asking here and your questions being closed, I don’t always agree with closures. But that doesn’t change the fact that your question doesn’t have any code that you tried. I can’t read your question and guess that you tried something and what didn’t work out in what you tried. And the site is full of people who just post a college exam question and expect some volunteer come answer. So it’s not a moral lesson, but an ear-jerk because if you did it in real life it would also catch up badly.

  • 1

    Even if it didn’t work, putting the code you tried is better than not putting - because at least there we can see where the difficulty is and doesn’t give the impression that nothing has been done and you just want everything ready (the "complete here" passes just this impression). This also helps to avoid closing, since questions like "do it for me" are outside the scope. I suggest a read on Checklist, in the FAQ and in the Guidebook

  • Yes, it’s a lot to read, but I assure you that if you understand and follow everything there, you will enjoy the site better :-)

1 answer

-1

First, the ideal is to understand the problem and then solve it with code.

We know that a palindrome is a sequence of letters that, even reversed, continues with the same "meaning" (represents the same thing or has the same meaning). Knowing this, we can think of comparing the same inverted text as we see letter to letter in function palindromo. Code is an example of implementation:

#include <stdio.h>
#include <stdlib.h>

int palindromo(char* texto, int tamanho_string) 
{
  int i = 0;
  // Com a função "strlen(texto)" teríamos o tamanho da string.
  // Mas dado que seu problema envolve passar o tamanho da string como argumento,
  // vamos usar um "int fixo". Usamos "- 1" para remover o caractere que representa
  // o final de uma string ('\0'):
  int j = tamanho_string - 1;
  
  while (texto[i] != '\0')
  {
    if (texto[i] != texto[j])
    {
        return 0;
    }
    
    j--;
    i++;
  }

  return 1;
}

int main() 
{ 
  char* p1 = (char*) malloc(sizeof(char)*5);

  p1[0] = 'r';
  p1[1] = 'a';
  p1[2] = 'd';
  p1[3] = 'a';
  p1[4] = 'r';

  if (palindromo(p1, 5))
  {
    printf("radar eh palindromo!\n");
  } 
  else 
  {
    printf("radar nao eh palindromo!\n");
  }

  if (palindromo(p1, 4))
  {
    printf("rada eh palindromo!\n");
  } 
  else 
  {
    printf("rada nao eh palindromo!\n");
  }

  return 0;
}

See that in the function palindromo we traverse, in a single loop of repetition, both sequences. If any letter is not equal even with the inverted sequence, we will know that the word is not a palindrome.

When compiling and executing the code above, we have the output:

radar eh palindromo!
rada nao eh palindromo!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.