SlideShare a Scribd company logo
C – Strings and String functions with
examples
String is an array of characters. In this guide, we learn how to declare strings,
how to work with strings in C programming and how to use the pre-defined string
handling functions.
We will see how to compare two strings, concatenate strings, copy one string to
another & perform various string manipulation operations. We can perform such
operations using the pre-defined functions of “string.h” header file. In order to use
these string functions you must include string.h file in your C program.
String Declaration
Method 1:
char address[]={'T', 'E', 'X', 'A', 'S', '0'};
Method 2: The above string can also be defined as –
char address[]="TEXAS";
In the above declaration NULL character (0) will automatically be inserted at the
end of the string.
What is NULL Char “0”?
'0' represents the end of the string. It is also referred as String terminator & Null
Character.
String I/O in C programming
Read & write Strings in C using Printf() and Scanf() functions
#include <stdio.h>
#include <string.h>
int main()
{
/* String Declaration*/
char nickname[20];
printf("Enter your Nick name:");
/* I am reading the input string and storing it in nickname
* Array name alone works as a base address of array so
* we can use nickname instead of &nickname here
*/
scanf("%s", nickname);
/*Displaying String*/
printf("%s",nickname);
return 0;
}
Output:
Enter your Nick name:Negan
Negan
Note: %s format specifier is used for strings input/output
Read & Write Strings in C using gets() and puts() functions
#include <stdio.h>
#include <string.h>
int main()
{
/* String Declaration*/
char nickname[20];
/* Console display using puts */
puts("Enter your Nick name:");
/*Input using gets*/
gets(nickname);
puts(nickname);
return 0;
}
C – String functions
C String function – strlen
Syntax:
size_t strlen(const char *str)
size_t represents unsigned short
It returns the length of the string without including end character (terminating
char ‘0’).
Example of strlen:
#include <stdio.h>
#include <string.h>
int main()
{
char str1[20] = "BeginnersBook";
printf("Length of string str1: %d", strlen(str1));
return 0;
}
Output:
Length of string str1: 13
strlen vs sizeof
strlen returns you the length of the string stored in array, however sizeof returns
the total allocated size assigned to the array. So if I consider the above example
again then the following statements would return the below values.
strlen(str1) returned value 13.
sizeof(str1) would return value 20 as the array size is 20 (see the first statement in
main function).
C String function – strnlen
Syntax:
size_t strnlen(const char *str, size_t maxlen)
size_t represents unsigned short
It returns length of the string if it is less than the value specified for maxlen
(maximum length) otherwise it returns maxlen value.
Example of strnlen:
#include <stdio.h>
#include <string.h>
int main()
{
char str1[20] = "BeginnersBook";
printf("Length of string str1 when maxlen is 30: %d", strnlen(str1, 30));
printf("Length of string str1 when maxlen is 10: %d", strnlen(str1, 10));
return 0;
}
Output:
Length of string str1 when maxlen is 30: 13
Length of string str1 when maxlen is 10: 10
Have you noticed the output of second printf statement, even though the string
length was 13 it returned only 10 because the maxlen was 10.
C String function – strcmp
int strcmp(const char *str1, const char *str2)
It compares the two strings and returns an integer value. If both the strings are
same (equal) then this function would return 0 otherwise it may return a negative
or positive value based on the comparison.
If string1 < string2 OR string1 is a substring of string2 then it would result in
a negative value. If string1 > string2 then it would return positive value.
If string1 == string2 then you would get 0(zero) when you use this function for
compare strings.
Example of strcmp:
#include <stdio.h>
#include <string.h>
int main()
{
char s1[20] = "BeginnersBook";
char s2[20] = "BeginnersBook.COM";
if (strcmp(s1, s2) ==0)
{
printf("string 1 and string 2 are equal");
}else
{
printf("string 1 and 2 are different");
}
return 0;
}
Output:
string 1 and 2 are different
C String function – strncmp
int strncmp(const char *str1, const char *str2, size_t n)
size_t is for unassigned short
It compares both the string till n characters or in other words it compares first n
characters of both the strings.
Example of strncmp:
#include <stdio.h>
#include <string.h>
int main()
{
char s1[20] = "BeginnersBook";
char s2[20] = "BeginnersBook.COM";
/* below it is comparing first 8 characters of s1 and s2*/
if (strncmp(s1, s2, 8) ==0)
{
printf("string 1 and string 2 are equal");
}else
{
printf("string 1 and 2 are different");
}
return 0;
}
Output:
string1 and string 2 are equal
C String function – strcat
char *strcat(char *str1, char *str2)
It concatenates two strings and returns the concatenated string.
Example of strcat:
#include <stdio.h>
#include <string.h>
int main()
{
char s1[10] = "Hello";
char s2[10] = "World";
strcat(s1,s2);
printf("Output string after concatenation: %s", s1);
return 0;
}
Output:
Output string after concatenation: HelloWorld
C String function – strncat
char *strncat(char *str1, char *str2, int n)
It concatenates n characters of str2 to string str1. A terminator char (‘0’) will
always be appended at the end of the concatenated string.
Example of strncat:
#include <stdio.h>
#include <string.h>
int main()
{
char s1[10] = "Hello";
char s2[10] = "World";
strncat(s1,s2, 3);
printf("Concatenation using strncat: %s", s1);
return 0;
}
Output:
Concatenation using strncat: HelloWor
C String function – strcpy
char *strcpy( char *str1, char *str2)
It copies the string str2 into string str1, including the end character (terminator
char ‘0’).
Example of strcpy:
#include <stdio.h>
#include <string.h>
int main()
{
char s1[30] = "string 1";
char s2[30] = "string 2 : I’m gonna copied into s1";
/* this function has copied s2 into s1*/
strcpy(s1,s2);
printf("String s1 is: %s", s1);
return 0;
}
Output:
String s1 is: string 2: I’m gonna copied into s1
C String function – strncpy
char *strncpy( char *str1, char *str2, size_t n)
size_t is unassigned short and n is a number.
Case1: If length of str2 > n then it just copies first n characters of str2 into str1.
Case2: If length of str2 < n then it copies all the characters of str2 into str1 and
appends several terminator chars(‘0’) to accumulate the length of str1 to make it
n.
Example of strncpy:
#include <stdio.h>
#include <string.h>
int main()
{
char first[30] = "string 1";
char second[30] = "string 2: I’m using strncpy now";
/* this function has copied first 10 chars of s2 into s1*/
strncpy(s1,s2, 12);
printf("String s1 is: %s", s1);
return 0;
}
Output:
String s1 is: string 2: I’m
C String function – strchr
char *strchr(char *str, int ch)
It searches string str for character ch (you may be wondering that in above
definition I have given data type of ch as int, don’t worry I didn’t make any
mistake it should be int only. The thing is when we give any character while using
strchr then it internally gets converted into integer for better searching.
Example of strchr:
#include <stdio.h>
#include <string.h>
int main()
{
char mystr[30] = "I’m an example of function strchr";
printf ("%s", strchr(mystr, 'f'));
return 0;
}
Output:
f function strchr
C String function – Strrchr
char *strrchr(char *str, int ch)
It is similar to the function strchr, the only difference is that it searches the string
in reverse order, now you would have understood why we have extra r in strrchr,
yes you guessed it correct, it is for reverse only.
Now let’s take the same above example:
#include <stdio.h>
#include <string.h>
int main()
{
char mystr[30] = "I’m an example of function strchr";
printf ("%s", strrchr(mystr, 'f'));
return 0;
}
Output:
function strchr
Why output is different than strchr? It is because it started searching from the
end of the string and found the first ‘f’ in function instead of ‘of’.
C String function – strstr
char *strstr(char *str, char *srch_term)
It is similar to strchr, except that it searches for string srch_term instead of a
single char.
Example of strstr:
#include <stdio.h>
#include <string.h>
int main()
{
char inputstr[70] = "String Function in C at BeginnersBook.COM";
printf ("Output string is: %s", strstr(inputstr, 'Begi'));
return 0;
}
Output:
Output string is: BeginnersBook.COM
You can also use this function in place of strchr as you are allowed to give single
char also in place of search_term string.

More Related Content

What's hot (20)

Strinng Classes in c++
Strinng Classes in c++Strinng Classes in c++
Strinng Classes in c++
Vikash Dhal
 
Strings
StringsStrings
Strings
Mitali Chugh
 
Strings in C
Strings in CStrings in C
Strings in C
Aditya Nihal Kumar Singh
 
String in programming language in c or c++
 String in programming language  in c or c++  String in programming language  in c or c++
String in programming language in c or c++
Samsil Arefin
 
Unit 2
Unit 2Unit 2
Unit 2
TPLatchoumi
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and String
Tasnima Hamid
 
Strings in c++
Strings in c++Strings in c++
Strings in c++
International Islamic University
 
Lecture 2. mte 407
Lecture 2. mte 407Lecture 2. mte 407
Lecture 2. mte 407
rumanatasnim415
 
Strings
StringsStrings
Strings
Michael Gordon
 
Strings in C
Strings in CStrings in C
Strings in C
Kamal Acharya
 
Chap 8(strings)
Chap 8(strings)Chap 8(strings)
Chap 8(strings)
Bangabandhu Sheikh Mujibur Rahman Science and Technology University
 
String notes
String notesString notes
String notes
Prasadu Peddi
 
Strings
StringsStrings
Strings
Dhiviya Rose
 
Handling of character strings C programming
Handling of character strings C programmingHandling of character strings C programming
Handling of character strings C programming
Appili Vamsi Krishna
 
C++ string
C++ stringC++ string
C++ string
Dheenadayalan18
 
Implementation Of String Functions In C
Implementation Of String Functions In CImplementation Of String Functions In C
Implementation Of String Functions In C
Fazila Sadia
 
function, storage class and array and strings
 function, storage class and array and strings function, storage class and array and strings
function, storage class and array and strings
Rai University
 
C string
C stringC string
C string
University of Potsdam
 
Strings in c++
Strings in c++Strings in c++
Strings in c++
Neeru Mittal
 
C Programming Unit-3
C Programming Unit-3C Programming Unit-3
C Programming Unit-3
Vikram Nandini
 

Similar to Unitii string (20)

Principals of Programming in CModule -5.pdfModule-4.pdf
Principals of Programming in CModule -5.pdfModule-4.pdfPrincipals of Programming in CModule -5.pdfModule-4.pdf
Principals of Programming in CModule -5.pdfModule-4.pdf
anilcsbs
 
POEPPSGNHwkejnkweoewnkenjwjewkjewoewkjewijewjk
POEPPSGNHwkejnkweoewnkenjwjewkjewoewkjewijewjkPOEPPSGNHwkejnkweoewnkenjwjewkjewoewkjewijewjk
POEPPSGNHwkejnkweoewnkenjwjewkjewoewkjewijewjk
hanumanthumanideeph6
 
UNIT 4C-Strings.pptx for c language and basic knowledge
UNIT 4C-Strings.pptx for c language and basic knowledgeUNIT 4C-Strings.pptx for c language and basic knowledge
UNIT 4C-Strings.pptx for c language and basic knowledge
2024163103shubham
 
C Programming Strings.docx
C Programming Strings.docxC Programming Strings.docx
C Programming Strings.docx
8759000398
 
[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++
Muhammad Hammad Waseem
 
Lecture 15_Strings and Dynamic Memory Allocation.pptx
Lecture 15_Strings and  Dynamic Memory Allocation.pptxLecture 15_Strings and  Dynamic Memory Allocation.pptx
Lecture 15_Strings and Dynamic Memory Allocation.pptx
JawadTanvir
 
string function with example...................
string function with example...................string function with example...................
string function with example...................
NishantsrivastavaV
 
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
STRINGS IN C MRS.SOWMYA JYOTHI.pdfSTRINGS IN C MRS.SOWMYA JYOTHI.pdf
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
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
JamesChristianGadian
 
unit-5 String Math Date Time AI presentation
unit-5 String Math Date Time AI presentationunit-5 String Math Date Time AI presentation
unit-5 String Math Date Time AI presentation
MukeshTheLioner
 
String_C.pptx
String_C.pptxString_C.pptx
String_C.pptx
HARSHITHA EBBALI
 
Bsc cs i pic u-4 function, storage class and array and strings
Bsc cs i pic u-4 function, storage class and array and stringsBsc cs i pic u-4 function, storage class and array and strings
Bsc cs i pic u-4 function, storage class and array and strings
Rai University
 
PROBLEM SOLVING USING A PPSC- UNIT -3.pdf
PROBLEM SOLVING USING A PPSC- UNIT -3.pdfPROBLEM SOLVING USING A PPSC- UNIT -3.pdf
PROBLEM SOLVING USING A PPSC- UNIT -3.pdf
JNTUK KAKINADA
 
Diploma ii cfpc u-4 function, storage class and array and strings
Diploma ii  cfpc u-4 function, storage class and array and stringsDiploma ii  cfpc u-4 function, storage class and array and strings
Diploma ii cfpc u-4 function, storage class and array and strings
Rai University
 
introduction to strings in c programming
introduction to strings in c programmingintroduction to strings in c programming
introduction to strings in c programming
mikeymanjiro2090
 
Btech i pic u-4 function, storage class and array and strings
Btech i pic u-4 function, storage class and array and stringsBtech i pic u-4 function, storage class and array and strings
Btech i pic u-4 function, storage class and array and strings
Rai University
 
Functions torage class and array and strings-
Functions torage class and array and strings-Functions torage class and array and strings-
Functions torage class and array and strings-
aneebkmct
 
c programming
c programmingc programming
c programming
Arun Umrao
 
String in programming language in c or c++
String in programming language in c or c++String in programming language in c or c++
String in programming language in c or c++
Azeemaj101
 
Mcai pic u 4 function, storage class and array and strings
Mcai pic u 4 function, storage class and array and stringsMcai pic u 4 function, storage class and array and strings
Mcai pic u 4 function, storage class and array and strings
Rai University
 
Principals of Programming in CModule -5.pdfModule-4.pdf
Principals of Programming in CModule -5.pdfModule-4.pdfPrincipals of Programming in CModule -5.pdfModule-4.pdf
Principals of Programming in CModule -5.pdfModule-4.pdf
anilcsbs
 
POEPPSGNHwkejnkweoewnkenjwjewkjewoewkjewijewjk
POEPPSGNHwkejnkweoewnkenjwjewkjewoewkjewijewjkPOEPPSGNHwkejnkweoewnkenjwjewkjewoewkjewijewjk
POEPPSGNHwkejnkweoewnkenjwjewkjewoewkjewijewjk
hanumanthumanideeph6
 
UNIT 4C-Strings.pptx for c language and basic knowledge
UNIT 4C-Strings.pptx for c language and basic knowledgeUNIT 4C-Strings.pptx for c language and basic knowledge
UNIT 4C-Strings.pptx for c language and basic knowledge
2024163103shubham
 
C Programming Strings.docx
C Programming Strings.docxC Programming Strings.docx
C Programming Strings.docx
8759000398
 
Lecture 15_Strings and Dynamic Memory Allocation.pptx
Lecture 15_Strings and  Dynamic Memory Allocation.pptxLecture 15_Strings and  Dynamic Memory Allocation.pptx
Lecture 15_Strings and Dynamic Memory Allocation.pptx
JawadTanvir
 
string function with example...................
string function with example...................string function with example...................
string function with example...................
NishantsrivastavaV
 
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
STRINGS IN C MRS.SOWMYA JYOTHI.pdfSTRINGS IN C MRS.SOWMYA JYOTHI.pdf
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
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
JamesChristianGadian
 
unit-5 String Math Date Time AI presentation
unit-5 String Math Date Time AI presentationunit-5 String Math Date Time AI presentation
unit-5 String Math Date Time AI presentation
MukeshTheLioner
 
Bsc cs i pic u-4 function, storage class and array and strings
Bsc cs i pic u-4 function, storage class and array and stringsBsc cs i pic u-4 function, storage class and array and strings
Bsc cs i pic u-4 function, storage class and array and strings
Rai University
 
PROBLEM SOLVING USING A PPSC- UNIT -3.pdf
PROBLEM SOLVING USING A PPSC- UNIT -3.pdfPROBLEM SOLVING USING A PPSC- UNIT -3.pdf
PROBLEM SOLVING USING A PPSC- UNIT -3.pdf
JNTUK KAKINADA
 
Diploma ii cfpc u-4 function, storage class and array and strings
Diploma ii  cfpc u-4 function, storage class and array and stringsDiploma ii  cfpc u-4 function, storage class and array and strings
Diploma ii cfpc u-4 function, storage class and array and strings
Rai University
 
introduction to strings in c programming
introduction to strings in c programmingintroduction to strings in c programming
introduction to strings in c programming
mikeymanjiro2090
 
Btech i pic u-4 function, storage class and array and strings
Btech i pic u-4 function, storage class and array and stringsBtech i pic u-4 function, storage class and array and strings
Btech i pic u-4 function, storage class and array and strings
Rai University
 
Functions torage class and array and strings-
Functions torage class and array and strings-Functions torage class and array and strings-
Functions torage class and array and strings-
aneebkmct
 
String in programming language in c or c++
String in programming language in c or c++String in programming language in c or c++
String in programming language in c or c++
Azeemaj101
 
Mcai pic u 4 function, storage class and array and strings
Mcai pic u 4 function, storage class and array and stringsMcai pic u 4 function, storage class and array and strings
Mcai pic u 4 function, storage class and array and strings
Rai University
 

More from Sowri Rajan (11)

Unit 5 dwqb ans
Unit 5 dwqb ansUnit 5 dwqb ans
Unit 5 dwqb ans
Sowri Rajan
 
Unit 3 (1)
Unit 3 (1)Unit 3 (1)
Unit 3 (1)
Sowri Rajan
 
Unit 2
Unit 2Unit 2
Unit 2
Sowri Rajan
 
Unit 1
Unit 1Unit 1
Unit 1
Sowri Rajan
 
Unit 5 quesn b ans5
Unit 5 quesn b ans5Unit 5 quesn b ans5
Unit 5 quesn b ans5
Sowri Rajan
 
Unit 5 quesn b ans5
Unit 5 quesn b ans5Unit 5 quesn b ans5
Unit 5 quesn b ans5
Sowri Rajan
 
Unit 4 qba
Unit 4 qbaUnit 4 qba
Unit 4 qba
Sowri Rajan
 
Unitii classnotes
Unitii classnotesUnitii classnotes
Unitii classnotes
Sowri Rajan
 
Uniti classnotes
Uniti classnotesUniti classnotes
Uniti classnotes
Sowri Rajan
 
Program flowchart
Program flowchartProgram flowchart
Program flowchart
Sowri Rajan
 

Recently uploaded (20)

PayPros-Journey-Overcoming-Challenges-Through-Governance.pptx
PayPros-Journey-Overcoming-Challenges-Through-Governance.pptxPayPros-Journey-Overcoming-Challenges-Through-Governance.pptx
PayPros-Journey-Overcoming-Challenges-Through-Governance.pptx
rayyansiddiqui034
 
Presentation for Schoool Management System
Presentation for Schoool Management SystemPresentation for Schoool Management System
Presentation for Schoool Management System
kolay922013
 
Baby panda 400.pdf de ciencias naturales
Baby panda 400.pdf de ciencias naturalesBaby panda 400.pdf de ciencias naturales
Baby panda 400.pdf de ciencias naturales
debbie loaiza
 
4K Video Downloader Crack (2025) + License Key Free
4K Video Downloader Crack (2025) + License Key Free4K Video Downloader Crack (2025) + License Key Free
4K Video Downloader Crack (2025) + License Key Free
Designer
 
Prof House interior Design Project exter
Prof House interior Design Project exterProf House interior Design Project exter
Prof House interior Design Project exter
NagudiBridget
 
325295919-AAC-Blocks-Seminar-Presentation.pdf
325295919-AAC-Blocks-Seminar-Presentation.pdf325295919-AAC-Blocks-Seminar-Presentation.pdf
325295919-AAC-Blocks-Seminar-Presentation.pdf
shivsin165
 
Modern Gradient Startup Pitch Deck PowerPoint Presentation and Google Slides ...
Modern Gradient Startup Pitch Deck PowerPoint Presentation and Google Slides ...Modern Gradient Startup Pitch Deck PowerPoint Presentation and Google Slides ...
Modern Gradient Startup Pitch Deck PowerPoint Presentation and Google Slides ...
SlidesBrain
 
Templates Wind Generator.pdf ahí. Ais d Ai d f
Templates Wind Generator.pdf ahí. Ais d Ai d fTemplates Wind Generator.pdf ahí. Ais d Ai d f
Templates Wind Generator.pdf ahí. Ais d Ai d f
jeremysegundob
 
DOC-20250121-WA0008._20250121_105938_0000.pptx
DOC-20250121-WA0008._20250121_105938_0000.pptxDOC-20250121-WA0008._20250121_105938_0000.pptx
DOC-20250121-WA0008._20250121_105938_0000.pptx
laugolac31
 
COTTER and KNUCKleeeeeeeeeeeeeeeeeee.pptx
COTTER and  KNUCKleeeeeeeeeeeeeeeeeee.pptxCOTTER and  KNUCKleeeeeeeeeeeeeeeeeee.pptx
COTTER and KNUCKleeeeeeeeeeeeeeeeeee.pptx
ayushjadon04
 
Internet Download Manager Crack Patch Latest IDM Free Download
Internet Download Manager Crack Patch Latest IDM Free DownloadInternet Download Manager Crack Patch Latest IDM Free Download
Internet Download Manager Crack Patch Latest IDM Free Download
Designer
 
An updated content measurement model - Elle Geraghty Content Strategy.pdf
An updated content measurement model - Elle Geraghty Content Strategy.pdfAn updated content measurement model - Elle Geraghty Content Strategy.pdf
An updated content measurement model - Elle Geraghty Content Strategy.pdf
Elle Geraghty
 
Lori Vanzant's portfolio. Please take a look!
Lori Vanzant's portfolio. Please take a look!Lori Vanzant's portfolio. Please take a look!
Lori Vanzant's portfolio. Please take a look!
vanzan01
 
masterddedeeeeeeeeedded seminar (1).pptx
masterddedeeeeeeeeedded seminar (1).pptxmasterddedeeeeeeeeedded seminar (1).pptx
masterddedeeeeeeeeedded seminar (1).pptx
tgavel7869
 
Minimalist Business Slides XL by Slidesgo.pptx
Minimalist Business Slides XL by Slidesgo.pptxMinimalist Business Slides XL by Slidesgo.pptx
Minimalist Business Slides XL by Slidesgo.pptx
karenalavamoran
 
Presentation mockup using lots of animals
Presentation mockup using lots of animalsPresentation mockup using lots of animals
Presentation mockup using lots of animals
ChunChihChenPhD
 
MOCCAE SUSTAINABLE TROPHY 2025 Presentation.pdf
MOCCAE SUSTAINABLE TROPHY 2025 Presentation.pdfMOCCAE SUSTAINABLE TROPHY 2025 Presentation.pdf
MOCCAE SUSTAINABLE TROPHY 2025 Presentation.pdf
asfianoor1
 
Lori Vanzant Online Presence. Take a look!
Lori Vanzant Online Presence. Take a look!Lori Vanzant Online Presence. Take a look!
Lori Vanzant Online Presence. Take a look!
vanzan01
 
Hi! I'm Lori Vanzant. Please take a look
Hi! I'm Lori Vanzant. Please take a lookHi! I'm Lori Vanzant. Please take a look
Hi! I'm Lori Vanzant. Please take a look
vanzan01
 
Emily's slide design 101 - training module
Emily's slide design 101 - training moduleEmily's slide design 101 - training module
Emily's slide design 101 - training module
yourmisswright
 
PayPros-Journey-Overcoming-Challenges-Through-Governance.pptx
PayPros-Journey-Overcoming-Challenges-Through-Governance.pptxPayPros-Journey-Overcoming-Challenges-Through-Governance.pptx
PayPros-Journey-Overcoming-Challenges-Through-Governance.pptx
rayyansiddiqui034
 
Presentation for Schoool Management System
Presentation for Schoool Management SystemPresentation for Schoool Management System
Presentation for Schoool Management System
kolay922013
 
Baby panda 400.pdf de ciencias naturales
Baby panda 400.pdf de ciencias naturalesBaby panda 400.pdf de ciencias naturales
Baby panda 400.pdf de ciencias naturales
debbie loaiza
 
4K Video Downloader Crack (2025) + License Key Free
4K Video Downloader Crack (2025) + License Key Free4K Video Downloader Crack (2025) + License Key Free
4K Video Downloader Crack (2025) + License Key Free
Designer
 
Prof House interior Design Project exter
Prof House interior Design Project exterProf House interior Design Project exter
Prof House interior Design Project exter
NagudiBridget
 
325295919-AAC-Blocks-Seminar-Presentation.pdf
325295919-AAC-Blocks-Seminar-Presentation.pdf325295919-AAC-Blocks-Seminar-Presentation.pdf
325295919-AAC-Blocks-Seminar-Presentation.pdf
shivsin165
 
Modern Gradient Startup Pitch Deck PowerPoint Presentation and Google Slides ...
Modern Gradient Startup Pitch Deck PowerPoint Presentation and Google Slides ...Modern Gradient Startup Pitch Deck PowerPoint Presentation and Google Slides ...
Modern Gradient Startup Pitch Deck PowerPoint Presentation and Google Slides ...
SlidesBrain
 
Templates Wind Generator.pdf ahí. Ais d Ai d f
Templates Wind Generator.pdf ahí. Ais d Ai d fTemplates Wind Generator.pdf ahí. Ais d Ai d f
Templates Wind Generator.pdf ahí. Ais d Ai d f
jeremysegundob
 
DOC-20250121-WA0008._20250121_105938_0000.pptx
DOC-20250121-WA0008._20250121_105938_0000.pptxDOC-20250121-WA0008._20250121_105938_0000.pptx
DOC-20250121-WA0008._20250121_105938_0000.pptx
laugolac31
 
COTTER and KNUCKleeeeeeeeeeeeeeeeeee.pptx
COTTER and  KNUCKleeeeeeeeeeeeeeeeeee.pptxCOTTER and  KNUCKleeeeeeeeeeeeeeeeeee.pptx
COTTER and KNUCKleeeeeeeeeeeeeeeeeee.pptx
ayushjadon04
 
Internet Download Manager Crack Patch Latest IDM Free Download
Internet Download Manager Crack Patch Latest IDM Free DownloadInternet Download Manager Crack Patch Latest IDM Free Download
Internet Download Manager Crack Patch Latest IDM Free Download
Designer
 
An updated content measurement model - Elle Geraghty Content Strategy.pdf
An updated content measurement model - Elle Geraghty Content Strategy.pdfAn updated content measurement model - Elle Geraghty Content Strategy.pdf
An updated content measurement model - Elle Geraghty Content Strategy.pdf
Elle Geraghty
 
Lori Vanzant's portfolio. Please take a look!
Lori Vanzant's portfolio. Please take a look!Lori Vanzant's portfolio. Please take a look!
Lori Vanzant's portfolio. Please take a look!
vanzan01
 
masterddedeeeeeeeeedded seminar (1).pptx
masterddedeeeeeeeeedded seminar (1).pptxmasterddedeeeeeeeeedded seminar (1).pptx
masterddedeeeeeeeeedded seminar (1).pptx
tgavel7869
 
Minimalist Business Slides XL by Slidesgo.pptx
Minimalist Business Slides XL by Slidesgo.pptxMinimalist Business Slides XL by Slidesgo.pptx
Minimalist Business Slides XL by Slidesgo.pptx
karenalavamoran
 
Presentation mockup using lots of animals
Presentation mockup using lots of animalsPresentation mockup using lots of animals
Presentation mockup using lots of animals
ChunChihChenPhD
 
MOCCAE SUSTAINABLE TROPHY 2025 Presentation.pdf
MOCCAE SUSTAINABLE TROPHY 2025 Presentation.pdfMOCCAE SUSTAINABLE TROPHY 2025 Presentation.pdf
MOCCAE SUSTAINABLE TROPHY 2025 Presentation.pdf
asfianoor1
 
Lori Vanzant Online Presence. Take a look!
Lori Vanzant Online Presence. Take a look!Lori Vanzant Online Presence. Take a look!
Lori Vanzant Online Presence. Take a look!
vanzan01
 
Hi! I'm Lori Vanzant. Please take a look
Hi! I'm Lori Vanzant. Please take a lookHi! I'm Lori Vanzant. Please take a look
Hi! I'm Lori Vanzant. Please take a look
vanzan01
 
Emily's slide design 101 - training module
Emily's slide design 101 - training moduleEmily's slide design 101 - training module
Emily's slide design 101 - training module
yourmisswright
 

Unitii string

  • 1. C – Strings and String functions with examples String is an array of characters. In this guide, we learn how to declare strings, how to work with strings in C programming and how to use the pre-defined string handling functions. We will see how to compare two strings, concatenate strings, copy one string to another & perform various string manipulation operations. We can perform such operations using the pre-defined functions of “string.h” header file. In order to use these string functions you must include string.h file in your C program. String Declaration Method 1: char address[]={'T', 'E', 'X', 'A', 'S', '0'}; Method 2: The above string can also be defined as – char address[]="TEXAS"; In the above declaration NULL character (0) will automatically be inserted at the end of the string. What is NULL Char “0”? '0' represents the end of the string. It is also referred as String terminator & Null Character.
  • 2. String I/O in C programming Read & write Strings in C using Printf() and Scanf() functions #include <stdio.h> #include <string.h> int main() { /* String Declaration*/ char nickname[20]; printf("Enter your Nick name:"); /* I am reading the input string and storing it in nickname * Array name alone works as a base address of array so * we can use nickname instead of &nickname here */ scanf("%s", nickname); /*Displaying String*/ printf("%s",nickname); return 0; } Output: Enter your Nick name:Negan Negan Note: %s format specifier is used for strings input/output Read & Write Strings in C using gets() and puts() functions #include <stdio.h> #include <string.h>
  • 3. int main() { /* String Declaration*/ char nickname[20]; /* Console display using puts */ puts("Enter your Nick name:"); /*Input using gets*/ gets(nickname); puts(nickname); return 0; } C – String functions C String function – strlen
  • 4. Syntax: size_t strlen(const char *str) size_t represents unsigned short It returns the length of the string without including end character (terminating char ‘0’). Example of strlen: #include <stdio.h> #include <string.h> int main() { char str1[20] = "BeginnersBook"; printf("Length of string str1: %d", strlen(str1)); return 0; } Output: Length of string str1: 13 strlen vs sizeof strlen returns you the length of the string stored in array, however sizeof returns the total allocated size assigned to the array. So if I consider the above example again then the following statements would return the below values. strlen(str1) returned value 13. sizeof(str1) would return value 20 as the array size is 20 (see the first statement in main function). C String function – strnlen Syntax: size_t strnlen(const char *str, size_t maxlen) size_t represents unsigned short It returns length of the string if it is less than the value specified for maxlen (maximum length) otherwise it returns maxlen value. Example of strnlen: #include <stdio.h> #include <string.h> int main()
  • 5. { char str1[20] = "BeginnersBook"; printf("Length of string str1 when maxlen is 30: %d", strnlen(str1, 30)); printf("Length of string str1 when maxlen is 10: %d", strnlen(str1, 10)); return 0; } Output: Length of string str1 when maxlen is 30: 13 Length of string str1 when maxlen is 10: 10 Have you noticed the output of second printf statement, even though the string length was 13 it returned only 10 because the maxlen was 10. C String function – strcmp int strcmp(const char *str1, const char *str2) It compares the two strings and returns an integer value. If both the strings are same (equal) then this function would return 0 otherwise it may return a negative or positive value based on the comparison. If string1 < string2 OR string1 is a substring of string2 then it would result in a negative value. If string1 > string2 then it would return positive value. If string1 == string2 then you would get 0(zero) when you use this function for compare strings. Example of strcmp: #include <stdio.h> #include <string.h> int main() { char s1[20] = "BeginnersBook"; char s2[20] = "BeginnersBook.COM"; if (strcmp(s1, s2) ==0) { printf("string 1 and string 2 are equal"); }else { printf("string 1 and 2 are different"); } return 0; } Output: string 1 and 2 are different C String function – strncmp
  • 6. int strncmp(const char *str1, const char *str2, size_t n) size_t is for unassigned short It compares both the string till n characters or in other words it compares first n characters of both the strings. Example of strncmp: #include <stdio.h> #include <string.h> int main() { char s1[20] = "BeginnersBook"; char s2[20] = "BeginnersBook.COM"; /* below it is comparing first 8 characters of s1 and s2*/ if (strncmp(s1, s2, 8) ==0) { printf("string 1 and string 2 are equal"); }else { printf("string 1 and 2 are different"); } return 0; } Output: string1 and string 2 are equal C String function – strcat char *strcat(char *str1, char *str2) It concatenates two strings and returns the concatenated string. Example of strcat: #include <stdio.h> #include <string.h> int main() { char s1[10] = "Hello"; char s2[10] = "World"; strcat(s1,s2); printf("Output string after concatenation: %s", s1); return 0; } Output: Output string after concatenation: HelloWorld C String function – strncat
  • 7. char *strncat(char *str1, char *str2, int n) It concatenates n characters of str2 to string str1. A terminator char (‘0’) will always be appended at the end of the concatenated string. Example of strncat: #include <stdio.h> #include <string.h> int main() { char s1[10] = "Hello"; char s2[10] = "World"; strncat(s1,s2, 3); printf("Concatenation using strncat: %s", s1); return 0; } Output: Concatenation using strncat: HelloWor C String function – strcpy char *strcpy( char *str1, char *str2) It copies the string str2 into string str1, including the end character (terminator char ‘0’). Example of strcpy: #include <stdio.h> #include <string.h> int main() { char s1[30] = "string 1"; char s2[30] = "string 2 : I’m gonna copied into s1"; /* this function has copied s2 into s1*/ strcpy(s1,s2); printf("String s1 is: %s", s1); return 0; } Output: String s1 is: string 2: I’m gonna copied into s1 C String function – strncpy char *strncpy( char *str1, char *str2, size_t n) size_t is unassigned short and n is a number. Case1: If length of str2 > n then it just copies first n characters of str2 into str1.
  • 8. Case2: If length of str2 < n then it copies all the characters of str2 into str1 and appends several terminator chars(‘0’) to accumulate the length of str1 to make it n. Example of strncpy: #include <stdio.h> #include <string.h> int main() { char first[30] = "string 1"; char second[30] = "string 2: I’m using strncpy now"; /* this function has copied first 10 chars of s2 into s1*/ strncpy(s1,s2, 12); printf("String s1 is: %s", s1); return 0; } Output: String s1 is: string 2: I’m C String function – strchr char *strchr(char *str, int ch) It searches string str for character ch (you may be wondering that in above definition I have given data type of ch as int, don’t worry I didn’t make any mistake it should be int only. The thing is when we give any character while using strchr then it internally gets converted into integer for better searching. Example of strchr: #include <stdio.h> #include <string.h> int main() { char mystr[30] = "I’m an example of function strchr"; printf ("%s", strchr(mystr, 'f')); return 0; } Output: f function strchr C String function – Strrchr char *strrchr(char *str, int ch)
  • 9. It is similar to the function strchr, the only difference is that it searches the string in reverse order, now you would have understood why we have extra r in strrchr, yes you guessed it correct, it is for reverse only. Now let’s take the same above example: #include <stdio.h> #include <string.h> int main() { char mystr[30] = "I’m an example of function strchr"; printf ("%s", strrchr(mystr, 'f')); return 0; } Output: function strchr Why output is different than strchr? It is because it started searching from the end of the string and found the first ‘f’ in function instead of ‘of’. C String function – strstr char *strstr(char *str, char *srch_term) It is similar to strchr, except that it searches for string srch_term instead of a single char. Example of strstr: #include <stdio.h> #include <string.h> int main() { char inputstr[70] = "String Function in C at BeginnersBook.COM"; printf ("Output string is: %s", strstr(inputstr, 'Begi')); return 0; } Output: Output string is: BeginnersBook.COM You can also use this function in place of strchr as you are allowed to give single char also in place of search_term string.