Header Ads Widget

Ticker

6/recent/ticker-posts

Practical 11 PPS

 Write a C Program to demonstrate the use of inbuilt string functions.

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

int main()
{
    char str1[20] = "Hello";
    char str2[20] = "World";

    // strlen(): returns the length of the string
    printf("Length of str1: %d\n", strlen(str1));

    // strcat(): concatenates two strings
    strcat(str1, str2);
    printf("Concatenated string: %s\n", str1);

    // strcmp(): compares two strings lexicographically
    int result = strcmp(str1, str2);
    if (result == 0)
    {
        printf("Strings are equal\n");
    }
    else if (result < 0)
    {
        printf("str1 is less than str2\n");
    }
    else
    {
        printf("str1 is greater than str2\n");
    }

    // strcpy(): copies one string to another
    strcpy(str1, "Goodbye");
    printf("New string: %s\n", str1);

    // strchr(): finds the first occurrence of a character in a string
    char *ptr = strchr(str1, 'b');
    printf("First occurrence of 'b': %s\n", ptr);

    // strstr(): finds the first occurrence of a substring in a string
    ptr = strstr(str1, "bye");
    printf("First occurrence of 'bye': %s\n", ptr);

    return 0;
}


Write a function power that computes x raised to the power y for integer x and y and returns double type value.

#include <stdio.h>
#include <conio.h>
#include <math.h>

int power(int a, int b);
void main()
{
    int x, y;
    clrscr();
    printf("enter the x:");
    scanf("%d", &x);
    printf("enter the y:");
    scanf("%d", &y);
    power(x, y);
    getch();
}
int power(int a, int b)
{
    double c;
    c = pow(a, b);
    printf("\n ans=%lf", c);
    return 0;
}

Write a calculator program (add, subtract, multiply, divide). Prepare user defined function for each functionality.

#include <stdio.h>

// functions declaration
int add(int n1, int n2);
int subtract(int n1, int n2);
int multiply(int n1, int n2);
int divide(int n1, int n2);

// main function
int main()
{
    int num1, num2;

    printf("Enter two numbers: ");
    scanf("%d %d", &num1, &num2);

    printf("%d + %d = %d\n", num1, num2, add(num1, num2));
    printf("%d - %d = %d\n", num1, num2, subtract(num1, num2));
    printf("%d * %d = %d\n", num1, num2, multiply(num1, num2));
    printf("%d / %d = %d\n", num1, num2, divide(num1, num2));

    return 0;
}

// function to add two integer numbers
int add(int n1, int n2)
{
    int result;
    result = n1 + n2;
    return result;
}

// function to subtract two integer numbers
int subtract(int n1, int n2)
{
    int result;
    result = n1 - n2;
    return result;
}

// function to multiply two integer numbers
int multiply(int n1, int n2)
{
    int result;
    result = n1 * n2;
    return result;
}

// function to divide two integer numbers
int divide(int n1, int n2)
{
    int result;
    result = n1 / n2;
    return result;
}

Post a Comment

0 Comments