0% found this document useful (0 votes)
9 views8 pages

Drill 6

The document provides an overview of built-in libraries in C programming, focusing on character and string manipulation functions, as well as mathematical functions. It includes examples of various functions such as isalpha, strlen, and sqrt, demonstrating their usage. Additionally, it outlines laboratory exercises for students to implement these concepts in practical programming tasks.

Uploaded by

rafolsjewels1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views8 pages

Drill 6

The document provides an overview of built-in libraries in C programming, focusing on character and string manipulation functions, as well as mathematical functions. It includes examples of various functions such as isalpha, strlen, and sqrt, demonstrating their usage. Additionally, it outlines laboratory exercises for students to implement these concepts in practical programming tasks.

Uploaded by

rafolsjewels1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Surigao Del Norte State University

C COLLEGE OF ENGINEERING & INFORMATION TECHNOLOGY


Narciso Street, Surigao City

Built – in Libraries
DRILL 6

STUDENT NAME :
SECTION :
DATE OF SUBMISSION: SCORE
Score

Engr. Catherine M. Verallo, MSCpE


Instructor
Topic 6. Built in Libraries

I. Learning Objective
At the end of the session, the student must be able to
• Identify the different built-in libraries for string and character manipulation.
• Execute sample programs using built – in libraries.

II. Discussion
Pre – defined Character Functions

C programming provides several pre-defined functions for character manipulation. These


functions allow you to perform various operations on characters, such as checking if a character
is alphabetic or numeric, converting characters to uppercase or lowercase, and more.
Understanding and utilizing these character functions can greatly enhance your ability to handle
and manipulate character data in C programming.

Common Character Manipulation Functions


isalpha - This function checks whether a given character is an alphabetic character (A-Z or
a-z). It returns a non-zero value if the character is alphabetic, otherwise it returns zero. Here's
an example:

#include <stdio.h>
#include <ctype.h>

int main() {
printf("%d\n", isalpha('c')); // displays > 0
return 0;
}

Isdigit - This function checks whether a given character is a digit (0-9). It returns a non-zero
value if the character is a digit, otherwise it returns zero. Here's an example:
#include <stdio.h>
#include <ctype.h>

int main() {
printf("%d\n", isdigit('4')); // displays > 0
printf("%d\n", isdigit('c')); // displays 0
return 0;
}

toupper and tolower – these functions convert a character to uppercase and lowercase,
respectively. They return the converted character.
#include <stdio.h>
#include <ctype.h>

int main() {
2
char ch = 'a';

printf("Original character: %c\n", ch); // Original character: a


printf("Uppercase: %c\n", toupper(ch)); // Uppercase: A
printf("Lowercase: %c\n", tolower(ch)); // Lowercase: a

return 0;
}

Isalnum - This function checks whether a given character is alphanumeric (A-Z, a-z, or 0-9). It
returns a non-zero value if the character is alphanumeric, otherwise it returns zero. Here's an
example:

#include <stdio.h>
#include <ctype.h>
int main() {
printf("%d\n", isalnum('9')); // displays > 0
return 0;
}

Iscntrl – This function checks whether a given character is a control character. Control
characters are non-printable characters used to control the behavior of devices such as printers
and terminals. The iscntrl function returns a non-zero value if the character is a control
character, and zero otherwise. Here's an example:
#include <stdio.h>
#include <ctype.h>

int main() {
printf("%d\n",iscntrl('\n')); // displays > 0
printf("%d\n",iscntrl('A')); // displays 0
return 0;
}

islower and isupper - These functions are used to determine whether a given character is
a lowercase letter (a-z) or uppercase (A – Z). The islower function returns a non-zero value if
the character is in lowercase, and zero otherwise. On the other hand, isupper function returns a
non-zero value if the character is in uppercase, and zero otherwise. Here's an example:
#include <stdio.h>
#include <ctype.h>

int main() {
printf("%d\n", isupper('c')); // displays 0
printf("%d\n", islower('c')); // displays > 0
return 0;
}

3
isspace – this function checks whether a given character is a white space character (space,
tab, newline, etc.). It returns a non-zero value if the character is a white space character,
otherwise it returns zero. Here's an example:
#include <stdio.h>
#include <ctype.h>

int main() {
printf("%d\n", isspace('\n')); // displays > 0
return 0;
}

Isprint - this function is used to determine whether a given character is a printable


character. Printable characters are those that can be displayed on the screen or printed. Here's
an example:
#include <stdio.h>
#include <ctype.h>

int main() {
printf("%d\n", isprint('\n')); // displays > 0
return 0;
}

ispunct – This function is used to determine whether a given character is a punctuation


character. Punctuation characters are those that are not alphanumeric or whitespace
characters. Here's an example:
#include <stdio.h>
#include <ctype.h>

int main() {
printf("%d\n", ispunct('.')); // displays > 0
return 0;
}

Isxdigit – This function is used to determine whether a given character is a hexadecimal


digit (0-9, A-F, or a-f). Here's an example:
#include <stdio.h>
#include <ctype.h>

int main() {
printf("%d\n", isxdigit('f')); // displays > 0
return 0;
}

4
Pre – defined String Functions

C programming provides a set of standard string functions that allow you to perform various
operations on strings. These functions simplify string manipulation, searching, and other
common tasks. Understanding and utilizing these string functions can greatly enhance your
ability to handle and manipulate string data in C programming.

Standard String Functions

strlen – This function is used to determine the length of a string. It returns the number of
characters in the string, excluding the null terminator ('\0'). Here's an example:

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

int main() {
char str[] = "Hello, World!";
int length = strlen(str);
printf("Length of the string: %d\n", length);
return 0;
}

Output :
Length of the string: 13".

Strcpy – This function is used to copy one string to another. It copies the contents of the
source string to the destination string, including the null terminator. Here's an example:

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

int main() {
char source[] = "Hello, World!";
char destination[20];
strcpy(destination, source);
printf("Copied string: %s\n", destination);
return 0;
}

Output :
"Copied string: Hello, World!".

Strcat - This function is used to concatenate (append) two strings. It appends the contents of
the source string to the end of the destination string, modifying the destination string. Here's an
example:

#include <stdio.h>

5
#include <string.h>

int main() {
char destination[20] = "Hello, ";
char source[] = "World!";
strcat(destination, source);
printf("Concatenated string: %s\n", destination);
return 0;
}

Output :
The output will be: "Concatenated string: Hello, World!".

Strcmp - This function is used to compare two strings. It returns an integer value that indicates
the relationship between the two strings. If the strings are equal, it returns 0. If the first string is
less than the second string, it returns a negative value. If the first string is greater than the
second string, it returns a positive value. Here's an example:

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

int main() {
char str1[] = "banana";
char str2[] = "banana";
int result = strcmp(str1, str2); // result will be 0
printf("%d", result);
return 0;
}

Output :
The output will be: "0".

String Manipulation and Searching Operations

Strchr – This function is used to search for a character in a string. It returns a pointer to the
first occurrence of the character in the string, or NULL if the character is not found. Here's an
example:

#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, World!";
char ch = 'a';
char *result = strchr(str, ch); // returns NULL
return 0;
}
Output :
The output will be: "NULL".
6
Strstr – This function is used to search for a substring in a string. It returns a pointer to the
first occurrence of the substring in the string, or NULL if the substring is not found. Here's an
example:

#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, World!";
char substring[] = "World";
char *result = strstr(str, substring);
printf("Substring '%s' found at index %ld\n", substring, result - str);
return 0;
}

Output
The output will be: "Substring 'World' found at index 7".

Pre-defined Math Functions


C programming provides a math library that contains a set of pre-defined mathematical
functions. These functions are included in the <math.h> header file and allow you to perform
various mathematical operations, such as calculating square roots, raising numbers to a power,
and evaluating trigonometric functions. This guide introduces the most commonly used math
library functions, along with their usage and examples.

Math Library Functions


Sqrt - The sqrt function is used to calculate the square root of a number. It takes a single
argument and returns the square root as a double value. Here's an example:

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

int main() {
double num = 16;
double result = sqrt(num);
printf("Square root of %.2f is %.2f\n", num, result);
return 0;
}

Output :
The output will be: "Square root of 16.00 is 4.00"

Pow - The pow function is used to raise a number to a power. It takes two arguments: the base
number and the exponent. It returns the result as a double value. Here's an example:
#include <stdio.h>
#include <math.h>
int main() {
double base = 2;
7
double exponent = 3;
double result = pow(base, exponent);
printf("%.2f raised to the power of %.2f is %.2f\n", base, exponent,
result);
return 0;
}
Output :
The output will be: "2.00 raised to the power of 3.00 is 8.00".

sin and cos - The sin and cos functions are used to calculate the sine and cosine of an angle,
respectively. These functions take the angle in radians as an argument and return the
corresponding sine or cosine value as a double value. Here's an example:

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

int main() {
double angle = 0.5;
double sin_result = sin(angle);
double cos_result = cos(angle);
printf("Sine of %.2f is %.2f\n", angle, sin_result);
printf("Cosine of %.2f is %.2f\n", angle, cos_result);
return 0;
}
Output :
"Sine of 0.50 is 0.48" "Cosine of 0.50 is 0.88".

III. Laboratory Exercise.


1. Create a C program that will accept a firstname and surname in any case and output the
complete name in ALL Capital Letters (10 points)
Sample output
Enter First Name : Maria
Enter Last Name : deLa CrUz
Your complete name is MARIA DELA CRUZ

2. Create a C program that will compute the circumference of a circle. Format your output
in 2 decimal places (15 points)

3. Create a C program that will accept an integer and output the Sine, Cosine and Tangent
Values (15 points)

References
1. CodeChum
2. Coursera.org

You might also like