Monday, 23 September 2019

Little Boy is Uttering His First Words and Snake Procession Code chef solution in C

Little Boy is uttering his first words. If there is sub sequence mom in the words he uttered, print Mom and if it contains sub sequence dad print Dad and if it has both print the one that whose sub sequence has occurred first. If it has no words at all, print Goo-goo

Program:
#include<stdio.h>

int main() {
    char st[100],s1[]="mom",s2[]="dad";
    scanf("%s",st);
    int i,j=0,k=0;
 
    for(i=0;st[i]!='\0';i++)
    {
        if(st[i] == s1[j])
        {
            j++;
            if(j==3)
                break;
        }
        if(st[i] == s1[k])
        {
            k++;
            if(k==3)
                break;
        }
    }
    if(j==3)
        printf("Mom");
    else if(k==3)
        printf("Dad");
    else
        printf("Goo-goo");
 
        return 0;

}

here in order to check whether a sub sequence is present or not in a given string, the best way to check is given in the above program.
Also once we found the sub sequence mom first we need to break because we need to print the first sub sequence that was uttered first.

=======================================================================

Snake Procession

Problem Code: SNAKPROC

Solution:


#include <stdio.h>
#include <string.h>
int main(void) {
int t;
scanf("%d",&t);
while(t--)
{
    int n,i,j=0,f=0,k;
    scanf("%d",&n);
    char st[n+1],temp[n+1];
    scanf("%s",st);
    int l =strlen(st);
    for(i=0;i<l;i++)
        if(st[i] !='.')
            temp[j++]=st[i];
    if(j == 0)
        f=1;
    else if (j%2 == 0)
        for(k=0;k+1<j;k=k+2)
            if(temp[k] == 'H' && temp[k+1] == 'T')
                f=1;
            else
            {
                f=0;
                break;
            }
   if(f == 1)
    printf("Valid\n");
   else
    printf("Invalid\n");
 
}
return 0;
}

in the above program never write  st[k+1] !='\0' and always use string length and compare whether index+1 exceeds string length or not.
Here as the index, k is not incremented by 1 but by 2 and hence we are not supposed to use
st[k+1]!='\0'

Saturday, 31 August 2019

I Love T code chef solution in C

Read sentences until user enters 0
for each sentence, if the first character in each word in the given sentence is vowel, append T and if it is not vowel replace that character by T. 
Also only first character in each word only must be capital letter.

Sample Input:
love apples
apples are gOOOd for HelatHH
0

Sample Output:
Tove Tapples
Tapples Tare Toood Tor Telathh



#include<stdio.h>
int isvowel(char ch)
{
    if(ch >='A' && ch <= 'Z')
        ch=ch+32;
    if(ch == 'a' ||ch == 'e'||ch == 'i'||ch == 'o'||ch == 'u')
        return 1;
    else
        return 0;
}

int main()
{
    char st[1000001];
    scanf("\n%[^\n]\n",st);
    while(st[0] != '0')
    {
        int i;
        for(i=0;st[i]!='\0';i++)
        {
        if(st[i] >= 'A' && st[i] <= 'Z')
                st[i]=st[i]+32;
        if(i == 0|| (st[i-1] == ' ' && st[i] != ' '))
        {
         
            if(isvowel(st[i]))
                printf("T%c",st[i]);
            else
                printf("T");
        }
        else
            printf("%c",st[i]);
        }
        printf("\n");
        scanf("\n%[^\n]\n",st);
    }
    return 0;
 
}

Monday, 26 August 2019

Migratory Birds Hacker Rank Solution in C

Problem:

https://www.hackerrank.com/challenges/migratory-birds/problem


#include <stdio.h> int main() { int n,i,max,ans; scanf("%d", &n); int a[n],h[6]={0}; for(i = 0; i < n; i++) { scanf("%d",&a[i]); h[a[i]]++; } max=h[1]; for(i = 2; i < 6; i++) { if(max<h[i]) { max=h[i]; ans=i; } } printf("%d",ans); return 0; }

Friday, 23 August 2019

Binary Numbers Hacker Rank Solution in C

 #include <stdio.h>
int main()
{
    int n,r,c=0,t=0;
    scanf("%d",&n);
    while(n>0)
    {
        r=n%2;
        if(r==1)
        {
            c++;
            if(c>t)
                t=c;
        }
        else
            c=0;
        n=n/2;
    }
    printf("%d",t);
    return 0;

}


Find Product Hacker Earth Solution in C,Let Us Understand Computer Hacker Earth Solution in C,Count Numbers Hacker Earth Solution in C

#include <stdio.h>
#define ma 1000000007
int main(){
int num,i,a;
unsigned long long int ans=1;
scanf("%d", &num);

for(i=0;i<num;i++){
   scanf("%d", &a);
   ans=(ans*a)%ma;
}
printf("%d", ans);

}

===============or=================
#include <stdio.h>
const int ma = 1e9+7;
int main(){
int num,i,a;
unsigned long long int ans=1;
scanf("%d", &num);

for(i=0;i<num;i++){
   scanf("%d", &a);
   ans=(ans*a)%ma;
}
printf("%d", ans);

}

=============================================================
Let Us Understand Computer Hacker Earth Solution in C

#include <stdio.h>
#include <math.h>
int main()
{
unsigned long long int t;
scanf("%llu",&t);
while(t--)
{
unsigned long long int k,n,ans,i;
scanf("%lld",&n);
i=1;
while(i<=sqrt(n))
{
i=i*2;
}
if(n/i>=i/2)
ans=n-n/i;
else
ans=(n-(i/2))+1;
  printf("%lld\n",ans);
}
}

===========Count Numbers Hacker Earth Solution in C==============
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. int main()
  4. {
  5. int t,i;
  6. scanf("%d",&t);
  7. for(i=1;i<=t;i++)
  8. {
  9. int n,j,c=0;
  10. scanf("%d",&n);
  11. n++;
  12. char s[n];
  13. scanf("%s",s);
  14. for(j=0;s[j]!='\0';j++)
  15. {
  16. if(isdigit(s[j]) && !isdigit(s[j-1]))
  17. c++;
  18. }
  19. printf("%d\n",c);
  20. }
  21. return 0;
  22. }

Thursday, 22 August 2019

Finalized without bounds infix to postfix

#include<stdio.h>
#include <process.h>
#define MAX 50
char stack[MAX];
int top=-1;
int getpriority(char);
void push(char);
char pop();
main()
{
char infix[50],temp,ch,postfix[50];
int i=0,j=0;
printf("enter the infix expression\n");
scanf("%s",&infix);
while(infix[i] != '\0')
{
ch=infix[i];
if(ch == '(')
push(ch);
else if(ch == ')')
{
while(stack[top]!='(')
postfix[j++]=pop();
temp=pop();// popping the (
}
else if(isdigit(ch)||isalpha(ch))
postfix[j++]=ch;
else
{
while(getpriority(stack[top])>=getpriority(ch))
postfix[j++]=pop();
push(ch);
}
i++;
}
while(top != -1)
postfix[j++]=pop();
postfix[j]='\0';
puts(postfix);
}
int getpriority(char ch)
{
if( ch == '*'|| ch == '/'|| ch == '%')
return 2;
else if(ch == '+' || ch == '-')
return 1;
else
return 0;
}
void push(char item)
{
if(top == MAX-1)
{
printf("stack is full");
return;
}
top=top+1;
stack[top]=item;
}

char pop()
{
if(top == -1)
{
printf("stack empty");
return;
}
char temp;
temp=stack[top];
top=top-1;
return temp;
}