0

I have written the below code to find whether the string is a palindrome or not. In this, what i am trying to do is, first i reversing the string being stored in char 'a' and storing that reversed string in char 'b', but i am getting the error "Array must be initialized with a brace enclosed initializer" for char 'b'. Can someone please tell me what is wrong with my code and why i am getting the above error.

Code:

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

int main()
{
   char a[100];
   scanf("%s",&a[100]);
   char b[100] = strrev(a);

   if(a[100]=b[100])
   {
        printf("It is a palindrome");
   }
   else
   {
        printf("It is not a palindrome");
   }
}
Manik
  • 3
  • 5
  • C or C++? Please make up your mind. – Jabberwocky Nov 24 '21 at 14:02
  • 5
    `scanf("%s",&a[100]);` is wrong, so is `char b[100] = strrev(a);` and `if(a[100]=b[100])` - you really need a better book if these things aren't explained at all – UnholySheep Nov 24 '21 at 14:04
  • Make sure to enable your compiler's warnings! (e.g. `-Wall -Wextra -pedantic` with gcc/clang) – ikegami Nov 24 '21 at 14:05
  • 1
    To be clear, `&a[100]` is the address of _the first character after the end of the array_ (the first character in the array is `a[0]` and the last one is `a[99]`) – Tim Randall Nov 24 '21 at 14:06
  • There are many problems with your code, but the error you're seeing is because you can't initialize an array with an expression like `strrev(a)`. See: https://stackoverflow.com/questions/70052795/in-c-why-do-i-have-s-initialization-requires-a-brace-enclosed-initializer/70052836#70052836 – Paul Hankin Nov 24 '21 at 14:07
  • @UnholySheep Sorry sir, but it would have been better to explain me what's wrong rather than roasting me. I have just begun to learn programming, and i m still learning. – Manik Nov 24 '21 at 14:09
  • `a[100]` and `b[100]` are out of bounds, as C uses 0-based array indexing. – Sourav Ghosh Nov 24 '21 at 14:09
  • After fixing the other errors, use `memcpy` or `strncpy` to copy one string to another, and then reverse that string in-place. Use `strcmp` to compare. Alternatively, you can do this without a second buffer by iterating through the string from both ends and comparing characters until the iterators meet in the middle. – paddy Nov 24 '21 at 14:10
  • @SouravGhosh Can you please elaborate what do you mean by that? – Manik Nov 24 '21 at 14:11
  • Thanks @paddy i was able to run my code the way you are telling on my own but i wanted to know what is wrong with the above code – Manik Nov 24 '21 at 14:12
  • for an array defined like `a[100]`, valid indexes are 0 to 99, inclusive. an index 100 is invalid. – Sourav Ghosh Nov 24 '21 at 14:12
  • Basically: you can't program by trial & error or by typing random stuff/guessing how C works. You need to be confident that you actually know what every single line you type does. – Lundin Nov 24 '21 at 14:47

1 Answers1

1

There are multiple issues in your code:

  • scanf("%s",&a[100]) is wrong, &a[100] is the address of the 101st char in your array, you want the address of the first character, so it should be &a[0] or simply a.
  • you need to use strcmp for comparing strings. Read this for a more detailed explanation
  • char b[100] = strrev(a) is wrong, you cannot assign entire arrays in C.

Bonus:

  • you should use scanf("%99s", ... in order to avoid buffer overflows. 99 because your string can contain at maximum 99 characters + the null terminator.

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

int main()
{
  char a[100];
  scanf("%s", a);  // ask user for string and put it in a

  char b[100];
  strcpy(b, a);    // copy a to b

  strrev(a);      // reverse a

  if (strcmp(a, b) == 0)  // compare a to b
  {
    printf("It is a palindrome");
  }
  else
  {
    printf("It is not a palindrome");
  }
}
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • I strongly recommend `scanf("%99s", a)` in this example. `scanf` is easily misused and ought to be avoided and arguments can be made that it's not worth trying to point out all of its foibles, but this at least should be demonstrated to the beginner. – William Pursell Nov 24 '21 at 14:11
  • I understand you're following OP, but while correcting things, please mention to avoid scanf for user inputs, too. – Sourav Ghosh Nov 24 '21 at 14:11
  • @SouravGhosh I entirely agree with avoiding `scanf` for user input, but that's a whole chapter on its own. – Jabberwocky Nov 24 '21 at 14:12
  • @Jabberwocky I agree. I was asking to mention about that in the notes, for reference, as a good coding practice. – Sourav Ghosh Nov 24 '21 at 14:13
  • Thanks @Jabberwocky i was able to run my code the way you are telling on my own but i wanted to know what is wrong with the above code – Manik Nov 24 '21 at 14:13
  • @Manik I mentioned what's wrong in your code. Refresh your browser. – Jabberwocky Nov 24 '21 at 14:14
  • Thanks alot @Jabberwocky for helping out – Manik Nov 24 '21 at 14:17
  • @Manik read [this](https://stackoverflow.com/a/54136210/898348) for detailed explanations on comparing strings – Jabberwocky Nov 24 '21 at 14:18