Is Palindrome


This is a companion discussion topic for the original entry at https://toph.co/p/is-palindrome
#include<stdio.h>
#include<string.h>
int main()
{
	char c[100];
	scanf("%s",c);
	int x=strlen(c);
	int i,j;
	for(i=0,j=x-1;i<x,j>=0;i++,j--)
	{
		if(c[i]==c[j])
		{
			if(i==x-1)
			{
				printf("yes");
				break;
			}
			else
			{
				continue;
			}
		}
		else
		{
			printf("no");
			break;
		}
	}
}

can anyone tell me what is wrong with that?
it says my submission made a boo-boo

#include<bits/stdc++.h>
using namespace std;

int main()
{
    string s,r;
    cin>>s;
    r=s;
    reverse(r.begin(),r.end());
    if(r==s)
        printf("Yes\n");
    else
        printf("No\n");
    return 0;
}

What possibly is wrong with this code?
My IDE shows the answer perfectly, but Toph is showing otherwise…
Suggestions PLz

#include<stdio.h>
#include<string.h>
int main()
{
    char a[100], b[100];
    gets(a);
    strcpy(b,a);
    puts(strrev(b));
    if (strcmp(a,b)==0){
    printf("Yes");
    }
    else{
    printf("No");
    }
    return 0;
}

There are two things that I can see not being quite right:

  1. You should not use gets. In fact, in Toph, you cannot use gets. It will result in a compilation error. To know why you shouldn’t use gets, you can read this StackOverflow answer.
  2. This line seems odd: puts(strrev(b));. You don’t need to print the reversed string.
1 Like

Some times I use gets and don’t get any error.

You said I shouldn’t use gets. So I used fgets and still get compilation error. Please check my code. Also, Reading your 2nd point seems strrev would work.

#include <stdio.h>
#include <string.h>
#define MAX 100
int main()
{
    char a[MAX],b[MAX];
    fgets(a,MAX,stdin);
    strcpy(b,a);
    strrev(a);
    //puts(b);
    //printf("%d",j);
    if(strcmp(a,b)==0){
        printf("Yes\n");
    }
    else{
        printf("No\n");
    }
	return 0;
}

@Galib.603386 Can you share your submission ID where you used fgets and received compilation error?

1 Like

Sure. The ID is 328328.

I’m getting error because I used strrev. Can you tell me why I shouldn’t use strrev?
The ID is 328328.

It seems the function strrev is no longer available in the latest standard of C (i.e. C11).

I know this can be confusing, but sometimes the standards get rid of certain functions for many reasons.

What I can do is allow an older standard of C to make sure that you can still use the strrev function that many are familiar with.

1 Like

Right. You should allow the older standard of C. Because If I search google how to to reverse two strings in C, it teaches me to use strrev.

Looks like I responded a bit prematurely.

strrev is not a part of any C standard. It is however implemented by some libraries, which unfortunately, is not available as a part of GCC (what we use to compile C and C++ code).

So in these cases you will have to implement your own function for reversing string in C.

In C++ there are standard ways to do this, so you can look into that.

1 Like

What’s the problem in my code?

#include <stdio.h>
#include <string.h>
#define MAX 100
int main()
{
    char a[MAX],b[MAX];
    fgets(a,MAX,stdin);
    strcpy(b,a);
    int j=strlen(a);
    int i=0;
    j-=2;
    for(;j>=0;j--,i++){
        b[i]=a[j];
    }
    if(strcmp(a,b)==0){
        printf("Yes\n");
    }
    else{
        printf("No\n");
    }
	return 0;
}

It’s failing the first sample. You need to figure out why.

Try debugging your code step by step.

I don’t think it’s failing the first sample. Have a closer look.

Tell me the submission ID please.

The submission ID is 328380. Why I need 20 characters to reply? This is so annoying.

If the length of your string is j, then why are you subtracting 2 from it?

If the string is “racecar”, then j == 7. If you want the index of the last character, then you should subtract 1 from j.

It’s because if I enter “racecar” then it shows 8 instead of 7.