SlideShare una empresa de Scribd logo
1 de 28
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

Más contenido relacionado

La actualidad más candente

constants, variables and datatypes in C
constants, variables and datatypes in Cconstants, variables and datatypes in C
constants, variables and datatypes in C
Sahithi Naraparaju
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
Tareq Hasan
 

La actualidad más candente (20)

String in c programming
String in c programmingString in c programming
String in c programming
 
Strings in c
Strings in cStrings in c
Strings in c
 
Control structures in C++ Programming Language
Control structures in C++ Programming LanguageControl structures in C++ Programming Language
Control structures in C++ Programming Language
 
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
 
Function in C program
Function in C programFunction in C program
Function in C program
 
C++ file
C++ fileC++ file
C++ file
 
String in c
String in cString in c
String in c
 
Storage classes in C
Storage classes in CStorage classes in C
Storage classes in C
 
array of object pointer in c++
array of object pointer in c++array of object pointer in c++
array of object pointer in c++
 
constants, variables and datatypes in C
constants, variables and datatypes in Cconstants, variables and datatypes in C
constants, variables and datatypes in C
 
Encapsulation C++
Encapsulation C++Encapsulation C++
Encapsulation C++
 
Ppt on Linked list,stack,queue
Ppt on Linked list,stack,queuePpt on Linked list,stack,queue
Ppt on Linked list,stack,queue
 
User defined functions
User defined functionsUser defined functions
User defined functions
 
Memory Management C++ (Peeling operator new() and delete())
Memory Management C++ (Peeling operator new() and delete())Memory Management C++ (Peeling operator new() and delete())
Memory Management C++ (Peeling operator new() and delete())
 
Arrays in java
Arrays in javaArrays in java
Arrays in java
 
Intro to c++
Intro to c++Intro to c++
Intro to c++
 
Regular expressions in Python
Regular expressions in PythonRegular expressions in Python
Regular expressions in Python
 
Unit 3. Input and Output
Unit 3. Input and OutputUnit 3. Input and Output
Unit 3. Input and Output
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
 
C functions
C functionsC functions
C functions
 

Destacado (10)

C programming & data structure [character strings & string functions]
C programming & data structure   [character strings & string functions]C programming & data structure   [character strings & string functions]
C programming & data structure [character strings & string functions]
 
Application of Stacks
Application of StacksApplication of Stacks
Application of Stacks
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
String c
String cString c
String c
 
Strings in C
Strings in CStrings in C
Strings in C
 
Stack Data Structure & It's Application
Stack Data Structure & It's Application Stack Data Structure & It's Application
Stack Data Structure & It's Application
 
Stack Applications
Stack ApplicationsStack Applications
Stack Applications
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
 
Stack
StackStack
Stack
 
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURE
 

Similar a String & its application

5 2. string processing
5 2. string processing5 2. string processing
5 2. string processing
웅식 전
 
cprogramming strings.pptx
cprogramming strings.pptxcprogramming strings.pptx
cprogramming strings.pptx
LECO9
 

Similar a String & its application (20)

Strings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothiStrings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothi
 
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
 
string in C
string in Cstring in C
string in C
 
Handling of character strings C programming
Handling of character strings C programmingHandling of character strings C programming
Handling of character strings C programming
 
Lecture 05 2017
Lecture 05 2017Lecture 05 2017
Lecture 05 2017
 
String (Computer programming and utilization)
String (Computer programming and utilization)String (Computer programming and utilization)
String (Computer programming and utilization)
 
Strings
StringsStrings
Strings
 
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
 
Implementation Of String Functions In C
Implementation Of String Functions In CImplementation Of String Functions In C
Implementation Of String Functions In C
 
C string
C stringC string
C string
 
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++
 
5 2. string processing
5 2. string processing5 2. string processing
5 2. string processing
 
cprogramming strings.pptx
cprogramming strings.pptxcprogramming strings.pptx
cprogramming strings.pptx
 
cprogramming strings.pptx
cprogramming strings.pptxcprogramming strings.pptx
cprogramming strings.pptx
 
Strings
StringsStrings
Strings
 
Strings in c++
Strings in c++Strings in c++
Strings in c++
 
String notes
String notesString notes
String notes
 
Strings CPU GTU
Strings CPU GTUStrings CPU GTU
Strings CPU GTU
 
string
stringstring
string
 
string.ppt
string.pptstring.ppt
string.ppt
 

Más de Tech_MX

Virtual base class
Virtual base classVirtual base class
Virtual base class
Tech_MX
 
Theory of estimation
Theory of estimationTheory of estimation
Theory of estimation
Tech_MX
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
Tech_MX
 
Statistical quality__control_2
Statistical  quality__control_2Statistical  quality__control_2
Statistical quality__control_2
Tech_MX
 
Stack data structure
Stack data structureStack data structure
Stack data structure
Tech_MX
 
Spanning trees & applications
Spanning trees & applicationsSpanning trees & applications
Spanning trees & applications
Tech_MX
 
Set data structure 2
Set data structure 2Set data structure 2
Set data structure 2
Tech_MX
 
Set data structure
Set data structure Set data structure
Set data structure
Tech_MX
 
Real time Operating System
Real time Operating SystemReal time Operating System
Real time Operating System
Tech_MX
 
Mouse interrupts (Assembly Language & C)
Mouse interrupts (Assembly Language & C)Mouse interrupts (Assembly Language & C)
Mouse interrupts (Assembly Language & C)
Tech_MX
 
Motherboard of a pc
Motherboard of a pcMotherboard of a pc
Motherboard of a pc
Tech_MX
 
More on Lex
More on LexMore on Lex
More on Lex
Tech_MX
 
MultiMedia dbms
MultiMedia dbmsMultiMedia dbms
MultiMedia dbms
Tech_MX
 
Merging files (Data Structure)
Merging files (Data Structure)Merging files (Data Structure)
Merging files (Data Structure)
Tech_MX
 
Memory dbms
Memory dbmsMemory dbms
Memory dbms
Tech_MX
 
Linear regression
Linear regressionLinear regression
Linear regression
Tech_MX
 

Más de Tech_MX (20)

Virtual base class
Virtual base classVirtual base class
Virtual base class
 
Uid
UidUid
Uid
 
Theory of estimation
Theory of estimationTheory of estimation
Theory of estimation
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
 
Statistical quality__control_2
Statistical  quality__control_2Statistical  quality__control_2
Statistical quality__control_2
 
Stack data structure
Stack data structureStack data structure
Stack data structure
 
Spss
SpssSpss
Spss
 
Spanning trees & applications
Spanning trees & applicationsSpanning trees & applications
Spanning trees & applications
 
Set data structure 2
Set data structure 2Set data structure 2
Set data structure 2
 
Set data structure
Set data structure Set data structure
Set data structure
 
Real time Operating System
Real time Operating SystemReal time Operating System
Real time Operating System
 
Parsing
ParsingParsing
Parsing
 
Mouse interrupts (Assembly Language & C)
Mouse interrupts (Assembly Language & C)Mouse interrupts (Assembly Language & C)
Mouse interrupts (Assembly Language & C)
 
Motherboard of a pc
Motherboard of a pcMotherboard of a pc
Motherboard of a pc
 
More on Lex
More on LexMore on Lex
More on Lex
 
MultiMedia dbms
MultiMedia dbmsMultiMedia dbms
MultiMedia dbms
 
Merging files (Data Structure)
Merging files (Data Structure)Merging files (Data Structure)
Merging files (Data Structure)
 
Memory dbms
Memory dbmsMemory dbms
Memory dbms
 
Linkers
LinkersLinkers
Linkers
 
Linear regression
Linear regressionLinear regression
Linear regression
 

Último

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 

Último (20)

Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 

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').