Canna

Tuesday, 25 September 2018

Functions-Lucky String

  • Problem Description

    Write a program to find whether the given string is Lucky or not.


    A string is said to be lucky if the sum of the ascii values of the characters in the string is even.

    Refer function specifications for the function details.

    The function accepts a pointer to a string and returns an int.

    The return value is 1 if the string is lucky and 0 otherwise.

    Input and Output Format:

    Input consists of a string. Assume that all characters in the string are lowercase letters and the maximum length of the string is 100.

    Refer sample input and output for formatting specifications.

    All text in bold corresponds to input and the rest corresponds to output.
  • Test Case 1

    Input (stdin)
    anitha
    
    
    Expected Output
    anitha is not lucky
  • Test Case 2

    Input (stdin)
    srm
    
    
    Expected Output
    srm is lucky
  • Program
  • #include<stdio.h>
     #include<string.h>
     int checklucky( char *);
     int main()
     {
        int c;
        char a[100];
        
        scanf("%s",a);
    
        c=checklucky(a);
    
        if(c==1)
            printf("%s is lucky\n",a);
        else
            printf("%s is not lucky\n",a);
    
        return 0;
    }
    
    int checklucky(char *a)
    {
        char *name;
        int sum=0,len,i;
        name=a;
    
        // printf("Enter the input string\n");
        // scanf("%s",name);
    
        len=strlen(name);
    
        for(i=0;i<len;i++)
       {
           sum=sum+name[i];
       }
    
       if(sum%2==0)
           return (1);
      else
           return (0);
    }

No comments:

Post a Comment

Note: only a member of this blog may post a comment.