SlideShare a Scribd company logo
Presentation on string.h
String.h- This header file defines several  functionsto manipulate C strings and arrays.Functions declared in string.h are extremely popularsince as a part of the C standard library, they are guaranteed to work on any platform  which supports CSome of its functions are defined below .
Memcpy:- UseCopy block of memory  SYNOPSIS#include <string.h> void *memcpy(void *s1, const void *s2, size_tn);  DESCRIPTIONThe memcpy() function copies n bytes from the object pointed to by s2 into the object pointed to by s1. If copying takes place between objects that overlap, the behaviour is undefined.  RETURN VALUEThe memcpy() function returns s1; no return value is reserved to indicate an error.
Example/* memcpy example */#include <stdio.h>#include <string.h> int main (){  char str1[]="Sample string";  char str2[40];  char str3[40];memcpy (str2,str1,strlen(str1)+1);memcpy (str3,"copy successful",16);printf ("str1: %s\nstr2: %s\nstr3: %s\n",str1,str2,str3);  return 0;}Outputstr1: Sample stringstr2: Sample stringstr3: copy successful
memmovememmoveMemmove:-UseMove block of memorySYNOPSIS#include <string.h> void *memmove(void *s1, const void *s2, size_tn);  DESCRIPTIONThe memmove() function copies n bytes from the object pointed to by s2 into the object pointed to by s1. Copying takes place as if the n bytes from the object pointed to by s2 are first copied into a temporary array of n bytes that does not overlap the objects pointed to by s1 and s2, and then the n bytes from the temporary array are copied into the object pointed to by s1.  RETURN VALUEThe memmove() function returns s1; no return value is reserved to indicate an error.
Example/* memmove example */ #include <stdio.h> #include <string.h>int main () {    char str[] = "memmove can be veryuseful......";memmove (str+20,str+15,11);    puts (str);    return 0;}}}Outputmemmove can be very very useful.
Memcmp :-UseCompare two blocks of memorySYNOPSIS#include <string.h> intmemcmp(const void *s1, const void *s2, size_tn);  DESCRIPTIONThe memcmp() function compares the first n bytes of the object pointed to by s1 to the first nbytes of the object pointed to by s2. The sign of a non-zero return value is determined by thesign of the difference between the values of the first pair of bytes that differ in the objects being compared.  RETURN VALUEThe memcmp() function returns an integer greater than, equal to or less than 0, if the object pointed to by s1 is greater than, equal to or less than the object pointed to by s2 respectively.
Example/* memcmp example */#include <stdio.h>#include <string.h> int main (){  char str1[256];  char str2[256];int n;size_t len1, len2;printf ("Enter a sentence: "); gets(str1);printf ("Enter another sentence: "); gets(str2);  len1=strlen(str1);  len2=strlen(str2);  n=memcmp ( str1, str2, len1>len2?len1:len2 );  if (n>0) printf ("'%s' is greater than '%s'.\n",str1,str2);  else if (n<0) printf ("'%s' is less than '%s'.\n",str1,str2);  else printf ("'%s' is the same as '%s'.\n",str1,str2);  return 0;}OutputEnter a sentence: buildingEnter another sentence: book'building' is greater than 'book'
Memset:-UseFill block of memorySYNOPSIS#include <string.h> void *memset(void *s, intc, size_tn);  DESCRIPTIONThe memset() function copies c (converted to an unsigned char) into each of the first n bytes of the object pointed to by s.  RETURN VALUEThe memset() function returns s; no return value is reserved to indicate an error.
Example/* memset example */#include <stdio.h>#include <string.h> int main (){  char str[] = "almost every programmer should know memset!";memset (str,'-',6);  puts (str);  return 0;}Output------ every programmer should know memset!
MemchrUseLocate character in block of memory SYNOPSIS#include <string.h> void *memchr(const void *s, intc, size_tn); DESCRIPTIONThe memchr() function locates the first occurrence of c (converted to an unsigned char) in the initial n bytes (each interpreted as unsigned char) of the object pointed to by s.  RETURN VALUEThe memchr() function returns a pointer to the located byte, or a null pointer if the byte does not occur in the object.
Example/* memchr example */#include <stdio.h>#include <string.h> int main (){  char * pch;  char str[] = "Example string";pch = (char*) memchr (str, 'p', strlen(str));  if (pch!=NULL)printf ("'p' found at position %d.\n", pch-str+1);  elseprintf ("'p' not found.\n");  return 0;}Output'p' found at position 5.
Thanks  for watching  .Hope u like it…..By :- NISHANK

More Related Content

PDF
String searching
PPS
Aae oop xp_06
PPTX
Lecture 2. mte 407
PPTX
Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing
PPTX
Function recap
PPTX
Programming in C (part 2)
PPTX
C programming - String
PPTX
C programming(part 3)
String searching
Aae oop xp_06
Lecture 2. mte 407
Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Function recap
Programming in C (part 2)
C programming - String
C programming(part 3)

What's hot (20)

PPT
Functions and pointers_unit_4
PPTX
C++ 11 Features
PPTX
C programming(Part 1)
PPTX
Strings
PDF
C++11 & C++14
PPTX
String Handling in c++
PDF
Maintainable go
PDF
Modern C++
PPT
14 strings
PDF
7 functions
PPT
4 Type conversion functions
PPT
Headerfiles
PPTX
C++11: Feel the New Language
PDF
4 operators, expressions &amp; statements
PDF
8 arrays and pointers
PDF
1 introducing c language
PDF
array, function, pointer, pattern matching
PDF
PPTX
Pointers in C/C++ Programming
PPT
Dd3.15 thru-3.21-advanced-functions
Functions and pointers_unit_4
C++ 11 Features
C programming(Part 1)
Strings
C++11 & C++14
String Handling in c++
Maintainable go
Modern C++
14 strings
7 functions
4 Type conversion functions
Headerfiles
C++11: Feel the New Language
4 operators, expressions &amp; statements
8 arrays and pointers
1 introducing c language
array, function, pointer, pattern matching
Pointers in C/C++ Programming
Dd3.15 thru-3.21-advanced-functions
Ad

Similar to String .h (20)

PPTX
Lecture 15_Strings and Dynamic Memory Allocation.pptx
PPTX
C++ Programming Homework Help
PPTX
POEPPSGNHwkejnkweoewnkenjwjewkjewoewkjewijewjk
PDF
Linked list
PPT
Savitch Ch 08
DOC
C - aptitude3
DOC
C aptitude questions
PDF
Unit 2
DOCX
Unitii string
PDF
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
PDF
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
PPT
Savitch ch 08
PPTX
Bsc cs i pic u-4 function, storage class and array and strings
PDF
Functions torage class and array and strings-
PPTX
Btech i pic u-4 function, storage class and array and strings
PDF
Imugi: Compiler made with Python
PPTX
CPP Homework Help
PPTX
Diploma ii cfpc u-4 function, storage class and array and strings
PDF
06 -working_with_strings
PPTX
Mcai pic u 4 function, storage class and array and strings
Lecture 15_Strings and Dynamic Memory Allocation.pptx
C++ Programming Homework Help
POEPPSGNHwkejnkweoewnkenjwjewkjewoewkjewijewjk
Linked list
Savitch Ch 08
C - aptitude3
C aptitude questions
Unit 2
Unitii string
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
Savitch ch 08
Bsc cs i pic u-4 function, storage class and array and strings
Functions torage class and array and strings-
Btech i pic u-4 function, storage class and array and strings
Imugi: Compiler made with Python
CPP Homework Help
Diploma ii cfpc u-4 function, storage class and array and strings
06 -working_with_strings
Mcai pic u 4 function, storage class and array and strings
Ad

Recently uploaded (20)

PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
sap open course for s4hana steps from ECC to s4
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
Spectroscopy.pptx food analysis technology
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
cuic standard and advanced reporting.pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Electronic commerce courselecture one. Pdf
PDF
KodekX | Application Modernization Development
PPT
Teaching material agriculture food technology
PDF
Empathic Computing: Creating Shared Understanding
PPTX
Big Data Technologies - Introduction.pptx
PDF
Network Security Unit 5.pdf for BCA BBA.
Digital-Transformation-Roadmap-for-Companies.pptx
Chapter 3 Spatial Domain Image Processing.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
sap open course for s4hana steps from ECC to s4
MYSQL Presentation for SQL database connectivity
Spectroscopy.pptx food analysis technology
Reach Out and Touch Someone: Haptics and Empathic Computing
Understanding_Digital_Forensics_Presentation.pptx
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Encapsulation_ Review paper, used for researhc scholars
cuic standard and advanced reporting.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Building Integrated photovoltaic BIPV_UPV.pdf
Electronic commerce courselecture one. Pdf
KodekX | Application Modernization Development
Teaching material agriculture food technology
Empathic Computing: Creating Shared Understanding
Big Data Technologies - Introduction.pptx
Network Security Unit 5.pdf for BCA BBA.

String .h

  • 2. String.h- This header file defines several functionsto manipulate C strings and arrays.Functions declared in string.h are extremely popularsince as a part of the C standard library, they are guaranteed to work on any platform which supports CSome of its functions are defined below .
  • 3. Memcpy:- UseCopy block of memory  SYNOPSIS#include <string.h> void *memcpy(void *s1, const void *s2, size_tn);  DESCRIPTIONThe memcpy() function copies n bytes from the object pointed to by s2 into the object pointed to by s1. If copying takes place between objects that overlap, the behaviour is undefined.  RETURN VALUEThe memcpy() function returns s1; no return value is reserved to indicate an error.
  • 4. Example/* memcpy example */#include <stdio.h>#include <string.h> int main (){ char str1[]="Sample string"; char str2[40]; char str3[40];memcpy (str2,str1,strlen(str1)+1);memcpy (str3,"copy successful",16);printf ("str1: %s\nstr2: %s\nstr3: %s\n",str1,str2,str3); return 0;}Outputstr1: Sample stringstr2: Sample stringstr3: copy successful
  • 5. memmovememmoveMemmove:-UseMove block of memorySYNOPSIS#include <string.h> void *memmove(void *s1, const void *s2, size_tn);  DESCRIPTIONThe memmove() function copies n bytes from the object pointed to by s2 into the object pointed to by s1. Copying takes place as if the n bytes from the object pointed to by s2 are first copied into a temporary array of n bytes that does not overlap the objects pointed to by s1 and s2, and then the n bytes from the temporary array are copied into the object pointed to by s1.  RETURN VALUEThe memmove() function returns s1; no return value is reserved to indicate an error.
  • 6. Example/* memmove example */ #include <stdio.h> #include <string.h>int main () { char str[] = "memmove can be veryuseful......";memmove (str+20,str+15,11); puts (str); return 0;}}}Outputmemmove can be very very useful.
  • 7. Memcmp :-UseCompare two blocks of memorySYNOPSIS#include <string.h> intmemcmp(const void *s1, const void *s2, size_tn);  DESCRIPTIONThe memcmp() function compares the first n bytes of the object pointed to by s1 to the first nbytes of the object pointed to by s2. The sign of a non-zero return value is determined by thesign of the difference between the values of the first pair of bytes that differ in the objects being compared.  RETURN VALUEThe memcmp() function returns an integer greater than, equal to or less than 0, if the object pointed to by s1 is greater than, equal to or less than the object pointed to by s2 respectively.
  • 8. Example/* memcmp example */#include <stdio.h>#include <string.h> int main (){ char str1[256]; char str2[256];int n;size_t len1, len2;printf ("Enter a sentence: "); gets(str1);printf ("Enter another sentence: "); gets(str2); len1=strlen(str1); len2=strlen(str2); n=memcmp ( str1, str2, len1>len2?len1:len2 ); if (n>0) printf ("'%s' is greater than '%s'.\n",str1,str2); else if (n<0) printf ("'%s' is less than '%s'.\n",str1,str2); else printf ("'%s' is the same as '%s'.\n",str1,str2); return 0;}OutputEnter a sentence: buildingEnter another sentence: book'building' is greater than 'book'
  • 9. Memset:-UseFill block of memorySYNOPSIS#include <string.h> void *memset(void *s, intc, size_tn);  DESCRIPTIONThe memset() function copies c (converted to an unsigned char) into each of the first n bytes of the object pointed to by s.  RETURN VALUEThe memset() function returns s; no return value is reserved to indicate an error.
  • 10. Example/* memset example */#include <stdio.h>#include <string.h> int main (){ char str[] = "almost every programmer should know memset!";memset (str,'-',6); puts (str); return 0;}Output------ every programmer should know memset!
  • 11. MemchrUseLocate character in block of memory SYNOPSIS#include <string.h> void *memchr(const void *s, intc, size_tn); DESCRIPTIONThe memchr() function locates the first occurrence of c (converted to an unsigned char) in the initial n bytes (each interpreted as unsigned char) of the object pointed to by s.  RETURN VALUEThe memchr() function returns a pointer to the located byte, or a null pointer if the byte does not occur in the object.
  • 12. Example/* memchr example */#include <stdio.h>#include <string.h> int main (){ char * pch; char str[] = "Example string";pch = (char*) memchr (str, 'p', strlen(str)); if (pch!=NULL)printf ("'p' found at position %d.\n", pch-str+1); elseprintf ("'p' not found.\n"); return 0;}Output'p' found at position 5.
  • 13. Thanks for watching .Hope u like it…..By :- NISHANK