Monday 14 March 2016

Functions – Lucky String


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.

Function specifications: 

int checkLucky(char * a) 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.

Sample Input and Output 1: 
Enter the input string
anitha
anitha is not lucky

Sample Input and Output 2: 
Enter the input string
technology
technology is lucky

Code:
  #include<stdio.h>
#include<string.h>
int checkLucky(char *a);
int main(){
  char s[100],c;
  printf("Enter the input string\n");
  scanf("%s",s);
  c=checkLucky(s);
  if(c==1)
    printf("%s is lucky",s);
  else if(c==0)
    printf("%s is not lucky",s);
  return 0;
}
int checkLucky(char *a)
{
  char *name;
  int i,sum=0,j;
  name=a;
  i=strlen(a);
  for(j=0;j<i;j++)
  {
    sum=sum+name[j];
  }
  if((sum%2)==0)
    return 1;
  else 
    return 0; 
}
  
  
  

2 comments:

  1. Compilation Errors - /tmp/ccKmj2t1.o: In function `main': m0.c:(.text+0x57): undefined reference to `findDistance' collect2: ld returned 1 exit status
    showing this kind of error for the above program may i know the solution

    ReplyDelete
  2. u have not passed pointer to character to the function
    use char *s1=s;
    c=checkLucky(s1);

    ReplyDelete