-3

The error usually occurs when I run the code. I'm a beginner to C programmer. I personally think that I made the mistake at the if statement after I printed and copied "word".

    char word[10]="hih",word1[10],i,j;
    
    for(i=0;i<10;i++)
    {
        printf("%c", word[i]);
    }
    printf("\n");
    for(j=10;j>=0;j--)
    {
        printf("%c", word1[j]);
    }
    printf("\n");
    if(word==word1)
    {
        printf("The word entered is a Palindrome word.");
    }
    else
    {
        printf("The word entered is not a Palindrome word.");
    }

    return 0;
}

This is the error that I get:

enter image description here

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • 5
    Don't spam tags. – Julien Mar 23 '22 at 17:15
  • `Python != Java != C++ != C`. – The Coding Fox Mar 23 '22 at 17:16
  • What is "the error"? – Cheatah Mar 23 '22 at 17:18
  • 1
    *The error* expand on this. Often an experienced programmer can tell you exactly what went wrong from a good description without having to look at code. Right now the code provided cannot compile, so we cannot run the program to see what the error is for ourselves. – user4581301 Mar 23 '22 at 17:18
  • 3
    That snippet won't compile. You should start with including required headers, a `main` function and tell us what exactly is not working. – Gerhardh Mar 23 '22 at 17:18
  • 4
    Compare strings using `strcmp` not `==` – Cheatah Mar 23 '22 at 17:19
  • 3
    `printf("%c", word1[j]);` operates on uninitialized variables. garbage output is a likely result. – user4581301 Mar 23 '22 at 17:20
  • You might get compiler warnings on using `word1[j]` without assigning any value to it. Also you are accessing invalid array index for `j==10`. Allowed range for `j` is `0..9`. I assume that loop is intented to set values for `word1`, not for printing. – Gerhardh Mar 23 '22 at 17:20
  • 1
    Just first work out the problem on paper, because your code doesn't really do anything useful. – Cheatah Mar 23 '22 at 17:21
  • Can I attach pictures in this ? – Just a guy who likes anime D Mar 23 '22 at 17:22
  • 3
    Please read this: [ask] and then [edit] and show a [mcve]. Also tell us what exact error you get when you do what. – Jabberwocky Mar 23 '22 at 17:27
  • (My teacher gave me a task to find if a word is a palindrome word or not. but I'm not allowed to use string.) – Just a guy who likes anime D Mar 23 '22 at 17:30
  • 1
    Don't post pictures of your code, and of your output. Post them as properly formatted text. – Jabberwocky Mar 23 '22 at 17:31
  • 1
    Think about what a palindrome is: The first character matches the last. The second character matches the second last The third character matches the third last. Repeat until you reach the center. – user4581301 Mar 23 '22 at 17:33
  • In the second for loop you print `word1[j]`, but the `word1` array hasn't been initialized, it contains undetermined values and that's what you're seeing. – Jabberwocky Mar 23 '22 at 17:33
  • 1
    Also `if (word==word1)` doesn't do what you think it does, read this: https://stackoverflow.com/questions/8004237/how-do-i-properly-compare-strings-in-c. But anyway for determining if a word is a palindrome, you don't need `word1` anyway. How do you figure out if a word is a palindrome with a pencil and a piece of paper? – Jabberwocky Mar 23 '22 at 17:35
  • Just a guy who likes anime D, Just a guy who likes anime D, `word==word1` compares pointers, not string contents. – chux - Reinstate Monica Mar 23 '22 at 18:20
  • Just a guy who likes anime D, "but I'm not allowed to use string" --> Note that `"%c"` is a _string_. – chux - Reinstate Monica Mar 23 '22 at 18:22
  • Mind you, I removed the C++ tag because of *a beginner to C programmer* and the code is 100% C. In C++ you have the option of `std::string` which might be what the instructor is disallowing – user4581301 Mar 23 '22 at 18:36
  • @SolvedGames Well, inequality isn't transitive – C couls still be equal to either of Java or Python (but not both)... – Aconcagua Mar 24 '22 at 09:15
  • *If* you really want to operate on two arrays and with `strcmp` then you'd need to first copy the input array to the second one in reverse order. Better is checking in place, though, as described before. Hint: Use two pointers to first and last character, compare dereferenced pointers for equality, increment one and decrement the other until the one starting at the end is equal or less than the one starting at the beginning. If you discover inequality, you can shortcut to 'no palindrome' (break the loop by some means). – Aconcagua Mar 24 '22 at 09:23
  • Note: Pointer comparison other than equality is legal here, but only because both point to the *same* array (otherwise it would be undefined behaviour). – Aconcagua Mar 24 '22 at 09:23

1 Answers1

0

Your issue is that you compare word with word1. You initialize word properly, but you do not initialize word1, so its content is undefined and you will get the result that it's not a palindrome.

This is how you could fix your code

    char word[10]="hih",i,j;
    
    for(i=0;i<10;i++)
    {
        printf("%c", word[i]);
    }
    printf("\n");
    for(j=10;j>=0;j--)
    {
        printf("%c", word[j]);
    }
    printf("\n");
    int isPalindrome = 1;
    int length = 3;
    for (i = 0; isPalindrome && (i < length/2); i++)
    {
        isPalindrome = word[i] == word[length - i - 1];
    }
    if(isPalindrome)
    {
        printf("The word entered is a Palindrome word.");
    }
    else
    {
        printf("The word entered is not a Palindrome word.");
    }

    return 0;
}

Note that we didn't need a second variable for word, we could just compare the starting characters with the ending characters.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175