Problem Statement:
In cricket, a captain is said to be lucky if he wins the toss in at least half of the matches.
Given the information regarding n matches, find whether a captain is lucky or not. W corresponds to winning the toss and L corresponds to losing the toss.
Assume: Maximum length of string is 50.
Input consists of a string that gives information regarding 'n' matches. Assume that the length of the string is always even.Output Format:
Print Invalid Input if any character in the input is invalid. The valid characters in the input are 'W' (Win) and 'L' (Loss).Sample Input1:
Print Lucky if the captain has won the toss in at least half of the matches.
Print Unlucky if the captain has not won the toss in at least half of the matches.
WLWLWW
Sample Output1:
Lucky
Sample Input2:
WLWLLL
Sample Output2:
Unlucky
Sample Input3:
WLWLEW
Sample Output3:
Invalid Input
Solution in | C | JAVA | PYTHON |
---|
#include<stdio.h>
int main() {
char tosses[30];
int i,wcount=0,lcount=0;
fgets(tosses,30,stdin);
for(i=0;tosses[i]!='\0';i++){
if(tosses[i]=='W'){
wcount++;
}else if(tosses[i]=='L'){
lcount++;
}else{
break;
}
}
if(wcount+lcount==i){
if(wcount>=lcount){
printf("Lucky");
}else{
printf("Unlucky");
}
}else{
print("Invalid Input");
}
}
int main() {
char tosses[30];
int i,wcount=0,lcount=0;
fgets(tosses,30,stdin);
for(i=0;tosses[i]!='\0';i++){
if(tosses[i]=='W'){
wcount++;
}else if(tosses[i]=='L'){
lcount++;
}else{
break;
}
}
if(wcount+lcount==i){
if(wcount>=lcount){
printf("Lucky");
}else{
printf("Unlucky");
}
}else{
print("Invalid Input");
}
}