You are on page 1of 16

Department of BES-1

COMPUTATIONAL THINKING FOR


STRUCTURED DESIGN

STRINGS AND STRING LIBRARY

Mr. B. ASHOK
Assistant Professor

KLEF CTSD BES-1


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
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


CREATED BY K. VICTOR BABU
Strings in C

 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.
KLEF CTSD BES-1
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
CREATED BY K. VICTOR BABU
String Initialization

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: Note: Make sure the size of the character
char str[10]; array is sufficient to hold the string,
strcpy(str, "Hello"); including the null character.

KLEF CTSD BES-1


CREATED BY K. VICTOR BABU
Reading and displaying a String

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: %s\n", str); Note: The %s format specifier in scanf
return 0; 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.

KLEF CTSD BES-1


CREATED BY K. VICTOR BABU
Understanding different formats for displaying strings
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("%s\n", str);
Hello, World!!
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);
|Hello wo|
printf("|%.*s|\n", precision , greetings);
|Hello wo|
printf("|%16s|\n", greetings); | Hello world|
printf("|%-16s|\n", greetings); |Hello world |
printf("|%*s|\n", biggerPrecision , greetings); | Hello world|
KLEF CTSD BES-1
CREATED BY K. VICTOR BABU
String Library Functions

The predefined functions that are designed to handle strings are available
in the library “string.h”. They are

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

KLEF CTSD BES-1


CREATED BY K. VICTOR BABU
strlen() function

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.

Syntax:

int strlen(const char *str);

Example:

char str[] = "India is my country";


int length = strlen(str);
printf("Length: %d\n", length);
Length: 19

KLEF CTSD BES-1


CREATED BY K. VICTOR BABU
strcpy() function

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.
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: %s\n", destination);
Copied string: KL Deemed to be University
KLEF CTSD BES-1
CREATED BY K. VICTOR BABU
strcat() function

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.
Syntax:

char *strcat(char *dest, const char *src);

Example:

char destination[20] = "Dennis ";


char source[] = "Ritchie";
strcat(destination, source);
printf("Concatenated string: %s\n", destination);
Concatenated string: Dennis Ritchie
KLEF CTSD BES-1
CREATED BY K. VICTOR BABU
strcmp() function

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.
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);
'Baahubali' is lexicographically equal to 'Baahubali'
KLEF CTSD BES-1
CREATED BY K. VICTOR BABU
strstr() function

The strstr() function is used to search for a substring within a given string.
Syntax:
char *strstr(const char *haystack, const char *needle);

Example:
char s1[] = "ignorance is bliss";
char s2[] = "is";
String found
char* p; First occurrence of string 'is' in 'ignorance is bliss' is
p = strstr(s1, s2); 'is bliss'
if (p) {
printf("String found\n");
printf("First occurrence of string '%s' in '%s' is '%s'", s2, s1, p);
} else
printf("String not found\n");
KLEF CTSD BES-1
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
CREATED BY K. VICTOR BABU
THANK YOU

Team – CTSD

KLEF CTSD BES-1


CREATED BY K. VICTOR BABU

You might also like