Comparing string contents in C to find palindrome

1

I need to check if a string A, for example, is equal to string B to determine if the entered word is a palindrome, string B is the inverse of A, I can not determine if the contents are the same. For example, I enter with the word "macaw", the program should display a message that the word is a palindrome, instead it shows the message saying 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;
    }
    
asked by anonymous 26.03.2017 / 23:48

3 answers

6

This code is too complex, simplifying it is much easier to understand. Simply compare the first with the last, the second with the penultimate character, and so on. It only needs to go halfway since 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ó precisa 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.");
}

See running on ideone . And at Coding Ground . Also put it on GitHub for future reference .

    
27.03.2017 / 00:17
3
  
  • Here you are forgetting index 0, in this case always counting +1 character
  •   
  • You are also using strlen , which is a function of h
  •   
  •   
for(i=strlen(palavra_a) - 1; i>0; i--)
{
   palavra_b[j]=palavra_a[i];
   j=j+1;
}
  
  • here, you do not need elseif ,
  •   
  • Because if two alphabetic characters are not equal, they are different
  •   
  • else resolves 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 invert 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 * polym (char arg) does, is to 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 as initial value the size of the string passed in the argument of that function * arg less 1 position because the size is always returned by counting from 1 instead of 0 . Therefore, if the smallest variable i is even smaller than the smallest variable x , then the smallest increment at the same time as decrements the largest variable. Within the loop , at the end, i will have the same size as x before loop (< in>. Then, as one of the indexes increases and another decreases respectively the characters are compared in reverse order.

Example

Does not apply:

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 to:

char a[5] = tenet

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

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

Answer also in the SOen .

    
27.03.2017 / 01:07
0

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]!='
#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]!='%pre%';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;
}
';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; }
    
27.03.2017 / 00:13