SlideShare a Scribd company logo
2
Most read
13
Most read
15
Most read
1
String
2
“A Collection of characters written in double quotation marks.”
String Declaration:
C++ stores a string as an array of characters.
Syntax:
char array_name[ length ];
String Initialization:
The syntax of String Initialization.
Syntax:
char array_name[ length ] = value;
char: It indicates the type of an array.
array_name: It indicates the name of an array.
length: It indicates the number of memory locations in the array.
value: It indicates the value of initialization in string.
String Input
A string value can be input from the user using different ways.
The “cin” object:
The cin object is used to input a string value without any spaces.
Syntax:
cin >> str;
The “cin.getline( )” object:
The cin.getline() object is used to input a string value including spaces.
Syntax:
cin.getline ( str, len );
The “cin.get( )” object:
The cin.get() object is used to input a single character.
Syntax:
cin.get ( ch );
str: Name of string variable.
len: Length of string variable.
ch: Any character.
3
String Input: ( Example )#include< iostream >
using namespace std;
void main()
{
char name[ 5 ];
char c;
count << "Enter your Name = ";
// "cin" Object
cin >> name; // Simple input
cout << endl << "Your Name: " << name << endl;
// "cin.getline ( str, len );" Objet
cin.getline( name, 90 ); // Take input including Space
cout << endl << "Your Name: " << name << endl;
// "cin.get ( ch );" Object
cin.get( c ); //Take one character
cout << endl << "Your Name: " << c << endl;
system ( "pause" );
}
4
Array Of Strings
Two - dimensional array of characters.
Each row represents one string.
Each character stored in separate index.
Syntax:
char str[rows][cols];
There are two methods for initializing
1. By assigning individual characters
2. By assigning complete string
For Input:
cin>>str[0];
For Output:
cout<<str[0];
5
String Functions:
memchr()
 Used to search a byte with particular value in buffer.
 SYNTAX : memchr( buffer, ch, size);
Memcmp()
 Used to compare each successive byte with corresponding
byte.
• SYNTAX : memcmp( buffer1, buffer2, size);
Memcpy()
 Used to copy the number of specified characters from 1st
buffer to 2nd buffer and returns the 1st buffer.
 SYNTAX : memcpy(buffer2 ,buffer1 ,size); 6
String Functions:
• strcat( ):
The strcat() function is used to append a copy of one string to
the end of second string. It also terminates the resulting
string with null character.
Syntax:
strcat ( srt1, str2 );
• strncat( ):
The strncat() function is used to append the specific number
of characters of one string to the end of second string. It also
terminates the resulting string with null character.
Syntax:
strncat ( srt1, str2, n ); 7
String Functions:
• strchr( ):
The strchr() function is used to find the first occurrence of
a character in string and returns a pointer to this character.
It returns NULL is character is not found.
Syntax:
strchr ( str, ch );
• strrchr( ):
The strrchr() function is used to find the last occurrence of
a character in string and returns a pointer to this character.
It returns NULL is character is not found.
Syntax:
strrchr ( str, ch ); 8
String Functions:
• stricmp( ):
The stricmp() function is used to compare two string character
by character.
Syntax:
stricmp ( str1, str1 );
• Strncmp( )
Use : compare the specified number of character in two string.
Syntax:
strncmp(str1, str 2, n);
• Strcoll( )
Use : Compare two string values the collating sequence
specified by set locale function and indicating the relationship.
Syntax:
strcoll(str1, str 2);
9
String Functions:
• Strcpy( )
Use : copy one string to another including the terminating null
character.
Syntax:
Strcpy (str1, str2);
• strncpy( )
Use : copy one string to another including the terminating null
character. It only copies only specified character.
Syntax:
strncpy (str1, str2, n);
• strlen( )
Use: find length of string.
Syntax: strlen (str);
10
String Functions:
strstr( ):
The strstr() function is used to find the first occurrence of second
string within first string and returns a pointer to this character.
It returns NULL if occurrence is not found.
Syntax:
strstr ( str1, str2 );
strpbrk( ):
The strpbrk() function locates the first occurrence in the string pointed
to by s1 of any character from the string pointed to by s2. It returns
NULL if occurrence is not found.
Syntax:
strpbrk (const char *s1, const char *s2);
11
String Functions:
12
strspn( ):
• The strspn() function compute length of maximum initial
segment of the string pointed by s1 which consist entirely of
character from the string pointed to by s2.
Syntax:
Strspn ( str1, str2 );
strerror( ):
The function takes an error number as parameter and returns a
pointer to error message associated with that error number.
The function can be called with global variable errno declared
in errno.h.
Syntax:
strerror ( errno );
String Functions:
• strrev( ):
• The function reverse all the characters in a string except the null character.
• Syntax:
strrev( str );
strset( ):
The function set all the characters in a string to the specified character accept the
null character.
Syntax: strset( str, ch );
strlwr( ):
The function converts all the characters of a string to lowercase.
Syntax:
strlwr( str );
strupr( ):
The function converts all the characters of a string to uppercase.
Syntax:
strupr( str );
13
Hello frands Question Poch lo
14
15

More Related Content

PDF
C standard library functions
PPTX
PPTX
Function in C program
PPTX
Pointers in c++
PPTX
Templates in C++
PPTX
arrays of structures
PPTX
Exception handling c++
PPT
RECURSION IN C
C standard library functions
Function in C program
Pointers in c++
Templates in C++
arrays of structures
Exception handling c++
RECURSION IN C

What's hot (20)

PDF
Java variable types
PPT
Strings Functions in C Programming
PPTX
Decision making and branching in c programming
PPTX
Type casting in java
PPTX
Dynamic memory allocation
PPT
Operation on string presentation
PPTX
Pointer in C++
PPTX
Structures and Unions
PPTX
Structures in c language
PPTX
Call by value
PPTX
PPTX
Strings in C
PPT
Strings
PPT
File handling in c
PDF
C Programming Storage classes, Recursion
PPTX
Templates in c++
PPTX
Virtual function in C++ Pure Virtual Function
PPTX
Constructor and Types of Constructors
PPT
structure and union
PPSX
C lecture 4 nested loops and jumping statements slideshare
Java variable types
Strings Functions in C Programming
Decision making and branching in c programming
Type casting in java
Dynamic memory allocation
Operation on string presentation
Pointer in C++
Structures and Unions
Structures in c language
Call by value
Strings in C
Strings
File handling in c
C Programming Storage classes, Recursion
Templates in c++
Virtual function in C++ Pure Virtual Function
Constructor and Types of Constructors
structure and union
C lecture 4 nested loops and jumping statements slideshare
Ad

Similar to String handling (20)

PPTX
Strings
PPTX
Strings cprogramminglanguagedsasheet.pptx
PPTX
String Handling in c++
PPT
Strings
PDF
Strings part2
PPT
Strings in c
PPT
Strings In C and its syntax and uses .ppt
PDF
Strings in c mrs.sowmya jyothi
PDF
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
PDF
Python Strings Methods
PDF
0-Slot21-22-Strings.pdf
PPT
strings
PPTX
Header file.pptx
PPTX
Strings in Java
PDF
String class and function for b.tech iii year students
PDF
C string _updated_Somesh_SSTC_ Bhilai_CG
PPTX
String in c programming
Strings
Strings cprogramminglanguagedsasheet.pptx
String Handling in c++
Strings
Strings part2
Strings in c
Strings In C and its syntax and uses .ppt
Strings in c mrs.sowmya jyothi
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
Python Strings Methods
0-Slot21-22-Strings.pdf
strings
Header file.pptx
Strings in Java
String class and function for b.tech iii year students
C string _updated_Somesh_SSTC_ Bhilai_CG
String in c programming
Ad

Recently uploaded (20)

PPTX
UV-Visible spectroscopy..pptx UV-Visible Spectroscopy – Electronic Transition...
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
Yogi Goddess Pres Conference Studio Updates
PPTX
UNIT III MENTAL HEALTH NURSING ASSESSMENT
PDF
LDMMIA Reiki Yoga Finals Review Spring Summer
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
Complications of Minimal Access Surgery at WLH
PPTX
master seminar digital applications in india
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Computing-Curriculum for Schools in Ghana
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
Cell Types and Its function , kingdom of life
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PPTX
History, Philosophy and sociology of education (1).pptx
PPTX
Orientation - ARALprogram of Deped to the Parents.pptx
UV-Visible spectroscopy..pptx UV-Visible Spectroscopy – Electronic Transition...
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Yogi Goddess Pres Conference Studio Updates
UNIT III MENTAL HEALTH NURSING ASSESSMENT
LDMMIA Reiki Yoga Finals Review Spring Summer
2.FourierTransform-ShortQuestionswithAnswers.pdf
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Complications of Minimal Access Surgery at WLH
master seminar digital applications in india
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
Anesthesia in Laparoscopic Surgery in India
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Computing-Curriculum for Schools in Ghana
Microbial diseases, their pathogenesis and prophylaxis
Cell Types and Its function , kingdom of life
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
History, Philosophy and sociology of education (1).pptx
Orientation - ARALprogram of Deped to the Parents.pptx

String handling

  • 1. 1
  • 2. String 2 “A Collection of characters written in double quotation marks.” String Declaration: C++ stores a string as an array of characters. Syntax: char array_name[ length ]; String Initialization: The syntax of String Initialization. Syntax: char array_name[ length ] = value; char: It indicates the type of an array. array_name: It indicates the name of an array. length: It indicates the number of memory locations in the array. value: It indicates the value of initialization in string.
  • 3. String Input A string value can be input from the user using different ways. The “cin” object: The cin object is used to input a string value without any spaces. Syntax: cin >> str; The “cin.getline( )” object: The cin.getline() object is used to input a string value including spaces. Syntax: cin.getline ( str, len ); The “cin.get( )” object: The cin.get() object is used to input a single character. Syntax: cin.get ( ch ); str: Name of string variable. len: Length of string variable. ch: Any character. 3
  • 4. String Input: ( Example )#include< iostream > using namespace std; void main() { char name[ 5 ]; char c; count << "Enter your Name = "; // "cin" Object cin >> name; // Simple input cout << endl << "Your Name: " << name << endl; // "cin.getline ( str, len );" Objet cin.getline( name, 90 ); // Take input including Space cout << endl << "Your Name: " << name << endl; // "cin.get ( ch );" Object cin.get( c ); //Take one character cout << endl << "Your Name: " << c << endl; system ( "pause" ); } 4
  • 5. Array Of Strings Two - dimensional array of characters. Each row represents one string. Each character stored in separate index. Syntax: char str[rows][cols]; There are two methods for initializing 1. By assigning individual characters 2. By assigning complete string For Input: cin>>str[0]; For Output: cout<<str[0]; 5
  • 6. String Functions: memchr()  Used to search a byte with particular value in buffer.  SYNTAX : memchr( buffer, ch, size); Memcmp()  Used to compare each successive byte with corresponding byte. • SYNTAX : memcmp( buffer1, buffer2, size); Memcpy()  Used to copy the number of specified characters from 1st buffer to 2nd buffer and returns the 1st buffer.  SYNTAX : memcpy(buffer2 ,buffer1 ,size); 6
  • 7. String Functions: • strcat( ): The strcat() function is used to append a copy of one string to the end of second string. It also terminates the resulting string with null character. Syntax: strcat ( srt1, str2 ); • strncat( ): The strncat() function is used to append the specific number of characters of one string to the end of second string. It also terminates the resulting string with null character. Syntax: strncat ( srt1, str2, n ); 7
  • 8. String Functions: • strchr( ): The strchr() function is used to find the first occurrence of a character in string and returns a pointer to this character. It returns NULL is character is not found. Syntax: strchr ( str, ch ); • strrchr( ): The strrchr() function is used to find the last occurrence of a character in string and returns a pointer to this character. It returns NULL is character is not found. Syntax: strrchr ( str, ch ); 8
  • 9. String Functions: • stricmp( ): The stricmp() function is used to compare two string character by character. Syntax: stricmp ( str1, str1 ); • Strncmp( ) Use : compare the specified number of character in two string. Syntax: strncmp(str1, str 2, n); • Strcoll( ) Use : Compare two string values the collating sequence specified by set locale function and indicating the relationship. Syntax: strcoll(str1, str 2); 9
  • 10. String Functions: • Strcpy( ) Use : copy one string to another including the terminating null character. Syntax: Strcpy (str1, str2); • strncpy( ) Use : copy one string to another including the terminating null character. It only copies only specified character. Syntax: strncpy (str1, str2, n); • strlen( ) Use: find length of string. Syntax: strlen (str); 10
  • 11. String Functions: strstr( ): The strstr() function is used to find the first occurrence of second string within first string and returns a pointer to this character. It returns NULL if occurrence is not found. Syntax: strstr ( str1, str2 ); strpbrk( ): The strpbrk() function locates the first occurrence in the string pointed to by s1 of any character from the string pointed to by s2. It returns NULL if occurrence is not found. Syntax: strpbrk (const char *s1, const char *s2); 11
  • 12. String Functions: 12 strspn( ): • The strspn() function compute length of maximum initial segment of the string pointed by s1 which consist entirely of character from the string pointed to by s2. Syntax: Strspn ( str1, str2 ); strerror( ): The function takes an error number as parameter and returns a pointer to error message associated with that error number. The function can be called with global variable errno declared in errno.h. Syntax: strerror ( errno );
  • 13. String Functions: • strrev( ): • The function reverse all the characters in a string except the null character. • Syntax: strrev( str ); strset( ): The function set all the characters in a string to the specified character accept the null character. Syntax: strset( str, ch ); strlwr( ): The function converts all the characters of a string to lowercase. Syntax: strlwr( str ); strupr( ): The function converts all the characters of a string to uppercase. Syntax: strupr( str ); 13
  • 14. Hello frands Question Poch lo 14
  • 15. 15