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 stringprintf("Length of str1: %d\n", strlen(str1));// strcat(): concatenates two stringsstrcat(str1, str2);printf("Concatenated string: %s\n", str1);// strcmp(): compares two strings lexicographicallyint 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 anotherstrcpy(str1, "Goodbye");printf("New string: %s\n", str1);// strchr(): finds the first occurrence of a character in a stringchar *ptr = strchr(str1, 'b');printf("First occurrence of 'b': %s\n", ptr);// strstr(): finds the first occurrence of a substring in a stringptr = 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 declarationint add(int n1, int n2);int subtract(int n1, int n2);int multiply(int n1, int n2);int divide(int n1, int n2);// main functionint 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 numbersint add(int n1, int n2){int result;result = n1 + n2;return result;}// function to subtract two integer numbersint subtract(int n1, int n2){int result;result = n1 - n2;return result;}// function to multiply two integer numbersint multiply(int n1, int n2){int result;result = n1 * n2;return result;}// function to divide two integer numbersint divide(int n1, int n2){int result;result = n1 / n2;return result;}
0 Comments