Canna

Sunday, 30 September 2018

payroll using structure

Problem Description

1. Create a Structure "employee"

2. Create six data members for structures as name(char), empid(int), salary(int), hra(int), da(int), total(float)

3. Input the data of the employee as name, empid, salary.

4. Calculate the HRA(10% salary), DA(20% salary) 

5. Total pay = salary +hra +da

6. Create structure variable as "emp"
Test Case 1

Input (stdin)

Bogar

1000

15000

Expected Output
Name=Bogar

Id=1000

HRA=1500

DA=3000

Total Salary=19500

#include <stdio.h>
struct employee
{
  char name[100];
  int empid;
  int salary;
  int hra;
  int da;
  float total;
}emp;
int main()
{
  scanf("%s%d%d",emp.name,&emp.empid,&emp.salary);
  printf("Name=%s\n",emp.name);
  printf("Id=%d\n",emp.empid);
  emp.hra=(emp.salary/100)*10;
  printf("HRA=%d\n",emp.hra);
  emp.da=(emp.salary/100)*20;
  printf("DA=%d\n",emp.da);
  printf("Total Salary=%d",emp.salary+emp.hra+emp.da);
 return 0;
}

Saturday, 29 September 2018

COUTNTALPHA

  • Problem Description

    Kevin and marhov play the words game.the game is identify the alphabet and numbers in a slide show.
  • Test Case 1

    Input (stdin)
    2
    
    
    Expected Output
    Not an alphabet
  • Test Case 2

    Input (stdin)
    Q
    
    
    Expected Output
    an alphabet
  • Program
  • #include <stdio.h>
    #include <string.h>
    int main()
    {
      char c;
      scanf("%c",&c);
      if((c>='a'&&c<='z')||(c>='A'&&c<='Z'))
      {
        printf("an alphabet");
        }
      else
        {
        printf("Not an alphabet");
    }
     return 0;
    }

Maximum Frequency

  • Problem Description

    How to find highest frequency character in a string using loop in C programming.Find frequency of each characters in the string and store it in some array of integers.
    Find maximum element in the array. Maximum element in the array will the be maximum occurring character in the string.
  • Test Case 1

    Input (stdin)
    aaabbcd
    
    
    Expected Output
    a=3
    
    b=2
    
    c=1
    
    d=1
  • Test Case 2

    Input (stdin)
    aabbccdd
    
    
    Expected Output
    a=2
    
    b=2
    
    c=2
    
    d=2
  • Program
  • #include <stdio.h>
    #include <string.h>
    int main()
    {
       char string[100];
       int c = 0, count[26] = {0}, x;
       scanf("%s",string);
     
       while (string[c] != '\0') 
       {
         if (string[c] >= 'a' && string[c] <= 'd') 
          {
             x = string[c] - 'a';
             count[x]++;
          }
     
          c++;
       }
     
       for (c = 0; c < 4; c++)
       printf("%c=%d\n", c + 'a', count[c]);
     
     return 0;
    }

Print Index

  • Problem Description

    How to find all occurrences of a character in a given string using for loop in C programming. Program to print all index of a character in a given string.
  • Test Case 1

    Input (stdin)
    u
    
    srmuniversity
    
    
    
    
    Expected Output
    u is found at index 3
  • Test Case 2

    Input (stdin)
    e
    
    srmuniversitylearningcentre
    
    
    Expected Output
    e is found at index 7
    
    e is found at index 14
    
    e is found at index 22
    
    e is found at index 26
  • Program
  • #include <stdio.h>
    #include<string.h>
    int main()
    {
      char a,word[50];
      int i=0;
      scanf("%c",&a);
      scanf("%s",word);
      for(i=0;i<strlen(word);i++)
      {
        if(word[i]==a)
        {
          
          printf("%c is found at index %d\n",a,i);
          
        }
      }
    
     return 0;
    }

Play With Strings

  • Problem Description

    Given are two strings(each not more than 100 characters). We need to find relation between them such that all the characters of the second string must appear in the first string sequentially (not necessarily continuously). The program should :

    Print 1: if the characters in the first string appear in the same sequence as that of the second string.
    Print 2: if the characters in the first string appear in the opposite sequence as that of second string.
    Print 3: if the characters in the first string appear in both ie same as well as opposite sequences of the second string.
    Print 0: if the characters in the first string dont appear in sequence of the second string. 


    The characters in the strings are case-sensitive.
    Input

    The first line should give the number of test cases. For each test case, it takes input two strings.
    Output

    An integer ( 0,1,2 or 3 ) for each of the test cases. 
  • Test Case 1

    Input (stdin)
    2
    
    njack njk
    
    anwesha awesa
    
    
    Expected Output
    1
    
    1
  • Test Case 2

    Input (stdin)
    2
    
    wysiwig iwi
    
    abcdef edba
    
    
    Expected Output
    3
    
    2
  • Program
  • #include <stdio.h>
    #include <string.h>
    
    int main()
    {
        //printf("Hello world!\n");
        int t,i,j,ans;
        char s1[102],s2[102];
        scanf("%d",&t);
        while(t--)
        {
            scanf("%s%s",s1,s2);
            for(j=-1,ans=i=0;s2[i];i++)
            {
                for(j++;s1[j];j++)
                 if(s2[i]==s1[j])
    
                   break;
    
                if(j>=strlen(s1))
                   break;
            }
    
            if(i==strlen(s2))
              ans++;
            for(j=-1,i=strlen(s2)-1;i>=0;i--)
            {
                for(j++;s1[j];j++)
                 if(s2[i]==s1[j])
                   break;
                if(j>=strlen(s1))
                   break;
            }
            if(i==-1)
            {
              if(ans)
                ans=3;
              else
                ans=2;
            }
            printf("%d\n",ans);
        }
        return 0;
    }

Substrings Palindromic

  • Problem Description

    Chef likes strings a lot but he likes palindromic strings more. Today, Chef has two strings A and B, each consisting of lower case alphabets.

    Chef is eager to know whether it is possible to choose some non empty strings s1 and s2 where s1 is a substring of A, s2 is a substring of B such that s1 + s2 is a palindromic string. Here + denotes the concatenation between the strings.

    Note:

    A string is a palindromic string if it can be read same both forward as well as backward. To know more about palindromes click here.
  • Test Case 1

    Input (stdin)
    1
    
    aba
    
    aabc
    
    
    Expected Output
    No
  • Test Case 2

    Input (stdin)
    1
    
    qwer
    
    abcbb
    
    
    Expected Output
    No
  • Program
  • #include <stdio.h>
    #include <string.h>
    int main(void)
    {
      int t; scanf("%d",&t);
      while(t--)
      {
        char a[1000]={'\0'},b[1000]={'\0'};
        scanf("%s",a);
        scanf("%s",b);
        int c1[26]={0},c2[26]={0},i,j,flag=0;
        for(i=0;i<1000;i++) c1[a[i]-'a']++;
        for(i=0;j<1000;j++) c2[b[j]-'b']++;
        for(i=0;i<26;i++)
        {
          if(c1[i]>0 && c2[i]>0) {flag=1; break;}
          else flag=0;
        }
        if(flag==1) 
          printf("Yes\n");
        else
          printf("No\n");
      }
     return 0;
    }

W String

  • Problem Description

    Kira likes to play with strings very much. Moreover he likes the shape of W very much. He takes a string and try to make a W shape out of it such that each angular point is a # character and each sides has same characters. He calls them W strings.

    For example, the W string can be formed from "aaaaa#bb#cc#dddd" such as:

    a
    a d
    a # d
    a b c d
    a b c d
    # #
    He also call the strings which can generate a W shape (satisfying the above conditions) W strings.

    More formally, a string S is a W string if and only if it satisfies the following conditions (some terms and notations are explained in Note, please see it if you cannot understand):

    The string S contains exactly 3 # characters. Let the indexes of all # be P1 < P2 < P3 (indexes are 0-origin).
    Each substring of S[0, P11], S[P1+1, P21], S[P2+1, P31], S[P3+1, |S|1] contains exactly one kind of characters, where S[a, b] denotes the non-empty substring from a+1th character to b+1th character, and |S| denotes the length of string S (See Note for details).
    Now, his friend Ryuk gives him a string S and asks him to find the length of the longest W string which is a subsequence of S, with only one condition that there must not be any # symbols between the positions of the first and the second # symbol he chooses, nor between the second and the third (here the "positions" we are looking at are in S), i.e. suppose the index of the #s he chooses to make the W string are P1, P2, P3 (in increasing order) in the original string S, then there must be no index i such that S[i] = # where P1 < i < P2 or P2 < i < P3.

    Help Kira and he wont write your name in the Death Note
  • Test Case 1

    Input (stdin)
    1
    
    aaaaa#bb#cc#dddd
    
    
    Expected Output
    16
  • Test Case 2

    Input (stdin)
    1
    
    acb#aab#bab#accba
    
    
    Expected Output
    10
  • Program
  • #include<stdio.h>
    #include<string.h>
    #include<malloc.h>
     
    //using namespace std;
    void program();
    char s[10003];
    int *max1, *max2, *max3;
    int k1=0;
     
    void fxn(int n)
    {
        int x[30]={0};
        int x2[30]={0};
        int x3[30]={0};
        int max=0,i,max_2=0,max_3=0;
        int j,k=0;
        for(i=0;i<n;i++)
        {
            //printf("%c",s[i]);
            if(s[i]=='#')
            {
                max1[k]=max;
                max2[k]= max_2;
                for(j=0;j<30;j++) x2[j]=0;
                max_2=0;
                k++;
                continue;
            }
            x[s[i]-'a']++;
            x2[s[i]-'a']++;
            if(x2[s[i]-'a']>max_2)
            max_2= x2[s[i]-'a'];
     
            if(x[s[i]-'a']>max)
            max= x[s[i]-'a'];
        }
        k--;
        k1=k;
        for(i=n-1;i>=0;i--)
        {
            if(s[i]=='#')
            {
                max3[k]= max_3;
                k--;
                continue;
            }
            x3[s[i]-'a']++;
            if(x3[s[i]-'a'] >max_3)
            max_3= x3[s[i]-'a'];
        }
     
     
    }
    int main()
    {
        int t;
        scanf("%d",&t);
        while(t--)
        program();
        return 0;
    }
     
    void program()
    {
        //char s[10003];
        scanf("%s",s);
        //int x[30]= {0};
        int max=0,t,i,count=0;
        //vector<int> v;
        int n;
        for(i=0;s[i]!='\0';i++)
        {
            if(s[i]=='#')
            count++;
        }
        max1= (int*)malloc(sizeof(int)*count);
        max2= (int*)malloc(sizeof(int)*count);
        max3= (int*)malloc(sizeof(int)*count);
        for(i=0;i<count;i++)
        {
            max1[i]=0;
            max2[i]=0;
            max3[i]=0;
        }
        n=strlen(s);
        max=0;
        fxn(n);
        int a,b,c,d;
        for(i=2;i<count;i++)
        {
          a= max1[i-2];
          b= max2[i-1];
          c= max2[i];
          d= max3[i];
           //printf("%d %d %d %d \n",a,b,c,d);
            if(a!=0 && b!=0 && c!=0 && d!=0)
            {
              t= a+b+c+d;
              if(t>max)
              max=t;
            }
        }
        if(max==0)
        printf("%d\n",max);
        else
        printf("%d\n",max+3);
    } 

Consecutive String Copy

  • Problem Description

    Write a program that will copy m consecutive characters from a string s1 beginning at position n into another string s2

    Refer sample Input and Output:
    1. The first input corresponds to the string input
    2. The Second Input corresponds to the number of characters to be copied
    3. The third input corresponds to the string index

    Sample Input 1:
    SRMUniversity
    5
    0
    Output 1:
    SRMUn

    Explanation:
    The String index starts from 0 and add five characters after that
    S=0
    R=1
    M=2
    U=3
    n=4

    Sample Input 2:
    SRMAPPLELAB
    6
    3

    Output:
    APPLEL


    Explanation:
    The String index starts from 3 and add six characters after that
    S=0
    R=1
    M=2
    A=3
    P=4
    P=5
    L=6
    E=7
    L=8
    A=9
    B=10

    Output: APPLEL
  • Test Case 1

    Input (stdin)
    SRMUniversity
    
    5
    
    0
    
    
    Expected Output
    SRMUn
  • Test Case 2

    Input (stdin)
    SRMAPPLELAB
    
    6
    
    3
    
    
    Expected Output
    APPLEL
  • Program
  • #include <stdio.h>
    #include<string.h>
    #include <stdlib.h>
    
    int main()
    {
        
      char a[50];
      int c,b,i;
      scanf("%s",a);
      scanf("%d%d",&c,&b);
      for(i=b;i<b+c;i++)
        printf("%c",a[i]);
      
      return 0;
    }
    
    

Conversion Code -02

  • Problem Description

    Write a C program to convert uppercase string to lowercase using inbuilt string library function strlwr(). How to use strlwr library function in C programming. Internally characters in C are represented as an integer value known as ASCII value. Which means if we write a or any other character it is translated into a numeric value in our case it is 97 as ASCII value of a = 97. 

    Here what we need to do is first we need to check whether the given character is upper case alphabet or not. If it is uppercase alphabet just add 32 to it which will result in lowercase alphabet (Since ASCII value of A=65, a=97 their difference is 97-65 = 32).
  • Test Case 1

    Input (stdin)
    SRMUNIVERSITYLEARNINGCENTRE
    
    
    Expected Output
    srmuniversitylearningcentre
  • Test Case 2

    Input (stdin)
    SRMAPPLELAB
    
    
    Expected Output
    srmapplelab
  • Program
  • #include <stdio.h>
    #include<string.h>
    int main()
    {
      char str[100];
      int i;
      scanf("%s",str);
      for(i=0;i<=strlen(str);i++)
      {
        if(str[i]>=65&&str[i]<=90)
          str[i]=str[i]+32;
      }
      printf("%s",str);
       
      return 0;
    }

Warm up

  • Problem Description

    This is a very easy warm-up problem.

    You are given a string. Your task is to determine whether number of occurrences of some character in the string is equal to the sum of the numbers of occurrences of other characters in the string. 
    Input

    The first line of the input contains an integer T denoting the number of test cases. Each of the next T lines contains one string S consisting of lowercase latin letters.
    Output

    For each test case, output a single line containing ""YES"" if the string satisfies the condition given above or ""NO"" otherwise.
    Constraints
    1 <= T <= 1000
    1 <= length of S <= 50
  • Test Case 1

    Input (stdin)
    4
    
    acab
    
    zzqzqq
    
    abc
    
    kklkwwww
    
    
    Expected Output
    YES
    
    YES
    
    NO
    
    YES
  • Test Case 2

    Input (stdin)
    3
    
    good
    
    beauty
    
    study
    
    
    Expected Output
    YES
    
    NO
    
    NO
  • Program
  • #include<stdio.h>
    #include<string.h>
    int main()
    {
     char s[200];
     int t,len,arr[26],i,f;
     scanf("%d",&t);
     while(t--)
     {
      for(i=0;i<26;i++) arr[i] = 0;
      scanf("%s",s);
      len = strlen(s);
      for(i=0;i<len;i++)
      {
       arr[s[i] - 'a']++;
      }
      f = 0;
      for(i=0;i<26;i++)
      {
       if(arr[i] == len - arr[i]) { f = 1;break; }
      }
      if(f == 1) printf("YES\n");
      else printf("NO\n");
     }
     return(0);
    }
    

Palindrome

  • Problem Description

    Teacher gave homework to the students that their task is to find a stirng which should remains the same even when we reverse it. Paul was interested in finding such string. Help paul by writing a code to find whether the given string is palindrome or not.
  • Test Case 1

    Input (stdin)
    mam
    
    
    Expected Output
    Palindrome
  • Test Case 2

    Input (stdin)
    Educate
    
    
    Expected Output
    Not palindrome
  • Program
  • 
     #include <stdio.h>
    #include <string.h>
    
    int main(){
        char string1[20];
        int i, length;
        int flag = 0;
        
        
        scanf("%s", string1);
        
        length = strlen(string1);
        
        for(i=0;i < length ;i++){
            if(string1[i] != string1[length-i-1]){
                flag = 1;
                break;
       }
    }
        
        if (flag) {
            printf("Not palindrome");
        }    
        else {
            printf("Palindrome");
        }
      return 0;
    }

Thursday, 27 September 2018

Compare 2 arrays

  • Problem Description

    Write a program to find whether 2 arrays are the same.

    Input Format:

    Input consists of 2n+1 integers. The first integer corresponds to "n" , the size of the array. The next "n" integers correspond to the elements in the first array. The next "n" integers correspond to the elements in the second array.Assume that the maximum value of n is 15.

    Output Format:

    Print yes if the 2 arrays are the same. Print no if the 2 arrays are different.
  • Test Case 1

    Input (stdin)
    5
    
    2 3 6 8 -1
    
    2 3 6 8 -1
    
    
    Expected Output
    yes
  • Test Case 2

    Input (stdin)
    5
    
    2 3 6 8 -1
    
    2 3 6 8 10
    
    
    Expected Output
    no
  • Program
  • #include <stdio.h>
    int main()
    {
      int i,j,n,a[50],b[50],count=0;
      scanf("%d",&n);
      for(i=0;i<n;i++)
      {
        scanf("%d ",&a[i]);
      scanf("%d ",&b[i]);
    }
      for(i=0;i<n;i++)
      {
        for(j=0;j<n;j++)
          if(a[i]==b[j])
            count=count+1;
      }
      if(count==n)
      printf("yes");
      else
      printf("no");
      return 0;
    }

Sum of positive Numbers

  • Problem Description

    Write a program to find the sum of positive numbers in an array.

    Input Format:
    Input consists of n+1 integers. The first integer corresponds to n , the size of the array. The next ?n? integers correspond to the elements in the array. Assume that the maximum value of n is 15.

    Output Format:
    Refer sample output for details
  • Test Case 1

    Input (stdin)
    5
    
    2 3 6 8 -1
    
    
    Expected Output
    sum=19
  • Test Case 2

    Input (stdin)
    5
    
    -1 -2 -3 -4 -5
    
    
    Expected Output
    sum=0
  • Program
  • #include <stdio.h>
    int main()
    {
      int num,a[100],sum=0,i;
      scanf("%d",&num);
      for(i=0;i<num;i++)
        scanf("%d",&a[i]);
      for(i=0;i<num;i++)
      {
        if(a[i]>=0)
          {
            sum=sum+a[i];
          }
      }
      printf("sum=%d",sum);
      return 0;
    }

Alternate

  • Problem Description

    There is a machine in a lab which is working not properly. It displays the alternative elements in a set of input. The lab instructor asked the students to write a C Program for this scenario. i.e. to Print the Alternate Elements in an Array
  • Test Case 1

    Input (stdin)
    5
    
    21 8 41 13 15
    
    
    Expected Output
    21 41 15
  • Test Case 2

    Input (stdin)
    9
    
    2 8 4 1 3 6 8 11 38
    
    
    Expected Output
    2 4 3 8 38
  • Program
  • #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int i,n;
        
        scanf("%d",&n);
        int a[n];
        for(i=0;i<n;i++)
        {
            
            scanf("%d",&a[i]);
        }
        printf("\n");
        for(i=0;i<n;i++)
            //printf("%d \t",a[i]);
    
        printf("\n\t");
        for(i=0;i<n;i=i+2)
           printf("%d \t",a[i]);
    
        return 0;
    }

Occurrence

  • Problem Description

    There was a group of elements in which there may be repeated elements. So there was a task to find occurrence of an element in one dimensional array. Write a C program for this scenario
  • Test Case 1

    Input (stdin)
    6
    
    10 20 30 10 50 60
    
    10
    
    
    Expected Output
    2
  • Test Case 2

    Input (stdin)
    3
    
    1 2 3
    
    3
    
    
    Expected Output
    1
  • Program
  • #include <stdio.h>
    int main()
    {
    int n,i,arr[10],c,count=0;
      scanf("%d",&n);
      for(i=0;i<n;i++)
      {
        scanf("%d",&arr[i]);
      }
      scanf("%d",&c);
      for(i=0;i<n;i++)
      {
        if(c==arr[i])
      count++;
      }
      printf("%d",count);
     return 0;
    }

Catch the Second..

  • Problem Description

    A bag contains set of integers and it is discovered by Ram. There is sheet in that bag. The person who found the bag should find the second largest number from the set of numbers. If the rightly found the will get a gold.. If you wish get the prize you can also try...
  • Test Case 1

    Input (stdin)
    5
    
    5 3 1 2 6
    
    
    Expected Output
    5
  • Test Case 2

    Input (stdin)
    6
    
    16 58 2 1 6 99
    
    
    Expected Output
    58
  • Program
  • #include <stdio.h>
    int main()
    {
      int a[50],size,i,j=0,big,secondbig;
      scanf("%d",&size);
      for(i=0;i<size;i++)
        scanf("%d",&a[i]);
      big=a[0];
      for(i=1;i<size;i++)
      {
        if(big<a[i])
        {
          big=a[i];
          j=i;
        }
      }
      secondbig=a[size-j-1];
      for(i=1;i<size;i++)
      {
        if(secondbig<a[i]&&j!=i)
          secondbig=a[i];
      }
      printf("%d",secondbig);
      return 0;
    }

Duplicate Removal

  • Problem Description

    Program to Delete duplicate elements from an array
  • Test Case 1

    Input (stdin)
    5
    
    1 3 4 5 3
    
    
    Expected Output
    1 3 4 5
  • Test Case 2

    Input (stdin)
    3
    
    1 3 3
    
    
    Expected Output
    1 3
  • Program
  • #include <stdio.h>
    int main()
    {
    int arr[20], i, j, k, size;
     
       
       scanf("%d", &size);
     
       
       for (i = 0; i < size; i++)
          scanf("%d", &arr[i]);
     
       printf("\n");
       for (i = 0; i < size; i++) {
          for (j = i + 1; j < size;) {
             if (arr[j] == arr[i]) {
                for (k = j; k < size; k++) {
                   arr[k] = arr[k + 1];
                }
                size--;
             } else
                j++;
          }
       }
     
       for (i = 0; i < size; i++) {
          printf("%d ", arr[i]);
       }
     return 0;
    }