Pre Defined Functions in C
Pre Defined Functions in C
Function is a block of code that has a name and used to perform some
specific task. The function contains the set of programming statements
enclosed by curly bracket { }. A function can be called multiple times, so it
is reusable.
Using the function we can divide a large program into small blocks known
as function.
Function Advantages
Function call Function can be called from anywhere in the program. The parameter
list must not differ in function calling and function declaration. We must pass the
same number of functions as it is declared in the function declaration.
Library functions are built-in functions that are grouped together and
placed in a common location called library.
String function strlen() is used to find the length of given string. It does
not count null (‘\0’) character. It returns number of character of the string.
#include <stdio.h>
OUTPUT
#include <string.h>
Enter string : Computer
int main()
String length is : 8
{
char str[25];
printf("Enter string : ");
scanf("%s",&str);
printf(“String length is : %d",strlen(str));
}
strlen() Program - 2
#include <stdio.h>
#include <string.h> OUTPUT
int main()
Enter string : C Fun
{
String length is : 5
char str[25];
int i=0;
printf("Enter string : ");
gets(str);
while(str[i]!='\0')
{
i=i+1;
}
printf(“String length = %d”,i);
}
String Function strrev()
#include <stdio.h>
OUTPUT
#include <string.h>
Enter string : CU
int main()
Reverse string is : UC
{
char str[25];
printf("Enter string : ");
scanf("%s",&str);
printf(“Reverse string is : %s",strrev(str));
}
String Function strcpy()
Example: If you want to copy one string to another the syntax is.
#include <stdio.h>
OUTPUT
#include <string.h>
Enter string : Hello
int main()
Value of str2 is : Hello
{
char str1[25], str2[25];
printf("Enter string : ");
scanf("%s",&str1);
strcpy( str2, str1);
printf(“Value of str2 is : %s",str2);
}
String Function strncpy()
Example: If you want to copy one string to another with specified number
of character the syntax is.
#include <stdio.h>
OUTPUT
#include <string.h>
Value of str2 is : C Pro
int main()
{
char str1[25]=“C Program, str2[25];
int n = 5;
strncpy( str2, str1, n);
printf(“Value of str2 is : %s",str2);
}
strncpy() Program - 2
#include <stdio.h>
#include <string.h> OUTPUT
int main()
{ Enter string : Hello C
char str1[25, str2[25]; Enter no of char : 5
int n; Value of str2 is : Hello
printf(“Enter string : “);
gets(str1);
printf(“Enter no. of character to be copy : “);
scanf(“%d”,&n);
strncpy( str2, str1, n);
printf(“Value of str2 is : %s",str2);
}
String Function strcat()
#include <stdio.h>
OUTPUT
#include <string.h>
Enter string : C Program
int main()
Value of str2 is : C Programming
{
char str1[25];
printf(“Enter string : “);
gets(str1);
strcat( str1, “ming”);
printf(“Value of str1 is : %s",str1);
}
strcat() Program - 2
#include <stdio.h>
OUTPUT
#include <string.h>
Full name is : Peter Smith
int main()
{
char fname[25]=“Peter”, lname[25]=“Smith”;
strcat(fname,” “);
strcat(fname, lname);
printf(“Full name is : %s",fname);
}
String Function strcmp()
#include <stdio.h>
#include <string.h> OUTPUT
int main()
{ Strings are not equal
char str1[25]="Hello";
char str2[25]="hello";
int n=strcmp(str1,str2);
if(n==0)
printf("Both strings are equal");
else
printf("Strings are not equal");
return 0;
}
String Function stricmp()
#include <stdio.h>
#include <string.h> OUTPUT
int main()
{ Strings are equal
char str1[25]="Hello";
char str2[25]="hello";
int n=stricmp(str1,str2);
if(n==0)
printf("Both strings are equal");
else
printf("Strings are not equal");
return 0;
}
String Function strncmp()
#include <stdio.h>
#include <string.h> OUTPUT
int main()
{ Strings are not equal
char str1[25]=“Wonderful";
char str2[25]=“Wonderer";
int n=strncmp(str1,str2,7);
if(n==0)
printf("Both strings are equal");
else
printf("Strings are not equal");
return 0;
}
String Function strlwr()
#include <stdio.h>
OUTPUT
#include <string.h>
Str1 = Hello
int main()
Str2 = hello
{
char str1[25]=“Hello";
printf(“Str1 = %s\n“,str1);
printf(“Lowercase = %s\n“,strlwr(str1));
return 0;
}
String Function strupr()
#include <stdio.h>
OUTPUT
#include <string.h>
Str1 = Hello
int main()
Uppercase = HELLO
{
char str1[25]=“Hello";
printf(“Str1 = %s\n“,str1);
printf(“Uppercase = %s\n“,strupr(str1));
return 0;
}
Character Function
The isdigit function returns a nonzero value if c is digit and returns zero
if c is not a digit.
a) abs (number)
b) ceil (number)
c) floor (number)
d) pow (d1,d2)
e) sqrt (number)
Mathematical Function
c) floor (number) – rounds down the given number. It returns the integer
#include <stdio.h>
#include <ctype.h> OUTPUT
int main()
{ 12
printf("%d",abs(-12)); 4.00
printf("\n%.2f",ceil(3.6)); 4.00
printf("\n%.2f",ceil(3.3)); 3.00
printf("\n%.2f",floor(3.6)); 3.00
printf("\n%.2f",floor(3.3)); 4.00
printf("\n%.2f",sqrt(16)); 2.00
printf("\n%.2f",sqrt(7)); 16.00
printf("\n%.2f",pow(2,4)); 27.00
printf("\n%.2f",pow(3,3));
return 0;
}