The link to the question is :https://www.hackerrank.com/challenges/funny-string
Problem Statement
Suppose you have a string S which has length N and is indexed from 0 to N−1. String R is the reverse of the string S. The string S is funny if the condition |Si−S(i−1)|=|Ri−R(i−1)| is true for every i from 1 to N−1.
(Note: Given a string str, stri denotes the ascii value of the ith character (0-indexed) of str. |x| denotes the absolute value of an integer x)
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main()
{
char r[10000],s[10000];
int t[10],i,j,n,c1,c2,l,f;
scanf("%d",&n);
for(i=0;i<=(n-1);i++)
{
t[i]=0;
c1=c2=0;f=0,l=0;
gets(s);
l=strlen(s);
for(j=0;j<l;j++)
r[j]=s[l-1-j];
for(j=1;j<l;j++)
{
c1=abs((int)(s[j])-(int)(s[j-1]));
c2=abs((int)(r[j])-(int)(r[j-1]));
if(c1==c2)
f=1;
else
f=0;
}
t[i]=f;
}
for(i=0;i<n;i++)
{
if(t[i]==0)
printf("Not Funny\n");
else
printf("Funny\n");
}
return 0;
}
This is my code and the required input/output are
input
2
acxz
bcxz
output
Funny
Not Funny
But I am getting a different output can anyone help me in what is wrong with the code and the worst output is for test case value 1.I am not getting it how it is giving that value
You are very close.
The problem is that after entering number using
scanf
buffer is left with one\n
character which gets interpreted as a end of a string, so the first input is empty string (and no, it's not funny). Before entering the strings, you need to clean the buffer:should be:
And now it works:
Of course, after figuring this one out, hear all the advices people posted as a comment to your question. And enable warnings in your compiler.