-1

isn't my assignment,condition and updation in my for loop correct?,its showing the error on that line

void main()
{  int i,j,n;
   char a[10];
   printf("\t\tPROGRAM TO CHECK IF A STRING IS PALINDROME OR NOT");
   printf("\n\t\t-----------------------------------------------");
   printf("\nEnter the string: ");
   gets(a);
   n=strlen(a);
   for(i=0;j=n-1;i<=j;i++;j--){
    if(toupper(a[i])!=toupper(a[j])){
        printf("The string is not palindrome");
        break;
    }
   }
   if(i==j){
    printf("\nThe string %s is a palindrome",a);
   }
   getch();

}
alvin lal
  • 138
  • 4
  • 10

2 Answers2

1

Your problem is that the line for(i=0;j=n-1;i<=j;i++;j--){ has too may semicolons in it!

Semicolons separate statements and for takes only three such. To put two (or more) expressions in the same statement, separate them with commas, as follows: for(i=0, j=n-1; i<=j; i++, j--){

Hope this helps.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
0
 #include<stdio.h>
 #include<string.h>
 #include<ctype.h>
 int main()
 {
   int i,j,n;
   char a[10];
   printf("\t\tPROGRAM TO CHECK IF A STRING IS PALINDROME OR NOT");
   printf("\n\t\t-----------------------------------------------");
   printf("\nEnter the string: ");
   gets(a);
   puts(a);
   n=strlen(a);
   for(i=0, j=n-1;i<(n-1)/2;i++, j--){
    if(toupper(a[i])!=toupper(a[j])){
        printf("The string is not palindrome");
        break;
    }
   }
   if(i==j || i == j-1){
    printf("The string  is a palindrome\n");
   }
   return 0;

}
  • your for loop is correct and the program works fine,mine goes to an infinite loop whenever a palindrome string is entered,can u explain why i<=j didnt work?, – alvin lal Sep 21 '19 at 09:16