dimanche 7 octobre 2018

I don't know how to print a sentence only under certain circumstances

I am blocked at solving aproblem in the book.

The problem is:

read a word and output the string backwards, and output it backwards, you should print the palindrome if it is the same as the original.

Also, do not use a library such as string.h, but include stdio.h only.

So I created the code below.

#include <stdio.h>

int main()
{
    char str[128];
    char temp;
    int leng = 0;
    char a;

    scanf("%s", str);
    {
        a = str;
    }
    while(str[leng] != '\0')
        leng++;

    for (int i = 0; i < leng/2; i++)
    {
      temp = str[i];
      str[i] = str[leng - i - 1];
      str[leng - i - 1] = temp;
    }
    printf("%s\n", str);
    {
        if (a == str)
            printf("palindrome\n");
    }
    return 0;
}

The output in reverse order was easily solved, but I blocked in the process at printing palindrome. I tried to print the palindrome only when the input and output values ​​are the same.

However, if (a == str) I used was a code to compare address values. Also,I thought that it would be useful to implement strcmp as a loop, but I can not find a way to compare the input value with the output value using strcmp.

Is there a way to compare the input and output values ​​in C? Or is there a way to make palindrome print only under certain circumstances (input = output)?

I am wondering if I can code the input value = output value in C exactly.

Note that my code prints the palindrome when the address values ​​are the same. So I haven't seen yet :(

Aucun commentaire:

Enregistrer un commentaire