Semi-Detailed Lesson Plan in CP1
Semi-Detailed Lesson Plan in CP1
I. Objectives
Topic: String
A. Array of character and string
B. String functions
C. Application of strings in a problem
III. Materials
Computer, Mobile Phone, Internet Connection, PowerPoint Presentation, internet connection.
IV. Procedure
A. Preparation
a. Greet the students, remind them to mark their attendance and have someone to lead the
prayer
B. Motivation
a. After the review the teacher will show flowchart showing the topic.
V. Lesson Proper
A. String is a sequence of characters that are treated as a single data item and terminated by a null
character '\0'. Remember that the C language does not support strings as a data type.
A string is actually a one-dimensional array of characters in C language. These are often used to
create meaningful and readable programs.
The string "home" contains 5 characters including the '\0' character which is automatically added by
the compiler at the end of the string.
Declaration of strings: Declaring a string is as simple as declaring a one-dimensional array. Below is
the basic syntax for declaring a string.
char str_name[size];
Initializing a String: A string can be initialized in different ways. Below is an example to declare a
string with name as str and initialize it with “Minerva”.
0 1 2 3 4 5 6 Index
String name str M i n e r v a
The scanf() function reads the sequence of characters until it encounters whitespace (space, newline,
tab, etc.).
B. String Function
String.h header file supports all the string functions in C language. All the string functions are given
below.
The following are the most commonly used string handling functions.
Method Description
strcat() It is used to concatenate(combine) two strings
strncat() Appends a portion of string to another
strlen() It is used to show the length of a string
strrev() It is used to show the reverse of a string
strcpy() Copies one string into another
strncpy() Copies given number of characters of one string to another
strcmp() It is used to compare two strings
strcmpi() Same as strcmp() function. But, this function negotiates case. “A” and “a” are
treated as same.
C – strcat() function
strcat( ) function in C language concatenates two given strings. It concatenates source string at the
end of destination string.
Syntax for strcat( ) function is given below.
char * strcat ( char * destination, const char * source );
Example:
strcat ( str2, str1 ); – str1 is concatenated at the end of str2.
strcat ( str1, str2 ); – str2 is concatenated at the end of str1.
C – strncat() function
strncat( ) function in C language concatenates (appends) portion of one string at the end of another
string. Syntax for strncat( ) function is given below.
char * strncat ( char * destination, const char * source, size_t num );
Example :
strncat ( str2, str1, 3 ); – First 3 characters of str1 is concatenated at the end of str2.
strncat ( str1, str2, 3 ); – First 3 characters of str2 is concatenated at the end of str1.
C – strlen() function
strlen( ) function in C gives the length of the given string.
Syntax for strlen( ) function is given below.
size_t strlen ( const char * str );
strlen( ) function counts the number of characters in a given string and returns the integer value.
It stops counting the character when null character is found. Because, null character indicates the
end of the string in C.
C – strrev() function
strrev( ) function reverses a given string in C language. Syntax for strrev( ) function is given below.
char *strrev(char *string);
strrev( ) function is non standard function which may not available in standard library in C.
C – strcpy() function
strcpy( ) function copies contents of one string into another string. Syntax for strcpy function is given
below.
char * strcpy ( char * destination, const char * source );
Example:
strcpy ( str1, str2) – It copies contents of str2 into str1.
strcpy ( str2, str1) – It copies contents of str1 into str2.
C – strncpy() function
strncpy( ) function copies portion of contents of one string into another string. Syntax for strncpy( )
function is given below.
char * strncpy ( char * destination, const char * source, size_t num );
Example:
strncpy ( str1, str2, 4) – It copies first 4 characters of str2 into str1.
strncpy ( str2, str1, 4) – It copies first 4 characters of str1 into str2.
C – strcmp() function
strcmp( ) function in C compares two given strings and returns zero if they are same.
If length of string1 < string2, it returns < 0 value. If length of string1 > string2, it returns > 0 value.
Syntax for strcmp( ) function is given below.
int strcmp ( const char * str1, const char * str2 );
strcmp( ) function is case sensitive. i.e, “A” and “a” are treated as different characters.
C – strcmpi() function
strcmpi( ) function in C is same as strcmp() function. But, strcmpi( ) function is not case sensitive. i.e,
“A” and “a” are treated as same characters. Whereas, strcmp() function treats “A” and “a” as
different characters.
strcmpi() function is nonstandard function which may not available in standard library in C.
Both functions compare two given strings and returns zero if they are same.
If length of string1 < string2, it returns < 0 value. If length of string1 > string2, it returns > 0 value.
Syntax for strcmp( ) function is given below.
int strcmpi ( const char * str1, const char * str2 );
VI. Generalization
Just like every other language C, also has a vast library of ready-to-use or in-built functions. To
handle any string of characters, you can use these functions directly. However, the functions are not
limited to these ones. There are many other string functions in C in the header file. Major benefit of them
is to cut the time and length of coding.
VII. Evaluation
Tell the students to answer the assessment at BPC E-Learning Portal.
VIII. Assignment
1. Write a C program that copy one string to another string and find the total number of copied
characters.
2. Write a C program that checks if a string is palindrome or not.
References:
https://www.tutorialspoint.com/cprogramming/c_strings.htm
https://study.com/
https://www.javatpoint.com/c-strings
Prepared by: