STRINGS AND
     ITS
APPLICATIONS
CONTENTS
   Introduction to Strings.
   Two Ways of using Strings.
   Initializing a string.
   Print a string.
   String Functions and its applications.
Introduction to Strings
   A String is an array of characters.
   Strings are stored in an array of char type along with the null terminating
    character "0" at the end.
   When sizing the string array we need to add plus one to the actual size
    of the string to make space for the null terminating character, "0"
   But the null character is not included in the String.
   Character Strings are often used to build meaningful and Readable
    programs.

  The Common Operations performed on Character Strings include
   Reading and Writing Strings.
   Combined Strings together.
   Copying one String to another.
   Comparing Strings for Equality.
   Extracting a portion of a String.
Two ways of using Strings:
   Using with a Character Array
   Using with a String Pointer


  With a Character Array:

   A character array is declared in the same way as a normal array.


                     char ca[10];

   We must set the value of each individual element of the array to the
    character We want and we must make the last character as 0. Remember
    to use %s when printing the string.
With String Pointers

   String pointers are declared as a pointer to a char.
                      char *sp;
     When we assign a value to the string pointer it will automatically put
    the 0 .
                              char *sp;
                              sp=“Hello”;


                                printf("%s",sp);
      We can read a string into only a character array using scanf and not
    a string pointer.
          If we want to read into a string pointer then we must make it point
    to a character array.
                                char ca[10],*sp;
                                  scanf("%s",ca);
                                  sp = ca;
                                  scanf("%s",sp);
Syntax for declaring a String:
  Syntax to declare a string in C:
                  char string_name[size];
   Sample Code:
                    char fname[4];
   The above statement declares a string called fname that can take up
     to 3 characters. It can be indexed just as a regular array as well.
                 fname[] = {'t','w','o'};
   The last character is the null character having ASCII value zero.


  Initializing a String

  •   To initialize our fname string store the name Ravi,
                   char fname[31] = {“Ravi"};
  •   We can observe from the above statement that initializing a string is
      same as with any array. However we also need to surround the string
      with quotes.
Print a String :

 To write strings to the terminal, we use a file stream known as stdout. The
  most common function to use for writing to stdout in C is the printf function,
  defined as follows:

                  int printf(const char *format, ...);

 To print out a prompt for the user we can:


                 printf("Please type a name: n"); 

 The above statement prints the prompt in the quotes and moves the cursor
  to the next line.
FUNCTIONS IN
STRING & ITS
APPLICATION
MEMCPY - copy characters to
         non-overlapping string.
•   "memcpy" copies exactly N characters from the string "s2" into the area of
    memory pointed to by "s1“.
•   Unlike the function "strncpy", "memcpy" does not check for the terminating
    '0' of string "s2"; it simply copies N characters.
•   It does not put a terminating '0' on the end of string "s1".

Example for memcpy():
ptr = memcpy( s1, s2, N );

void *s1; points to an area of memory that is to receive the copied
characters. This must be able to hold at least N characters.
const void *s2; points to the string from which characters will be copied.
size_t N; gives the number of characters to copy.
void *ptr; points to the copied string (i.e. "s1").
MEMMOVE - copy characters to (possibly
       overlapping) string.
 •   "memmove" moves exactly N characters from the string "s2" into the area
     of memory pointed to by "s1“.
 •   Unlike the function "strncpy", "memmove" does not check for the
     terminating '0' of string "s2"; it simply moves N characters.
 •   It does not put a terminating '0' on the end of string "s1".

 EXAMPLE FOR MEMMOVE():
 ptr = memmove( s1, s2, N );

 void *s1; points to an area of memory that is to receive the moved
 characters. This must be able to hold at least N characters.
 const void *s2; points to the string from which characters will be copied.
 size_t N; gives the number of characters to copy.
 void *ptr; points to the copied string (i.e. "s1").
STRCPY - copy one string to another.
•   "strcpy" copies the string "s2" into the area pointed to by "s1“.
•   This area must be big enough to hold the contents of "s2“.
•   Since "strcpy" moves the '0' at the end of "s2", the new string "s1"
    will have the usual terminating '0'.

EXAMPLE FOR STRCPY():
ptr = strcpy( s1, s2 );

char *s1; points to an area of memory that is to receive the copied
characters.
const char *s2; points to the string from which characters will be copied.
This must end with the usual '0‘.
char *ptr; points to the copied string (i.e. "s1").
STRNCPY - copy characters from string.
 •   "strncpy" copies at most N characters from the string "s2" into the area of
     memory pointed to by "s1“.
 •   If it encounters a '0' before it has copied N characters, it pads "s1" to N
     characters by adding '0' characters.
 •   If "s2" is more than N-1 characters long, the first N characters will be
     copied and the string "s1" will NOT receive the usual terminating '0'.

 EXAMPLE FOR STRNCPY():
 ptr = strncpy( s1, s2, N );

 char *s1; points to an area of memory that is to receive the copied
 characters. This must be able to hold at least N characters.
 const char *s2; points to the string from which characters will be copied.
 size_t N; gives the number of characters to copy.
 char *ptr; points to the copied string (i.e. "s1").
STRCAT - append a string to another.
•   "strcat" appends a copy of the string "s2" to the end of the string "s1“.
•   Both strings "s1" and "s2" must be terminated by the usual '0' character.

EXAMPLE FOR STRCAT():
ptr = strcat( s1, s2 );

char *s1; points to a string terminated by the usual '0‘.
const char *s2; points to a string that will be appended to "s1“.
char *ptr; points to the new string ("s1").
STRNCAT - append part of a string to
            another.
•   "strncat" appends at most N characters from the string "s2" to the end of
    the string "s1“.
•   If string "s2" contains less than N characters, "strncat" will stop when it
    encounters the terminating '0'.

EXAMPLE FOR STRNCAT():
ptr = strncat( s1, s2, N );
char *s1;points to a string terminated by the usual '0‘.
const char *s2;points to a string whose characters will be appended to the
end of "s1“
size_t N;is the maximum number of characters from string "s2" that should
be appended to "s1“.
char *ptr;points to the new string ("s1").
MEMCMP - compare parts of two strings.
  •   "memcmp" compares the first N characters of the string "s1" to the first N
      characters of the string "s2".
  • Unlike the function "strncmp", "memcmp" does not check for a '0'
      terminating either string. Thus it examines a full N characters, even if the
      strings are not actually that long.
  EXAMPLE FOR MEMCMP():
  i = memcmp( s1, s2, N );

  const void *s1, *s2; are the strings to be compared.
  size_t N; gives the number of characters to be examined.
  int i; gives the results of the comparison. "i" is zero if the first N characters of
  the strings are identical. "i" is positive if string "s1" is greater than string "s2",
  and is negative if string "s2" is greater than string "s1". Comparisons of
  "greater than" and "less than" are made according to the ASCII collating
  sequence.
STRCMP - compare two strings.
•   "strcmp" compares the string "s1" to the string "s2". Both strings must be
    terminated by the usual '0' character.

EXAMPLE FOR STRCMP():
i = strcmp( s1, s2 );

const char *s1, *s2; are the strings to be compared.
int i; gives the results of the comparison. "i" is zero if the strings are identical.
"i" is positive if string "s1" is greater than string "s2", and is negative if string
"s2" is greater than string "s1". Comparisons of "greater than" and "less than"
are made according to the ASCII collating sequence.
STRNCMP - compare parts of two
               strings.
•   "strncmp" compares the first N characters of the string "s1" to the first N
    characters of the string "s2“.
•   If one or both of the strings is shorter than N characters (i.e. if "strncmp"
    encounters a '0'), comparisons will stop at that point.
•   Thus N represents the maximum number of characters to be examined,
    not the exact number.

EXAMPLE FOR STRNCMP():
i = strncmp( s1, s2, N );
const char *s1, *s2; are the strings to be compared.
size_t N; gives the number of characters to be examined.
int i; gives the results of the comparison. "i" is zero if the first N characters of
the strings are identical. "i" is positive if string "s1" is greater than string "s2",
and is negative if string "s2" is greater than string "s1". Comparisons of
"greater than" and "less than" are made according to the ASCII collating
sequence.
MEMCHR - find first occurrence of a
         character in a string.
•    "memchr" returns a pointer to the first occurrence of the character "c" in
     the first N characters of the string "s".
•    Unlike "strchr", "memchr" ignores any '0' bytes it comes across; thus it
     does not stop if it finds the end of the string, but keeps on going until it
     has looked at N characters.

EXAMPLE FOR MEMCHR():
ptr = memchr( s, c, N );
const void *s; points to the string to be scanned.
int c; is the character to look for.
size_t N; is the number of characters to look at before giving up.
void *ptr; points to the first occurrence of the character in the string. If the
character is not found, the NULL pointer is returned.
STRCHR - find first occurrence of a
         character in a string.
•   "strchr" returns a pointer to the first occurrence of the character "c" in the
    string "s".
•   The '0' that terminates the string is considered part of the string; thus it is
    possible to find the end of the string with a call like
    ptr = strchr( s, '0' );

EXAMPLE FOR STRCHR():
ptr = strchr( s, c );

const char *s; points to the string that is to be scanned for the presence of
the character.
int c; is the character to look for.
char *ptr; points to the first occurrence of the character in the string. If the
character does not appear in the string, the NULL pointer is returned.
STRCSPN - scan string for one or more
            characters.
 •   "strcspn" scans through "s" and finds the first character in "s" that also
     appears in "stop“.]
 •   It returns the position of that character within "s“.
 •   Another way of saying this is that "strcspn" returns the number of
     characters at the beginning of "s" that are NOT found in "stop".

 EXAMPLE FOR STRCSPN():
 i = strcspn( s, stop );

 const char *s; points to the string to be scanned.
 const char *stop; points to a string containing the set of "stopping"
 Characters.
 size_t i; gives the position of the first character from "stop“
  that appears in "s".
STRPBRK - find first character from set
              in string.
 •   "strpbrk" looks through "string" character by character until it finds one of
     the characters in "set“.
 •   For example, if "set" is a string consisting of a blank and a tab, "strpbrk"
     would return a pointer to the first blank or tab appearing in "string".

 EXAMPLE FOR STRPBRK():
 ptr = strpbrk(string,set);

 const char *string; is the string you want to examine.
 const char *set; gives a set of characters to look for.
 char *ptr; points to the first character in "string" that also appears in "set". If
 no characters from "set" can be found in "string", "strpbrk" returns a null
 pointer.
STRRCHR - find last occurrence of a
     character in a string.
•   "strrchr" returns a pointer to the last occurrence of the character "c" in the
    string "s".

EXAMPLE FOR STRRCHR():
ptr = strrchr( s, c );

const char *s; points to the string that is to be scanned for the presence of
the character.
int c; is the character to look for.
char *ptr; points to the last occurrence of the character in the string. If the
character does not appear in the string, a null pointer is returned.
STRSPN - scan string for span of
             characters.
•   "strspn" returns the number of characters from the string "set" that appear
    at the beginning of the string pointed to by "s".

EXAMPLE FOR STRSPN().
i = strspn( s, set );

const char *s; points to the string to be scanned.
const char *set; points to a string containing the set of characters to scan for.
size_t i; is the number of characters at the beginning of "s" which also appear
in the string "set".
STRSTR - obtain first substring of string.
  •   "strstr" returns a pointer to the first occurrence of a substring within
      another string.

  EXAMPLE FOR STRSTR():
  ptr = strstr( s, subs );

  const char *s; points to the string to be scanned.
  const char *subs; points to the (sub)string to scan for.
  char *ptr; points to the first occurrence of the substring in the given string. If
  the substring is not found, this will be a null pointer.
MEMSET - set memory to specific value.
 •   "memset" sets N bytes to the given value, beginning at the byte pointed to
     by "p".

 EXAMPLE FOR MEMSET():
 ret = memset( p, value, N ); Where:

 void *p; points to the beginning of the memory that should be set to the
 given value.
 int value; is the value that is to be assigned to each byte.
 size_t N; is the number of bytes whose value is to be set.
 void *ret; points to the beginning of the initialized memory.
STRERROR - error message
      associated with number.
•   The "strerror" function returns a string describing the error associated with
    the given error number.

EXAMPLE FOR STRERROR():
msg = strerror(errnum); Where:

int errnum; is an error number (that is, one of the recognized values that
"errno" may take).
char *msg; is a message explaining the meaning of the given error number.
STRLEN - find the length of a string.
 •   "strlen" returns the number of characters in a string.

 EXAMPLE FOR STRLEN():
 i = strlen( s );

 const char *s; points to the string whose length is to be determined. This
 string must end with the usual '0‘.
 size_t i; is the number of characters in the string "s" (not counting the '0').
THANK YOU

More Related Content

PPTX
String in c programming
PPT
Bitwise operators
PPTX
Programming in c Arrays
PPTX
Inline function in C++
PPT
Arithmetic circuits
PPT
358 33 powerpoint-slides_15-hashing-collision_chapter-15
PPT
Strings
PPTX
Ternary operator
String in c programming
Bitwise operators
Programming in c Arrays
Inline function in C++
Arithmetic circuits
358 33 powerpoint-slides_15-hashing-collision_chapter-15
Strings
Ternary operator

What's hot (20)

PPT
structure and union
PPT
One Dimensional Array
PPTX
Number System
PDF
Character Array and String
PPTX
Basics of Object Oriented Programming in Python
PPTX
Data types in C
PPT
Algebraic structures
PPTX
C language ppt
PPT
Bubble sort
PPT
Array in c
PPTX
PDF
Python list
PPT
Strings Functions in C Programming
PPTX
Merge sort algorithm
PPTX
Looping statement in python
PPTX
Transposition Cipher
PDF
c++ lab manual
PPTX
Data types
PPT
DATA REPRESENTATION
PPTX
Loops in c language
structure and union
One Dimensional Array
Number System
Character Array and String
Basics of Object Oriented Programming in Python
Data types in C
Algebraic structures
C language ppt
Bubble sort
Array in c
Python list
Strings Functions in C Programming
Merge sort algorithm
Looping statement in python
Transposition Cipher
c++ lab manual
Data types
DATA REPRESENTATION
Loops in c language
Ad

Viewers also liked (11)

PDF
C programming & data structure [character strings & string functions]
PPT
Application of Stacks
PPT
Functions in C++
PPT
PPTX
Strings in C
PPT
Stack Data Structure & It's Application
PDF
Applications of stack
PPSX
DOC
String in c
PPTX
STACKS IN DATASTRUCTURE
C programming & data structure [character strings & string functions]
Application of Stacks
Functions in C++
Strings in C
Stack Data Structure & It's Application
Applications of stack
String in c
STACKS IN DATASTRUCTURE
Ad

Similar to String & its application (20)

PDF
[ITP - Lecture 17] Strings in C/C++
PDF
Strings in c mrs.sowmya jyothi
PPTX
cprogramming strings.pptx
PPTX
cprogramming strings.pptx
PDF
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
PPT
Cfbcgdhfghdfhghggfhghghgfhgfhgfhhapter11.PPT
PPT
THE FORMAT AND USAGE OF STRINGS IN C.PPT
PPT
Chapterabcdefghijklmnopqrdstuvwxydanniipo
PPT
14 strings
PPTX
C programming - String
PPT
PPT
strings
PPT
Unit-3 Strings.pptreeeeeeeeeeeeeereeeeere
PPT
Strings(2007)
PPTX
Strings CPU GTU
PPT
string function with example...................
PPTX
programming for problem solving using C-STRINGSc
PPT
Lesson in Strings for C Programming Lessons
PPTX
Strings and pointers
PPTX
String (Computer programming and utilization)
[ITP - Lecture 17] Strings in C/C++
Strings in c mrs.sowmya jyothi
cprogramming strings.pptx
cprogramming strings.pptx
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
Cfbcgdhfghdfhghggfhghghgfhgfhgfhhapter11.PPT
THE FORMAT AND USAGE OF STRINGS IN C.PPT
Chapterabcdefghijklmnopqrdstuvwxydanniipo
14 strings
C programming - String
strings
Unit-3 Strings.pptreeeeeeeeeeeeeereeeeere
Strings(2007)
Strings CPU GTU
string function with example...................
programming for problem solving using C-STRINGSc
Lesson in Strings for C Programming Lessons
Strings and pointers
String (Computer programming and utilization)

More from Tech_MX (20)

PPTX
Virtual base class
PPTX
Uid
PPTX
Theory of estimation
PPTX
Templates in C++
PPTX
Statistical quality__control_2
PPTX
Stack data structure
PPTX
Spss
PPTX
Spanning trees & applications
PPTX
Set data structure 2
PPTX
Set data structure
PPTX
Real time Operating System
PPTX
Parsing
PPTX
Mouse interrupts (Assembly Language & C)
PPT
Motherboard of a pc
PPTX
More on Lex
PPTX
MultiMedia dbms
PPTX
Merging files (Data Structure)
PPTX
Memory dbms
PPTX
Linkers
PPTX
Linear regression
Virtual base class
Uid
Theory of estimation
Templates in C++
Statistical quality__control_2
Stack data structure
Spss
Spanning trees & applications
Set data structure 2
Set data structure
Real time Operating System
Parsing
Mouse interrupts (Assembly Language & C)
Motherboard of a pc
More on Lex
MultiMedia dbms
Merging files (Data Structure)
Memory dbms
Linkers
Linear regression

Recently uploaded (20)

PPTX
Power Point PR B.Inggris 12 Ed. 2019.pptx
PDF
LATAM’s Top EdTech Innovators Transforming Learning in 2025.pdf
PDF
Physical pharmaceutics two in b pharmacy
PPTX
Theoretical for class.pptxgshdhddhdhdhgd
PPT
hsl powerpoint resource goyloveh feb 07.ppt
PPTX
ENGlishGrade8_Quarter2_WEEK1_LESSON1.pptx
PPTX
Neurology of Systemic disease all systems
PPTX
Cite It Right: A Compact Illustration of APA 7th Edition.pptx
PDF
anganwadi services for the b.sc nursing and GNM
PPTX
PAIN PATHWAY & MANAGEMENT OF ACUTE AND CHRONIC PAIN SPEAKER: Dr. Rajasekhar ...
PDF
Chevening Scholarship Application and Interview Preparation Guide
PDF
WHAT NURSES SAY_ COMMUNICATION BEHAVIORS ASSOCIATED WITH THE COMP.pdf
PPTX
climate change of delhi impacts on climate and there effects
PDF
Horaris_Grups_25-26_Definitiu_15_07_25.pdf
PPTX
Copy of ARAL Program Primer_071725(1).pptx
PPTX
Unit1_Kumod_deeplearning.pptx DEEP LEARNING
PPTX
MMW-CHAPTER-1-final.pptx major Elementary Education
PDF
faiz-khans about Radiotherapy Physics-02.pdf
PDF
Health aspects of bilberry: A review on its general benefits
PDF
Review of Related Literature & Studies.pdf
Power Point PR B.Inggris 12 Ed. 2019.pptx
LATAM’s Top EdTech Innovators Transforming Learning in 2025.pdf
Physical pharmaceutics two in b pharmacy
Theoretical for class.pptxgshdhddhdhdhgd
hsl powerpoint resource goyloveh feb 07.ppt
ENGlishGrade8_Quarter2_WEEK1_LESSON1.pptx
Neurology of Systemic disease all systems
Cite It Right: A Compact Illustration of APA 7th Edition.pptx
anganwadi services for the b.sc nursing and GNM
PAIN PATHWAY & MANAGEMENT OF ACUTE AND CHRONIC PAIN SPEAKER: Dr. Rajasekhar ...
Chevening Scholarship Application and Interview Preparation Guide
WHAT NURSES SAY_ COMMUNICATION BEHAVIORS ASSOCIATED WITH THE COMP.pdf
climate change of delhi impacts on climate and there effects
Horaris_Grups_25-26_Definitiu_15_07_25.pdf
Copy of ARAL Program Primer_071725(1).pptx
Unit1_Kumod_deeplearning.pptx DEEP LEARNING
MMW-CHAPTER-1-final.pptx major Elementary Education
faiz-khans about Radiotherapy Physics-02.pdf
Health aspects of bilberry: A review on its general benefits
Review of Related Literature & Studies.pdf

String & its application

  • 1. STRINGS AND ITS APPLICATIONS
  • 2. CONTENTS  Introduction to Strings.  Two Ways of using Strings.  Initializing a string.  Print a string.  String Functions and its applications.
  • 3. Introduction to Strings  A String is an array of characters.  Strings are stored in an array of char type along with the null terminating character "0" at the end.  When sizing the string array we need to add plus one to the actual size of the string to make space for the null terminating character, "0"  But the null character is not included in the String.  Character Strings are often used to build meaningful and Readable programs. The Common Operations performed on Character Strings include  Reading and Writing Strings.  Combined Strings together.  Copying one String to another.  Comparing Strings for Equality.  Extracting a portion of a String.
  • 4. Two ways of using Strings:  Using with a Character Array  Using with a String Pointer With a Character Array:  A character array is declared in the same way as a normal array. char ca[10];  We must set the value of each individual element of the array to the character We want and we must make the last character as 0. Remember to use %s when printing the string.
  • 5. With String Pointers  String pointers are declared as a pointer to a char. char *sp;  When we assign a value to the string pointer it will automatically put the 0 . char *sp; sp=“Hello”; printf("%s",sp);  We can read a string into only a character array using scanf and not a string pointer.  If we want to read into a string pointer then we must make it point to a character array. char ca[10],*sp; scanf("%s",ca); sp = ca; scanf("%s",sp);
  • 6. Syntax for declaring a String: Syntax to declare a string in C: char string_name[size];  Sample Code: char fname[4];  The above statement declares a string called fname that can take up to 3 characters. It can be indexed just as a regular array as well. fname[] = {'t','w','o'};  The last character is the null character having ASCII value zero. Initializing a String • To initialize our fname string store the name Ravi, char fname[31] = {“Ravi"}; • We can observe from the above statement that initializing a string is same as with any array. However we also need to surround the string with quotes.
  • 7. Print a String :  To write strings to the terminal, we use a file stream known as stdout. The most common function to use for writing to stdout in C is the printf function, defined as follows: int printf(const char *format, ...);  To print out a prompt for the user we can: printf("Please type a name: n");   The above statement prints the prompt in the quotes and moves the cursor to the next line.
  • 8. FUNCTIONS IN STRING & ITS APPLICATION
  • 9. MEMCPY - copy characters to non-overlapping string. • "memcpy" copies exactly N characters from the string "s2" into the area of memory pointed to by "s1“. • Unlike the function "strncpy", "memcpy" does not check for the terminating '0' of string "s2"; it simply copies N characters. • It does not put a terminating '0' on the end of string "s1". Example for memcpy(): ptr = memcpy( s1, s2, N ); void *s1; points to an area of memory that is to receive the copied characters. This must be able to hold at least N characters. const void *s2; points to the string from which characters will be copied. size_t N; gives the number of characters to copy. void *ptr; points to the copied string (i.e. "s1").
  • 10. MEMMOVE - copy characters to (possibly overlapping) string. • "memmove" moves exactly N characters from the string "s2" into the area of memory pointed to by "s1“. • Unlike the function "strncpy", "memmove" does not check for the terminating '0' of string "s2"; it simply moves N characters. • It does not put a terminating '0' on the end of string "s1". EXAMPLE FOR MEMMOVE(): ptr = memmove( s1, s2, N ); void *s1; points to an area of memory that is to receive the moved characters. This must be able to hold at least N characters. const void *s2; points to the string from which characters will be copied. size_t N; gives the number of characters to copy. void *ptr; points to the copied string (i.e. "s1").
  • 11. STRCPY - copy one string to another. • "strcpy" copies the string "s2" into the area pointed to by "s1“. • This area must be big enough to hold the contents of "s2“. • Since "strcpy" moves the '0' at the end of "s2", the new string "s1" will have the usual terminating '0'. EXAMPLE FOR STRCPY(): ptr = strcpy( s1, s2 ); char *s1; points to an area of memory that is to receive the copied characters. const char *s2; points to the string from which characters will be copied. This must end with the usual '0‘. char *ptr; points to the copied string (i.e. "s1").
  • 12. STRNCPY - copy characters from string. • "strncpy" copies at most N characters from the string "s2" into the area of memory pointed to by "s1“. • If it encounters a '0' before it has copied N characters, it pads "s1" to N characters by adding '0' characters. • If "s2" is more than N-1 characters long, the first N characters will be copied and the string "s1" will NOT receive the usual terminating '0'. EXAMPLE FOR STRNCPY(): ptr = strncpy( s1, s2, N ); char *s1; points to an area of memory that is to receive the copied characters. This must be able to hold at least N characters. const char *s2; points to the string from which characters will be copied. size_t N; gives the number of characters to copy. char *ptr; points to the copied string (i.e. "s1").
  • 13. STRCAT - append a string to another. • "strcat" appends a copy of the string "s2" to the end of the string "s1“. • Both strings "s1" and "s2" must be terminated by the usual '0' character. EXAMPLE FOR STRCAT(): ptr = strcat( s1, s2 ); char *s1; points to a string terminated by the usual '0‘. const char *s2; points to a string that will be appended to "s1“. char *ptr; points to the new string ("s1").
  • 14. STRNCAT - append part of a string to another. • "strncat" appends at most N characters from the string "s2" to the end of the string "s1“. • If string "s2" contains less than N characters, "strncat" will stop when it encounters the terminating '0'. EXAMPLE FOR STRNCAT(): ptr = strncat( s1, s2, N ); char *s1;points to a string terminated by the usual '0‘. const char *s2;points to a string whose characters will be appended to the end of "s1“ size_t N;is the maximum number of characters from string "s2" that should be appended to "s1“. char *ptr;points to the new string ("s1").
  • 15. MEMCMP - compare parts of two strings. • "memcmp" compares the first N characters of the string "s1" to the first N characters of the string "s2". • Unlike the function "strncmp", "memcmp" does not check for a '0' terminating either string. Thus it examines a full N characters, even if the strings are not actually that long. EXAMPLE FOR MEMCMP(): i = memcmp( s1, s2, N ); const void *s1, *s2; are the strings to be compared. size_t N; gives the number of characters to be examined. int i; gives the results of the comparison. "i" is zero if the first N characters of the strings are identical. "i" is positive if string "s1" is greater than string "s2", and is negative if string "s2" is greater than string "s1". Comparisons of "greater than" and "less than" are made according to the ASCII collating sequence.
  • 16. STRCMP - compare two strings. • "strcmp" compares the string "s1" to the string "s2". Both strings must be terminated by the usual '0' character. EXAMPLE FOR STRCMP(): i = strcmp( s1, s2 ); const char *s1, *s2; are the strings to be compared. int i; gives the results of the comparison. "i" is zero if the strings are identical. "i" is positive if string "s1" is greater than string "s2", and is negative if string "s2" is greater than string "s1". Comparisons of "greater than" and "less than" are made according to the ASCII collating sequence.
  • 17. STRNCMP - compare parts of two strings. • "strncmp" compares the first N characters of the string "s1" to the first N characters of the string "s2“. • If one or both of the strings is shorter than N characters (i.e. if "strncmp" encounters a '0'), comparisons will stop at that point. • Thus N represents the maximum number of characters to be examined, not the exact number. EXAMPLE FOR STRNCMP(): i = strncmp( s1, s2, N ); const char *s1, *s2; are the strings to be compared. size_t N; gives the number of characters to be examined. int i; gives the results of the comparison. "i" is zero if the first N characters of the strings are identical. "i" is positive if string "s1" is greater than string "s2", and is negative if string "s2" is greater than string "s1". Comparisons of "greater than" and "less than" are made according to the ASCII collating sequence.
  • 18. MEMCHR - find first occurrence of a character in a string. • "memchr" returns a pointer to the first occurrence of the character "c" in the first N characters of the string "s". • Unlike "strchr", "memchr" ignores any '0' bytes it comes across; thus it does not stop if it finds the end of the string, but keeps on going until it has looked at N characters. EXAMPLE FOR MEMCHR(): ptr = memchr( s, c, N ); const void *s; points to the string to be scanned. int c; is the character to look for. size_t N; is the number of characters to look at before giving up. void *ptr; points to the first occurrence of the character in the string. If the character is not found, the NULL pointer is returned.
  • 19. STRCHR - find first occurrence of a character in a string. • "strchr" returns a pointer to the first occurrence of the character "c" in the string "s". • The '0' that terminates the string is considered part of the string; thus it is possible to find the end of the string with a call like ptr = strchr( s, '0' ); EXAMPLE FOR STRCHR(): ptr = strchr( s, c ); const char *s; points to the string that is to be scanned for the presence of the character. int c; is the character to look for. char *ptr; points to the first occurrence of the character in the string. If the character does not appear in the string, the NULL pointer is returned.
  • 20. STRCSPN - scan string for one or more characters. • "strcspn" scans through "s" and finds the first character in "s" that also appears in "stop“.] • It returns the position of that character within "s“. • Another way of saying this is that "strcspn" returns the number of characters at the beginning of "s" that are NOT found in "stop". EXAMPLE FOR STRCSPN(): i = strcspn( s, stop ); const char *s; points to the string to be scanned. const char *stop; points to a string containing the set of "stopping" Characters. size_t i; gives the position of the first character from "stop“ that appears in "s".
  • 21. STRPBRK - find first character from set in string. • "strpbrk" looks through "string" character by character until it finds one of the characters in "set“. • For example, if "set" is a string consisting of a blank and a tab, "strpbrk" would return a pointer to the first blank or tab appearing in "string". EXAMPLE FOR STRPBRK(): ptr = strpbrk(string,set); const char *string; is the string you want to examine. const char *set; gives a set of characters to look for. char *ptr; points to the first character in "string" that also appears in "set". If no characters from "set" can be found in "string", "strpbrk" returns a null pointer.
  • 22. STRRCHR - find last occurrence of a character in a string. • "strrchr" returns a pointer to the last occurrence of the character "c" in the string "s". EXAMPLE FOR STRRCHR(): ptr = strrchr( s, c ); const char *s; points to the string that is to be scanned for the presence of the character. int c; is the character to look for. char *ptr; points to the last occurrence of the character in the string. If the character does not appear in the string, a null pointer is returned.
  • 23. STRSPN - scan string for span of characters. • "strspn" returns the number of characters from the string "set" that appear at the beginning of the string pointed to by "s". EXAMPLE FOR STRSPN(). i = strspn( s, set ); const char *s; points to the string to be scanned. const char *set; points to a string containing the set of characters to scan for. size_t i; is the number of characters at the beginning of "s" which also appear in the string "set".
  • 24. STRSTR - obtain first substring of string. • "strstr" returns a pointer to the first occurrence of a substring within another string. EXAMPLE FOR STRSTR(): ptr = strstr( s, subs ); const char *s; points to the string to be scanned. const char *subs; points to the (sub)string to scan for. char *ptr; points to the first occurrence of the substring in the given string. If the substring is not found, this will be a null pointer.
  • 25. MEMSET - set memory to specific value. • "memset" sets N bytes to the given value, beginning at the byte pointed to by "p". EXAMPLE FOR MEMSET(): ret = memset( p, value, N ); Where: void *p; points to the beginning of the memory that should be set to the given value. int value; is the value that is to be assigned to each byte. size_t N; is the number of bytes whose value is to be set. void *ret; points to the beginning of the initialized memory.
  • 26. STRERROR - error message associated with number. • The "strerror" function returns a string describing the error associated with the given error number. EXAMPLE FOR STRERROR(): msg = strerror(errnum); Where: int errnum; is an error number (that is, one of the recognized values that "errno" may take). char *msg; is a message explaining the meaning of the given error number.
  • 27. STRLEN - find the length of a string. • "strlen" returns the number of characters in a string. EXAMPLE FOR STRLEN(): i = strlen( s ); const char *s; points to the string whose length is to be determined. This string must end with the usual '0‘. size_t i; is the number of characters in the string "s" (not counting the '0').