Palindrome string; please help why its wrong

Q: Palindrome String | InterviewBit

#include<bits/stdc++.h>
using namespace std;
int main ()
{
    
 int n,N;
 cin>>N;
 for(n=0;n<N;n++)
 {
     int k=0,t=0,m=0,l=0,i,j,L=0;
     string s;
     string alp="";
      cin>>s;
      l=s.length();
  for(i=0;i<l;i++)
  {    
    if(isalnum (s[i]))
    {
        alp+=s[i];
    }
    else 
    {
        continue;
    }
  }
  L=alp.length();
  m=L/2;
  for(j=0;j<=m;j++)
  {
      k=((L-1)-j);
     if(alp[j]==alp[k])
     {
         t++;
     }
  }
  if(t==(m+1))
  {
      cout<<1;
  }
  else
  {
      cout<<0;
  }
 }
 return 0;
}
1 Like

@aditigedam
a==A is true
but in your case it is false

@aditigedam for the testcase “Aa”
it should print 1 but in your program its printing 0
so your program should check irrespective of their upper or lower cases
so if you encounter a uppercase letter change it to a lowercase one and then check whether they are equal
isupper() returns 1 if it is a uppercase letter else 0
tolower() converts the given letter to a lowercase one

refer https://www.geeksforgeeks.org/isupper-islower-application-c/
for the syntax

why this is showing wrong again?? what the problem with this one please help!!!

#include<bits/stdc++.h>
using namespace std;
int main ()
{
     int k=0,t=0,m=0,l=0,i,j,L=0;
     string s;
     string alp="";
      cin>>s;
      l=s.length();
  for(i=0;i<l;i++)
  {    
    if(isalnum (s[i]))
    {
        if(isupper (s[i]))
    {
      s[i]=  char(tolower (s[i])) ;
    }
        alp+=s[i];
    }
    else 
    {
        continue;
    }
  }
  L=alp.length();
  m=L/2;
  for(j=0;j<=m;j++)
  {
      k=((L-1)-j);
     if(alp[j]==alp[k])
     {
         t++;
     }
  }
  if(t==(m+1))
  {
      return 1;
  }
  else
  {
      return 0;
  }
}

@aditigedam in the printing statement you have put “return” instead of “cout”
please change that

question have this note" You only need to implement the given function. Do not read input, instead use the arguments to the function. Do not print the output, instead return values as specified."
so what does it mean?

ufff … still it is showing some error!

#include<bits/stdc++.h>
using namespace std;
int main ()
{
     int k=0,t=0,m=0,l=0,i,j,L=0;
     string s;
     string alp="";
      cin>>s;
      l=s.length();
  for(i=0;i<l;i++)
  {    
    if(isalnum (s[i]))
    {
        if(isupper (s[i]))
    {
      s[i]=  char(tolower (s[i])) ;
    }
        alp+=s[i];
    }
    else 
    {
        continue;
    }
  }
  L=alp.length();
  m=L/2;
  for(j=0;j<=m;j++)
  {
      k=((L-1)-j);
     if(alp[j]==alp[k])
     {
         t++;
     }
  }
  if(t==(m+1))
  {
      cout<< 1;
  }
  else
  {
      cout<< 0;
  }
  if(L=0)
  {
      cout<<1;
  }
  return 0;
}

@aditigedam in the interviewbit you are supposed to submit only the function code and not the full main code
the arguments passed to the function is your input and the value you return is the output
below is the function code for this palindrome string
so you have to submit only the function code and not the full main code

int Solution::isPalindrome(string A)
{
  int k=0,t=0,m=0,l=0,i,j,L=0;
     string s;
     string alp="";
      s=A;
      l=s.length();
  for(i=0;i<l;i++)
  {    
    if(isalnum (s[i]))
    {
        if(isupper (s[i]))
    {
      s[i]=  char(tolower (s[i])) ;
    }
        alp+=s[i];
    }
    else 
    {
        continue;
    }
  }
  L=alp.length();
  m=L/2;
  for(j=0;j<=m;j++)
  {
      k=((L-1)-j);
     if(alp[j]==alp[k])
     {
         t++;
     }
  }
  if(L==0)
  {
      return 1;
  }
  else
    {
          if(t==(m+1))
        {
          return 1;
        }
      else
        {
          return 0;
        }
    }
}
1 Like