PPS Q-Bank Unit : 03

31. Short note on String.

Ans: In C language, a string is an array of characters terminated by a null character ‘\0’. The null character is used to indicate the end of the string. For example, the string “hello” is represented in C as an array of characters {‘h’, ‘e’, ‘l’, ‘l’, ‘o’, ‘\0’}. The length of this string is 5 characters, but the size of the array is 6 (including the null character). C provides a number of built-in functions to work with strings. Here are some commonly used string functions in C:

strlen(): calculates the length of a string (excluding the null character).

strcpy(): copies a string from one location to another.

strcat(): concatenates two strings into one. strcmp(): compares two strings to see if they are equal.

strchr(): searches a string for a given character and returns a pointer to its first occurrence.

To use these functions, you need to include the header file string.h. C also provides a way to declare a string literal directly in your code, without having to manually create an array of characters. For example, you can write “hello” instead of {‘h’, ‘e’, ‘l’, ‘l’, ‘o’, ‘\0’}. The compiler automatically creates an array of characters for you and adds the null character at the end.

32. Explain different methods to read and print string.

Ans: 

1.Using printf() and scanf() functions:

You can use the printf() function to print a string, and the scanf() function to read a string. The %s format specifier is used to specify a string in both functions. Here’s an example:

#include <stdio.h>
#include <conio.h>
void main() {
    char str[100];
    clrscr();
    printf("Enter a string: ");
    scanf("%s", str);
    printf("You entered: %s", str);
    getch();
}

2.Using puts() and gets() functions:

You can use the puts() function to print a string, and the gets() function to read a string. The puts() function automatically adds a newline character after the string, while gets() reads a string until a newline character is encountered. Here’s an example:

#include <stdio.h>
#include <conio.h>
void main() {
    char str[100];
    clrscr();
    printf("Enter a string: ");
    gets(str);
    printf("You entered: ");
    puts(str);
    getch();
}

3.Using fgets() and fputs() functions:

You can use the fgets() function to read a string from a file, and the fputs() function to write a string to a file. These functions are more secure than gets() and puts() because you can specify the maximum number of characters to be read or written. Here’s an example:

#include <stdio.h>
#include <conio.h>
void main() {
    FILE *fp;
    char str[100];
    clrscr();
    fp = fopen("test.txt", "w");
    printf("Enter a string: ");
    fgets(str, 100, stdin);
    fputs(str, fp);
    fclose(fp);
    printf("String written to file!");
    getch();
}

33. Explain following : strlen, strcpy, strncpy, strcat, strncat, strcmp, strncmp, strstr, strchr, strrchr

Ans :

  • strlen(): This function is used to determine the length of a string, i.e., the number of characters in the string. It takes a string as input and returns an integer value representing the length of the string.
  • strcpy(): This function is used to copy one string to another. It takes two strings as input – the destination string and the source string – and copies the contents of the source string to the destination string.
  • strncpy(): This function is similar to strcpy(), but it allows you to specify the maximum number of characters to be copied from the source string to the destination string.
  • strcat(): This function is used to concatenate two strings. It takes two strings as input – the destination string and the source string – and appends the contents of the source string to the end of the destination string.
  • strncat(): This function is similar to strcat(), but it allows you to specify the maximum number of characters to be appended from the source string to the destination string.
  • strcmp(): This function is used to compare two strings. It takes two strings as input and returns an integer value indicating whether the strings are equal or not. If the two strings are equal, it returns 0. If the first string is greater than the second string, it returns a positive value. If the first string is less than the second string, it returns a negative value.
  • strncmp(): This function is similar to strcmp(), but it allows you to specify the maximum number of characters to be compared from the two strings.
  • strstr(): This function is used to search for a substring within a string. It takes two strings as input – the main string and the substring to be searched for – and returns a pointer to the first occurrence of the substring in the main string. If the substring is not found, it returns a NULL pointer.
  • strchr(): This function is used to search for a character within a string. It takes two arguments – a string and a character to search for – and returns a pointer to the first occurrence of the character in the string. If the character is not found, it returns a NULL pointer.
  • strrchr(): This function is similar to strchr(), but it searches for the last occurrence of the character in the string.

34. Any Program of String could be asked.

click here … For String programs. ( Program 10 – 15)

35. What is function? Why it is important?

Ans : A function is a named block of code that performs a specific task. It is important because it promotes code reusability, modularity, organization, and abstraction, making the code more readable, maintainable, and scalable. Functions also facilitate code testing, debugging, and enhance software development efficiency.

Important :

  • Using function we can avoid rewriting the same logic or code again and again in a program.
  • We can track or understand large program easily when it is divide into functions.
  • It provides reusability.
  • It help in testing and debugging because it can be tested for errors individually in the easiest way.
  • Reduction in size of program due to code of a function can be used again and again, by calling it.

36. Explain the Parts of function : function declaration(function header/prototype),calling function, function definition(called function).

1) Function Declaration (Function Header/Prototype):

  • The function declaration, also known as the function header or prototype, defines the function’s name, return type, and parameters.
  • It declares the existence and signature of the function without providing its implementation.
  • It serves as a blueprint for the function, allowing other parts of the program to know how to use the function correctly. Example:
   // Function declaration/prototype
   int addNumbers(int a, int b);

2) Calling a Function:

  • To use a function in your code, you need to call it.
  • Function calls typically include the function name followed by parentheses, which may contain arguments (values) passed to the function.
  • When a function is called, the control transfers to that function, executes its code, and returns to the calling point after completion. Example:
   // Function call
   int result = addNumbers(5, 3);

3) Function Definition (Called Function):

  • The function definition, also known as the called function, provides the actual implementation of the function.
  • It includes the function’s name, return type, parameters, and the block of code that is executed when the function is called.
  • The code within the function definition performs the desired operations and may return a value using the return statement. Example:
   // Function definition
   int addNumbers(int a, int b) {
       int sum = a + b;
       return sum;
   }

In the example above, we have a function named addNumbers. Its function declaration or prototype specifies that it takes two integer parameters (int a and int b) and returns an integer (int). The function call addNumbers(5, 3) passes the values 5 and 3 as arguments, and the function definition performs the addition operation and returns the sum.

37. Explain type (category) of Functions. Explain any two in detail.

Note : We have Explain all.

Ans :

  • Library Functions: These are the functions that are provided by the C standard library. They are predefined and can be used by programmers in their programs without having to write the function code. Some examples of library functions in C programming are printf(), scanf(), malloc(), strlen(), strcpy(), sin(), cos(), sqrt(), and exit(). These functions are grouped into various header files, such as stdio.h, stdlib.h, math.h, and string.h, among others.
  • User-defined Functions: These are the functions that are created by programmers to perform specific tasks in their programs. These functions are defined by the programmer and can be called from other parts of the program to perform the task. User-defined functions are useful in breaking down a large program into smaller, more manageable pieces, making the code more modular and easier to maintain.
  • Recursive Functions: These are the functions that call themselves during their execution. Recursive functions are useful in solving problems that can be broken down into smaller subproblems, such as the Fibonacci sequence and the factorial of a number.
  • Inline Functions: These are the functions that are expanded inline by the compiler during compilation. The inline functions are used to optimize the performance of a program by avoiding the overhead of function call and return. The inline function code is copied and pasted at the point of function call, eliminating the overhead of function call and return.
  • Function Pointers: These are the pointers that point to a function in memory. Function pointers are useful in passing functions as arguments to other functions or in assigning a function to a variable.

38. Explain nesting of function.

Let us see first what is basically a nested function in any language. In lemman’s language, nested functions are nothing but a function defined inside another function. The scope of a nested function lies within the function in which it is declared and defined (enclosing function). Any local function, variable, constant, type, class, etc., that is in the same scope or any enclosing scope can be accessed by a nested function. This is the basic difference between nested functions and any other function, i.e., they have access to, and control over variables set out in their parent functions. 

#include <stdio.h>
void main_func1() {
    printf("this is the main function\n");

    void nest_func1() {
        printf("nested function one\n");

        void nest_func2() {
            printf("nested function two\n");
        }
        nest_func2();
    }
    nest_func1();
}
int main(void) {
    main_func1();
    return 0;
}

39. Explain recursion.

  • Any function which calls itself is called recursive function and such function calls are called recursive calls. 
  • Recursion cannot be applied to all problems, but it is more useful for the tasks that can be defined in terms of a similar subtask.
  • It is idea of representing problem a with smaller problems.
  • Any problem that can be solved recursively can be solved iteratively.
  • When recursive function call itself, the memory for called function allocated and different copy of the local variable is created for each function call.
  • Some of the problem best suitable for recursion are
    • Factorial
    • Fibonacci
    • Tower of Hanoi

40. Explain Scope , visibility and Lifetime of a variable.

Scope, visibility, and lifetime are important concepts related to variables in programming. Let’s understand each term:

1) Scope:

  • Scope refers to the region or portion of code where a variable is visible and can be accessed.
  • It determines the accessibility and visibility of a variable.
  • Variables have different scopes depending on where they are declared.
  • The scope of a variable determines where it can be accessed and used within a program.

Example of Scope:

#include <stdio.h>
#include<conio.h>
void main() {
    int x = 5; // Variable x is declared within the main function and has scope limited to the main function.
    if (x > 0) {
        int y = 10; // Variable y is declared within the if statement block and has scope limited to the if block.
        printf("x: %d, y: %d\n", x, y); // Both x and y are accessible within this block.
    }
    // printf("%d", y); // Error: y is not accessible here as its scope is limited to the if block.
    getch();
}

In the example above, the variables x and y have different scopes. The scope of x is the entire main function, so it can be accessed within the if block as well. However, the scope of y is limited to the if block, and it cannot be accessed outside of it.

2) Visibility:

  • Visibility refers to the ability to access or refer to a variable in a particular portion of code.
  • It is determined by the scope of the variable.
  • Variables with a larger scope are visible to smaller scopes, but variables with a smaller scope are not visible to larger scopes.

3) Lifetime:

  • The lifetime of a variable is the duration for which it exists in the memory during program execution.
  • It determines the period in which a variable retains its value.
  • Variables have different lifetimes based on where and how they are declared.
  • The lifetime of a variable is associated with its scope.

Example of Lifetime:

#include<stdio.h>
#include<conio.h>
int globalVariable; // Global variable has a lifetime throughout the program execution.
void main() {
    int localVariable = 5; // Local variable has a lifetime within the main function.
    if (localVariable > 0) {
        int blockVariable = 10; // Variable has a lifetime within the if block.
        printf("blockVariable: %d\n", blockVariable);
    }
    printf("localVariable: %d\n", localVariable);
    getch();
}

In the example above, the global variable globalVariable has a lifetime throughout the program execution, as it is declared outside any function. The local variable localVariable has a lifetime limited to the main function. The block variable blockVariable has a lifetime limited to the if block.

41. Any Program with user defined function could be asked.

Click Here. For function programs ( Program 04 – 10)

Leave a Comment

Your email address will not be published. Required fields are marked *

error: Content is protected !!
Scroll to Top