1. CREATED BY K. VICTOR BABU
COMPUTATIONAL THINKING FOR
STRUCTURED DESIGN
STRINGS AND STRING LIBRARY
Department of BES-1
KLEF CTSD BES-1
Mr. B. ASHOK
Assistant Professor
2. CREATED BY K. VICTOR BABU
AIM OF THE SESSION
To familiarize students with the concepts of strings and string library functions in C programming,
enabling them to utilize these concepts effectively in their programming tasks.
INSTRUCTIONAL OBJECTIVES
This Session is designed to:
1. Define and explain the concept of strings in C programming.
2. Utilize various string library functions in C effectively.
3. Apply string manipulation techniques, such as concatenation and comparison, to
manipulate strings.
4. Solve programming problems by employing appropriate string handling functions.
LEARNING OUTCOMES
At the end of this session, you should be able to:
1. Understand strings in C programming.
2. Use string library functions effectively in C.
3. Perform string manipulation tasks, such as concatenation and comparison.
4. Solve programming problems involving string handling.
KLEF CTSD BES-1
3. CREATED BY K. VICTOR BABU
Outline
• Strings in C
• Declaration and initialization of strings
• Understanding different formats for displaying strings
• String library functions to perform string operations
KLEF CTSD BES-1
4. CREATED BY K. VICTOR BABU
Strings in C
KLEF CTSD BES-1
A string is a collection of characters terminated by a null character (‘0’) used to
represent text or sequences of characters. The null character marks the end of the
string.
String manipulation functions like strlen, strcpy, strcat, etc., are available in the C
standard library (string.h) to perform operations on strings.
String literals in C are enclosed in double quotes (e.g., "Hello, World!"). They are
automatically null-terminated and can be assigned to character arrays or used with
pointers to access and manipulate strings.
5. CREATED BY K. VICTOR BABU
String Declaration
• Syntax:
char string_variable_name [size];
• Example: char str [6];
In this representation, each element of character array str is denoted by a question
mark (“?”). Since the array is declared but not initialized, the initial values of the
elements are indeterminate or garbage values.
KLEF CTSD BES-1
6. CREATED BY K. VICTOR BABU
String Initialization
KLEF CTSD BES-1
In C, a string can be initialized in several ways:
1) Using a character array:
i. char str[] = "Hello";
ii. char str[6] = {'H', 'e', 'l', 'l', 'o', '0'};
iii. char str[] = {'H', 'e', 'l', 'l', 'o', '0'};
iv. char str[6];
str[0] = 'H'; str[1] = 'e'; str[2] = 'l'; str[3] = 'l'; str[4] = 'o'; str[5] = '0';
2) Using a pointer to a string literal:
char *str = "Hello";
3) Using the strcpy function:
char str[10];
strcpy(str, "Hello");
Note: Make sure the size of the character
array is sufficient to hold the string,
including the null character.
7. CREATED BY K. VICTOR BABU
Reading and displaying a String
KLEF CTSD BES-1
scanf() function is used to read input from the user and the printf() function to
display the string. Here's an example:
#include <stdio.h>
int main() {
char str[100];
printf("Enter a string: ");
scanf("%s", str);
printf("The entered string is: %sn", str);
return 0;
}
Note: The %s format specifier in scanf
reads a string until it encounters
whitespace, so it may not be suitable for
reading strings with spaces. To read a
string with spaces, fgets() function is used
instead.
8. CREATED BY K. VICTOR BABU
Understanding different formats for displaying
strings
KLEF CTSD BES-1
There are various format specifiers that can be used with the printf function to display strings:
1) %s: This specifier is used to display a null-terminated string.
char str[] = "Hello, World!";
printf("%sn", str);
2) %.*s: This specifier is used to display a string with a specified maximum width. The width is
determined by providing an integer argument before the string.
int precision = 8;
int biggerPrecision = 16;
char greetings[] = "Hello world";
printf("|%.8s|n", greetings);
printf("|%.*s|n", precision , greetings);
printf("|%16s|n", greetings);
printf("|%-16s|n", greetings);
printf("|%*s|n", biggerPrecision , greetings);
Hello, World!!
|Hello wo|
|Hello wo|
| Hello world|
|Hello world |
| Hello world|
9. CREATED BY K. VICTOR BABU
String Library Functions
KLEF CTSD BES-1
Function Description
strlen() Calculates the length of a string
strcpy() Copies a string from source to destination
strcat() Concatenates two strings
strcmp() Compares two strings lexicographically
strstr() Used to search for a substring within a string
The predefined functions that are designed to handle strings are available
in the library “string.h”. They are
10. CREATED BY K. VICTOR BABU
strlen() function
Syntax:
int strlen(const char *str);
Example:
char str[] = "India is my country";
int length = strlen(str);
printf("Length: %dn", length);
KLEF CTSD BES-1
The strlen() is used to calculate the length of a string. It returns the number
of characters in the string, excluding the null character ('0') at the end.
Length: 19
11. CREATED BY K. VICTOR BABU
strcpy() function
Syntax:
char *strcpy(char *dest, const char *src);
Example:
char source[] = "KL Deemed to be University";
char destination[20];
strcpy(destination, source);
printf("Copied string: %sn", destination);
KLEF CTSD BES-1
The strcpy() is used to copy a string from the source to the destination. It
copies each character of the source string to the destination string until it
encounters the null character ('0') that marks the end of the string.
Copied string: KL Deemed to be University
12. CREATED BY K. VICTOR BABU
strcat() function
Syntax:
char *strcat(char *dest, const char *src);
Example:
char destination[20] = "Dennis ";
char source[] = "Ritchie";
strcat(destination, source);
printf("Concatenated string: %sn", destination);
KLEF CTSD BES-1
The strcat() is used to concatenate (append) two strings together. It appends
the contents of the source string to the end of the destination string,
combining them into a single string.
Concatenated string: Dennis Ritchie
13. CREATED BY K. VICTOR BABU
strcmp() function
Syntax:
int strcmp(const char *str1, const char *str2);
Example:
char str1[] = "Baahubali";
char str2[] = "Baahubali";
int result = strcmp(str1, str2);
if (result < 0)
printf("'%s' is lexicographically smaller than '%s'n", str1, str2);
else if (result > 0)
printf("'%s' is lexicographically greater than '%s'n", str1, str2);
else
printf("'%s' is lexicographically equal to '%s'n", str1, str2);
KLEF CTSD BES-1
The strcmp() function is used to compare two strings. The function takes
two string arguments and returns an integer value that indicates the
relationship between the two strings.
'Baahubali' is lexicographically equal to 'Baahubali'
14. CREATED BY K. VICTOR BABU
strstr() function
Syntax:
char *strstr(const char *haystack, const char *needle);
Example:
char s1[] = "ignorance is bliss";
char s2[] = "is";
char* p;
p = strstr(s1, s2);
if (p) {
printf("String foundn");
printf("First occurrence of string '%s' in '%s' is '%s'", s2, s1, p);
} else
printf("String not foundn");
KLEF CTSD BES-1
The strstr() function is used to search for a substring within a given string.
String found
First occurrence of string 'is' in 'ignorance is bliss' is
'is bliss'
15. CREATED BY K. VICTOR BABU
Summary
• Strings are declared as arrays of characters, terminated by a null character (‘0’).
• Strings can be initialized during declaration or assigned later using the assignment
operator (=).
• scanf() and printf() functions are used used for string input and output.
• The <string.h> header provides various string manipulation functions, such as strlen(),
strcpy(), strupr() etc.,
• Strings can be concatenated using the strcat() function or by manually copying
characters.
• String comparison can be performed using the strcmp() function.
• The strstr() function is used to search for a substring within a string.
KLEF CTSD BES-1
16. CREATED BY K. VICTOR BABU
THANK YOU
Team – CTSD
KLEF CTSD BES-1