Thursday 4 February 2021

String Questions

Q) Today, Monk went for a walk in a garden. There are many trees in the garden and each tree has an English alphabet on it. While Monk was walking, he noticed that all trees with vowels on it are not in good state. He decided to take care of them. So, he asked you to tell him the count of such trees in the garden. Note : The following letters are vowels: 'A', 'E', 'I', 'O', 'U' ,'a','e','i','o' and 'u'. Input: The first line consists of an integer T denoting the number of test cases. Each test case consists of only one string, each character of string denoting the alphabet (may be lowercase or uppercase) on a tree in the garden. Output: For each test case, print the count in a new line.

Q)  John is fond of vowels. In a line of text, he wanted to see all lower-case vowels in uppercase & vice-versa. Implement a C program to perform the following.

Input: India is my cOuntry

Output: indIA Is my country

 

Q2) Write a user-defined function in C to concatenate first n characters of string2 with string1 considering string1, string2 and n as arguments.

Q3) Write a program that extracts part of the given string from the specified index. For example, if the string is " Working with string is fun", then if from index 3, 4 characters are to be extracted then the program should return string as "king". Moreover, if the index from where the string is to be extracted is given and the number of characters to be extracted is 0 then the program should extract entire string from the specified index.

Q4) Array of Strings concept: Write a program to sort the color names given in set in alphabetical order Sample Input: violet, blue, green, orange, red, aqua. Sample Output: aqua, blue, green, orange, red, violet.

Q5) Consider two strings s1 and s2 and compare the 2 strings alphabetically without using and string handling functions (case insensitive comparison).

Sample Input: s1=” Hai” s2=”hai”

Sample Output: Both strings are similar

Q6) Implement a C program to reverse a given string using a user-defined recursive function.

Sample Input: KLU is in Guntur

Sample Output: rutnuG ni si ULK

Wednesday 3 February 2021

Questions on Structures Concept

Design a structure Account to store the name, account number and balance of customer. Use array of structures and develop the appropriate C functions for the following operations a. Store Customer information, b. Add $100 in the balance of all the customers having more than $1000 in their balance and then print the incremented value of the updated record. 

A college library has many books. A librarian gives the task of automating the following activities. Define the structure for book with the following fields: ISBN, book title, author, year of publishing, price and number of copies. The task is to write a C program

to store the details of 10 books. Write a menu driven program and implement the following functions: a) Display() – displays the data of all available books b) Search() – searches for a given book based on ISBN number and prints the data of the book if available. Otherwise, print “No such book available” c) Sort() – sorts the data based on ISBN number.


Define structure date containing three members day, month and year. Use this structure to update the given date by one day. If day is the last day of the month, the month field must be incremented by one. If month is 12, the value of the year field must be changed when the day is 31. Also check for leap year. Write a program to update the date by using structure for above mentioned conditions.

Define 2 structures to store the student details and marks. student structure should contain members like name, age, height, address (consisting of street name, city, state, pin code) and marks structure should contain members like three subject marks, cgpa, grade and branch. Define whatever data types are needed. How many bytes are needed to store the student and marks structures


Create a structure to specify data of students given below: Roll number, Name, Department, Course, Year of joining Assume that there are not more than 20 students enrolled in the course and read data for 15 students. (a) print () function to print names of all students who joined in a particular year. (b) search () function to print the data of a student whose roll number is given as

argument. (c) count () function to count the number of students who joined on or before a specified year. (d) append () function to insert the data of new student.


Define a structure data type named date containing three integer members day, month and year. Write a function to read 10 different dates into structure members and print the date in the following format. January 09,2001


Define user defined data type Book. A Book structure contains its title, author name, ISDN number and price. Implement a program to read N number of books information and then arrange those records in sorted order based on its price.


Define a structure student with fields name, id, branch, total_marks. Read the data for 10 students and print the data of students who secured more than 40 marks.


Define a structure named point with two fields representing x and y coordinates of a point. Implement c program that takes the coordinates of 10 points as input and count how many points belong to quadrants 1,2,3 and 4.


Define a structure for an inventory item consisting of 5 fields: part number, part name, description, number of items currently on hand (integer), and price per unit. Allocate memory for 1 item using DMA function. Read the data through keyboard and print the data.


11.  Define a user defined data type student. A student structure contains his/her id number, Name and total marks obtained. Develop a program to read and display data for 10 students in class. Also display the details of topper of the class.

 Define 2 structures to store the following student and class schedule data as : cgpa, major, address (consisting of street address, city, state, zip), and class schedule (consisting of up to six class records, each of which has description, time, and days components).Define whatever data types are needed.

How many bytes are needed to store the above student & class schedule structures, by assuming 2-Bytes for integer and 1-Byte for a Character?

Q) There are 100 records present in a file with the following structure:

Struct date{ int d,m,y;};

Struct employee

{

int empcode[6];

char empname[20];

struct date join_date;

float salary;

};

Write a program to read these records, arrange them in ascending order of join_date and print them.


Define a structure type called subscriber that contains the components as name, street, address and bill amount (i.e. the amount the subscriber needs to pay). Implement a C program to

a. read the above inputs for each of 10 subscribers.

b. sort the subscriber records in ascending order based on the bill amount and print each subscriber record after sorting.

c. count the number of subscribers who have minimal due (i.e. <1000).


Write a C program to create a structure for a patient with the following fields: name, age, gender, height, weight. The program should read data for 10 patients and print the details of patients with age >=60.

Tuesday 2 February 2021

Interview questions that will make you think

Q1)  Suppose you have nine coins and one of them is lighter and all other 8 are of same weight. you have to identify which one is lighter by weighing only two times in weighing balance .

Convert nested loops into recursion in C, patterns using recursion

 #include <stdio.h>

int main()
{
    int r,c, n,nr,nc,c1=1,c2=1;
    scanf("%d", &n);
    nr=2*n-1;
    nc=2*n-1;
    for(r=1;r<=nr;r++)
    {
        for(c=1;c<=nc;c++)
        {
            if(c<c1||c>c2)
                printf(" ");
            else
                printf("*");
        }
        if(r<n)
        {
            c1++;
            c2=c2+2;
        }
        else
        {
            c1--;
            c2=c2-2;
        }
        printf("\n");
    }
}

Consider a nested loop as shown above..
First i tried converting inner c loop into recursion and i got this code below

#include <stdio.h>

void loop(int c,int nc,int c1,int c2)
{
    if(c>nc)
        return;
    if(c<c1||c>c2)
        printf(" ");
    else
        printf("*");
    loop(c+1,nc,c1,c2);
}

int main()
{
    int r,c, n,nr,nc,c1=1,c2=1;
    scanf("%d", &n);
    nr=2*n-1;
    nc=2*n-1;
    for(r=1;r<=nr;r++)
    {
        loop(1,nc,c1,c2);
        if(r<n)
        {
            c1++;
            c2=c2+2;
        }
        else
        {
            c1--;
            c2=c2-2;
        }
        printf("\n");
    }
}

Now further converting outer r loop the program would like this as shown below

#include <stdio.h>

void loop(int c,int nc,int c1,int c2)
{
    if(c>nc)
        return;
    if(c<c1||c>c2)
        printf(" ");
    else
        printf("*");
    loop(c+1,nc,c1,c2);
}
void pattern(int r,int n,int c1,int c2)
{
    if(r>2*n-1)
        return;
    loop(1,2*n-1,c1,c2);
    if(r<n)
    {
        c1++;
        c2=c2+2;
    }
    else
    {
        c1--;
        c2=c2-2;
    }
    printf("\n");
    pattern(r+1,n,c1,c2);
    
}
int main()
{
    int r,c, n,nr,nc,c1=1,c2=1;
    scanf("%d", &n);
    nr=2*n-1;
    nc=2*n-1;
    pattern(1,n,c1,c2);
}

Further can be improved as
#include <stdio.h> void loop(int c,int nc,int c1,int c2) { if(c>nc) return; if(c<c1||c>c2) printf(" "); else printf("*"); loop(c+1,nc,c1,c2); } void pattern(int r,int n,int c1,int c2) { if(r>2*n-1) return; loop(1,2*n-1,c1,c2); if(r<n) { printf("\n"); pattern(r+1,n,c1+1,c2+2); } else { printf("\n"); pattern(r+1,n,c1-1,c2-2); } } int main() { int r,c, n,nr,nc,c1=1,c2=1; scanf("%d", &n); nr=2*n-1; nc=2*n-1; pattern(1,n,c1,c2); }