c - The Funny string code -
the link question :https://www.hackerrank.com/challenges/funny-string
problem statement
suppose have string s has length n , indexed 0 n−1. string r reverse of string s. string s funny if condition |si−s(i−1)|=|ri−r(i−1)| true every 1 n−1.
(note: given string str, stri denotes ascii value of ith character (0-indexed) of str. |x| denotes absolute value of 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 code , required input/output
input
2
acxz
bcxz
output
funny
not funny
but getting different output can me in wrong code , worst output test case value 1.i not getting how giving value
you close.
the problem after entering number using scanf buffer left 1 \n character gets interpreted end of string, first input empty string (and no, it's not funny). before entering strings, need clean buffer:
scanf("%d",&n); should be:
scanf("%d",&n); while (getchar() != '\n'); and works:
2 acxz bcxz funny not funny of course, after figuring 1 out, hear advices people posted comment question. , enable warnings in compiler.
Comments
Post a Comment