C Programming – HackerRank Solution | VOWELS CARDS GAME |

(HackerRank) Write a Modular C Programming code to solve VOWELS CARDS GAME, Pintu is having N cards, each card is written with unique alphabets.

Pintu is having N cards, each card is written with unique alphabets. He makes use of stack to store all the N cards.

Chintu, brother of Pintu wants to segregate the stack of cards based on whether card is having vowel or consonant. Chintu snips off the top card from the stack each time if the card is vowel then he placeS it into the V stack. If not, into C stack.

Help Chintu to complete this task.

Input Format

First line indicates the Total Number of cards.

Second line indicates all the Name of the cards (alphabet on the each card).

Constraints

2 ≤ N ≤ 26

Output Format

First line indicates the total Number of cards on V – Stack and C – Stack (if present) otherwise 0.

Second line indicates Name of the each card on V – Stack (if present) otherwise -1.

Third line indicates Name of the each card on C – Stack (if present) otherwise -1.

Sample Input 0

4
A K M E

Sample Output 0

2 2 
E A
M K

Sample Input 1

1

Sample Output 1

More number of cards needed.

Sample Input 2

5
A E I O U

Sample Output 2

5 0
U O I E A
-1

Refer : C Programming HackerRank all solutions for Loops | Arrays | strings | Data Structures | Linked lists | Stacks | Queues | Binary Trees

 

CODE:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>

#define SIZE 100
struct stack
{
    char data[SIZE];
    int top;
};
void push(struct stack *sptr, char num);
char pop(struct stack *sptr);
int main()
{
    struct stack * sptr1,* sptr2;
    struct stack s1,s2;
    sptr1=&s1;
    sptr1->top=-1;
    sptr2=&s2;
    sptr2->top=-1;
    int num,i;
    char ch;
    scanf("%d",&num);
    if(num<2)
    {
        printf("More number of cards needed.");
        exit(0);
    }
    for(i=0;i<num;i++) { 
scanf(" %c ",&ch); 
if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U') 
{ 
push(sptr1,ch); 
} 
else { 
push(sptr2,ch); 
} 
} 
char ch1;
if(sptr1->top==-1)
    {
       printf("0 ");
    }
    else
    {
        printf("%d ",sptr1->top+1);
    }
    if(sptr2->top==-1)
    {
       printf("0\n");
    }
    else
    {
        printf("%d\n",sptr2->top+1);
    }
    if(sptr1->top==-1)
        {
            printf("-1");
        }
    while(sptr1->top!=-1)
    {
        ch1=pop(sptr1);
        printf("%c ",ch1);
    }
    printf("\n");
    if(sptr2->top==-1)
        {
            printf("-1\n");
        }
    while(sptr2->top!=-1)
    {
        ch1=pop(sptr2);
        printf("%c ",ch1);
    }
    return 0;
}
void push(struct stack *sptr, char num)
{
    if(sptr->top==SIZE-1)
    {
        printf("Stack Overflow\n");
    }
    else
    {
        sptr->top++;
        sptr->data[sptr->top]=num;
    }
}
char pop(struct stack *sptr)
{
    char num;
    if(sptr->top==-1)
    {
        return -1;
    }
    else
    {
        num=sptr->data[sptr->top];
        sptr->top--;
    }
    return num;
}


OUTPUT

Congratulations, you passed the sample test case.

Click the Submit Code button to run your code against all the test cases.

Input (stdin)

4
A K M E
Your Output (stdout)

2 2
E A 
M K 
Expected Output

2 2 
E A
M K

 

Please find some more codes of Loops, Condition Statements, 1D Arrays, 2D Arrays, Strings, Pointers, Data Structures, Files, Linked lists, Stacks, Queues, Binary Trees, MISC, Solved model question papers & Hacker Rank all solutions on the below page:

Top 100+ C Programming codes – KLE Technological University

Leave a Comment

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

Welcome to FactsPrime

Sorry, We have detected that you have activated Ad-Blocker. Please Consider supporting us by disabling your Ad Blocker, It helps us in maintaining this website. To View the content, Please disable adblocker and refresh the page.

Thank You !!!