SlideShare a Scribd company logo
1 of 16
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
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
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.
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
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.
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.
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|
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
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
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
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
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'
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'
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

More Related Content

Similar to 24_2-String and String Library.pptx

Similar to 24_2-String and String Library.pptx (20)

fundamentals of c programming_String.pptx
fundamentals of c programming_String.pptxfundamentals of c programming_String.pptx
fundamentals of c programming_String.pptx
 
Strings
StringsStrings
Strings
 
C string
C stringC string
C string
 
String in c programming
String in c programmingString in c programming
String in c programming
 
string in C
string in Cstring in C
string in C
 
[ITP - Lecture 17] Strings in C/C++
[ITP - Lecture 17] Strings in C/C++[ITP - Lecture 17] Strings in C/C++
[ITP - Lecture 17] Strings in C/C++
 
Lesson in Strings for C Programming Lessons
Lesson in Strings for C Programming LessonsLesson in Strings for C Programming Lessons
Lesson in Strings for C Programming Lessons
 
CPSTRINGSARGAVISTRINGS.PPT
CPSTRINGSARGAVISTRINGS.PPTCPSTRINGSARGAVISTRINGS.PPT
CPSTRINGSARGAVISTRINGS.PPT
 
BHARGAVISTRINGS.PPT
BHARGAVISTRINGS.PPTBHARGAVISTRINGS.PPT
BHARGAVISTRINGS.PPT
 
Strings part2
Strings part2Strings part2
Strings part2
 
Strings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothiStrings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothi
 
Arrays & Strings.pptx
Arrays & Strings.pptxArrays & Strings.pptx
Arrays & Strings.pptx
 
Strings
StringsStrings
Strings
 
manipulation of Strings+PPT.pptx
manipulation of Strings+PPT.pptxmanipulation of Strings+PPT.pptx
manipulation of Strings+PPT.pptx
 
CP-STRING (1).ppt
CP-STRING (1).pptCP-STRING (1).ppt
CP-STRING (1).ppt
 
CP-STRING.ppt
CP-STRING.pptCP-STRING.ppt
CP-STRING.ppt
 
CP-STRING.ppt
CP-STRING.pptCP-STRING.ppt
CP-STRING.ppt
 
Team 1
Team 1Team 1
Team 1
 
String notes
String notesString notes
String notes
 
5 2. string processing
5 2. string processing5 2. string processing
5 2. string processing
 

Recently uploaded

The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...Pooja Nehwal
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 

Recently uploaded (20)

The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 

24_2-String and String Library.pptx

  • 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