The Funny string code

5.4k views Asked by At

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

4

There are 4 answers

0
Nemanja Boric On BEST ANSWER

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:

scanf("%d",&n);

should be:

scanf("%d",&n);
while (getchar() != '\n');

And now it works:

2
acxz
bcxz
Funny
Not Funny

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.

0
Varun Garg On

1) You do not need to store all your outputs in a array and print them later, you can print them simultaneously, otherwise it may exceed memory limit, and implementing it will also waste your time

2)The reason its not working properly is because scanf leaves \n in your buffer. You can clear it by adding this after scanf

char clean;
while (clean=getchar()!='\n' && clean !=EOF);
2
JIJO T KOSHY On
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        /*
         * Enter your code here. Read input from STDIN. Print output to STDOUT.
         * Your class should be named Solution.
         */
        String s;
        int t;

        Scanner in = new Scanner(System.in);
        t = in.nextInt();

        for (int f = 0; f < t; ++f) {
            s = in.next();
            getResult(s);
        }
        in.close(); 
    }

    public static int getAbsoluteValue(int a, int b) {
        int value = Math.abs(a - b);
        return value;
    }

    public static void getResult(String s) {
        char[] cArr;
        String sReverse = "";
        char[] cArrReverse;
        int count = 0;
        cArr = s.toCharArray();

        for (int i = cArr.length - 1; i >= 0; i--) {
            sReverse += cArr[i];
        }
        cArrReverse = sReverse.toCharArray();

        for (int i = 0; i < cArr.length - 1; i++) {
            int h = getAbsoluteValue(cArr[i + 1], cArr[i]);
            int k = getAbsoluteValue(cArrReverse[i + 1], cArrReverse[i]);

            if (h == k) {
                count++;

            }
        }
        if (count == cArr.length - 1) {
            System.out.println("funny");
        } else {
            System.out.println("not funny");
        }
    }

}
0
prabhas On
int main() {

    char s[10000], r[10000];
    int times;
    scanf("%d", &times);
    for (int t = 0; t < times; t++) {
        scanf("%s", s);
        int len = strlen(s);
        for (int i = 0; i < len; i++) {
            r[i] = s[strlen(s) - i - 1];
        }
        int funny = 1;
        for (int i = 1; i < len; i++) {
            if (abs(r[i] - r[i-1]) != abs(s[i] - s[i-1])) {
                printf("Not Funny\n");
                funny = 0;
                break;
            }     
        }
        if (funny) {
            printf("Funny\n");
        }

    }

    return 0;
}