Comparing C string contents to find palindrome

Asked

Viewed 2,111 times

1

I need to check whether a string The, for example, is equal to string B to determine if the word inserted is a palindrome, the problem is that a string B is the reverse of A, I’m not able to determine if the contents are equal. For example, I enter the word "macaw", the program should show a message that the word is a palindrome, instead it shows the message that says the word is not a palindrome.

#define N 20

int main()
{
    char palavra_a[N], palavra_b[N];
    int i,j=0,flag=0;

    puts("\nInsira uma palavra: \n");//pegando a palavra
    fflush(stdin);
    gets(palavra_a);

    for(i=strlen(palavra_a);i>0;i--)
    {
        palavra_b[j]=palavra_a[i];
        j=j+1;
    }

    for(i=0;i<strlen(palavra_a)-1;i++)
    {
        if(palavra_a[i]==palavra_b[i])
        {
            flag=0;
            printf("%c",palavra_b[i]);
        }
        else
        if(palavra_a[i]!=palavra_b[i])
        {
            flag=1;
            break;//caso a letra da palavra b seja diferente da palavra a, o laço é quebrado
        }

    }
    printf("\n  %s",palavra_b[N]);
    switch (flag)
    {
        case 0:
            puts("\nA palavra inserida eh um palindromo.");
        break;

        case 1:
            puts("\nA palavra inserida nao eh um palindromo.");
        break;
    }

    system("Pause");
    return 0;
    }
  • use the function strcmp, in case you haven’t loaded it, do #include <string.h>

  • but I need to copy backwards to check if the word is palindroma

  • what he meant is after copying it backwards, use only strcmp() to compare. strcmp(palavra_a, palavra_b)

  • 1

    By your code you see something different, because by what you’re now saying in case it would be, uncouple, reverse, regroup and finally compare.

3 answers

7


This code is too complex, simplifying it is much easier to understand. Just compare the first with the last, the second with the penultimate character and so on. It only needs to go half as long as the other half has already been compared together.

#include <stdio.h>
#include <string.h>
#define N 20

int main() {
    char palavra[N];
    printf("Insira uma palavra: ");
    scanf("%s", palavra);
    int tamanho = strlen(palavra);
    for (int i = 0; i < tamanho / 2; i++) { //só irá até o meio
        if (palavra[i] != palavra[tamanho - i - 1]) {
            printf("\nA palavra inserida nao eh um palindromo.");
            return 0;
        }
    }
    printf("\nA palavra inserida eh um palindromo.");
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • the code worked fine, but if it’s not asking properly, which I was missing in the other logic?

  • I don’t know, nor did I spend time trying to understand because of the complication, I chose to simplify.

3

  • here you are forgetting the index 0, in this case you are always counting +1 character
  • Voce is also using the strlen, which is a function of the library <string. h>
  • to solve, subtract -1 here also and imports the library using #include <string. h>
for(i=strlen(palavra_a) - 1; i>0; i--)
{
   palavra_b[j]=palavra_a[i];
   j=j+1;
}
  • here, you don’t need elseif,
  • because if two alphabetic characters are not equal, they are different
  • one Else solve this
for(i=0;i<strlen(palavra_a)-1;i++)
{
    if(palavra_a[i]==palavra_b[i])
    {
       flag=0;
       printf("%c",palavra_b[i]);
    } else {
       flag=1;
       break;
    }
}

You can also create a function, but using the same type of loop to reverse the word:

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

int main()
{
    char a[10];

    puts("\nPalavra com 10 caracteres no maximo");
    gets(a);

    if(polindromo(a)){
        puts("sim e polindrome");
    } else {
        puts("nao e polindrome");
    }
    return 0;
}

int polindromo(char *a){
    int i,x;
    for(i = 0,x = strlen(a) - 1; i < x; ++i, --x){
        if(a[i] != a[x]){
            return 0;
        }
    }
    return 1;
}

What happens ?

What function *polyhedron(char Arg) makes, is declare 2 variables x and y, where one of them will have the initial value 0 equivalent to the position of the first character in a string, and the other will have the initial value, the size of the string passed in the argument of that function *Arg least 1 position, because the size is always returned counting from the 1 instead of the 0. Then compare if the smallest variable i is even smaller than smaller than the larger variable x, for the smallest, one makes a increment at the same time as decrease the largest variable. Inside the loop, in the end, i will be the same size as x before the loop ((size de_x) - 1). Therefore, as one index increases and another decreases respectively the characters are compared in reverse order.

Example

Does not match:

char a[6] = anatel

ordem a[i](+)  a[x](-)

1º    a        l
2º    n        e
3º    a        t
4º    t        a
5º    e        n
6º    l        a

Corresponds:

char a[5] = tenet

ordem a[i](+)  a[x](-)

1º    t        t
2º    e        e
3º    n        n
4º    e        e
5º    t        t

Response also in Soen.

1

Good afternoon, see if this helps you:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define N 20

int main(){
    char palavra_a[N], palavra_b[N];
    int i,j=0,flag=0;

    puts("\nInsira uma palavra: \n");//pegando a palavra
    gets(palavra_a);

    j=strlen(palavra_a)-1; 
    for(i=0;palavra_a[i]!='\0';i++){    
       palavra_b[j--]=palavra_a[a]; 
    }

    if (strcmp (palavra_a, palavra_b) == 0){
        flag=0;
    } else{
        flag=1;
    }

    printf("\n  %s",palavra_b[N]);
    switch (flag){
        case 0:
            puts("\nA palavra inserida eh um palindromo.");
        break;

        case 1:
            puts("\nA palavra inserida nao eh um palindromo.");
        break;
    }

    system("Pause");
    return 0;
}

Browser other questions tagged

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