SlideShare a Scribd company logo
STRING HANDLING IN C++
By: Muhammad Fahim
Student of BSCS
AJKU University
CONTENTS:
 String
 String inputs
 Array of string
 String function
STRING
A collection of characters written in
double quotation is called string.
it may consist of alphabatic
characters, digits and special
symbols.
STRING DECLARATION
C++ stores a string as an array of
characters. An array is a group of
contegious memory location that can be
stored same type of data.
So, the string declaration is same as
array declaration. e.g
 int a[20]
 char ch[50] etc.
STRING INITIALIZATION
A string variable can be initialized with
a string value as follows:
char str[50]=“oop in c++”;
It can also initialize without defining the
length of string:
Char a[]=“pakistan”;
STRING INPUTS
THE ‘CIN’ OBJECT
CIN.GETLINE()
 The cin object is
used to input a
string value without
any blank space. It
does not support a
string with spaces.
 The cin object is used
to input a string value
including blank space.
 The syntax of this
function is:
 Cin.getline(str,len);
CIN.GET()
The get() function of cin object is
used to input a single character. The
syntax of this function is as follows:
cin.get(ch);
ARRAY OF STRINGS
An array of string is actually a two
dimensional array of characters. Each row
of the array represents one string
Syntax:
char str[3][5];
INITIALIZING ARRAY OF STRING:
An array of string is initialize in different
ways.it can initialize by assigning individual
character to each index. It can also be
initialize by complete string
char str[2][3]={‘a’,’c’,’f’,’e’,’r’,’e’};
char str[2][3]={“ali”,”zan”};
STRING FUNCTIONS (STRING.H)
Memcpy():
The memcpy() is used to copy the number of
specified character from first buffer to second
buffer.
Syntax:
memcpy(buffer2,buffer1,size);
STRCMP():
The strcmp() is used to compare two string
character by character
Syntax:
strcmp(str1,str2);
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char string1[];
char string2[];
int result;
cout << "enter first string: " << endl;
cin >> string1;
cout << "enter second string: " << endl;
cin >> string2;
result = strcmp( string1, string2 );
switch( result )
{
case ( 1 ):
cout << "First string is greater than second string " << endl;
break;
case ( -1 ):
cout << "First string is less than second string " << endl; }
STRCPY()
The word strcpy stand for
string copy.so the function
strcpy() is used to copy
one string to another.
Syntax:
strcpy(str1,str2);
/* strcpy example */
#include <iostream>
#include <string.h>
int main ()
{
char str1[]="Sample string";
char str2[40];
char str3[40];
strcpy (str2,str1);
strcpy (str3,"copy successful");
Cout<<str1<<str2<<str3<<endl;
}
STRLEN():
The word strlen stands for
string length. The function is
used for find the length of
string. This includes all
character and spaces as well
Syntax:
strlen(str);
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char data[50];
int size=0;;
cout<<"please input the wordn";
cin>>data[50];
size=strlen(data);
cout<<"the number of letters is "<<size;
return 0;
}
STRCAT():
Strcat() is used to append a
copy of one string to the end
of another;
Syntax:
strcat(str1,str2);
//Example concatenates two strings to output //By
asking the user for input
#include <cstring>
#include <iostream>
using namespace std;
int main()
{
char *s1 = new char[30];
char *s2 = new char[30];
cout<<"Enter string one(without spaces): ";
cin>>s1;
cout<<"Enter string two(without spaces): ";
cin>>s2;
cout<<strcat(s1, s2);
}
String Handling in c++

More Related Content

What's hot

File handling in C
File handling in CFile handling in C
File handling in C
Kamal Acharya
 
Handling of character strings C programming
Handling of character strings C programmingHandling of character strings C programming
Handling of character strings C programming
Appili Vamsi Krishna
 
Structure in c language
Structure in c languageStructure in c language
Structure in c language
sangrampatil81
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
Vineeta Garg
 
String C Programming
String C ProgrammingString C Programming
String C Programming
Prionto Abdullah
 
Function in C
Function in CFunction in C
Function in C
Dr. Abhineet Anand
 
Exception handling in c++
Exception handling in c++Exception handling in c++
Exception handling in c++
imran khan
 
String In C Language
String In C Language String In C Language
String In C Language
Simplilearn
 
Tuple in python
Tuple in pythonTuple in python
Tuple in python
Sharath Ankrajegowda
 
datatypes and variables in c language
 datatypes and variables in c language datatypes and variables in c language
datatypes and variables in c language
Rai University
 
Strings in C
Strings in CStrings in C
Strings in C
Kamal Acharya
 
Strings in c++
Strings in c++Strings in c++
Strings in c++
Neeru Mittal
 
Decision Making and Looping
Decision Making and LoopingDecision Making and Looping
Decision Making and Looping
Munazza-Mah-Jabeen
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
Mohammed Sikander
 
Data Types and Variables In C Programming
Data Types and Variables In C ProgrammingData Types and Variables In C Programming
Data Types and Variables In C Programming
Kamal Acharya
 
structure and union
structure and unionstructure and union
structure and unionstudent
 
String classes and its methods.20
String classes and its methods.20String classes and its methods.20
String classes and its methods.20myrajendra
 

What's hot (20)

File handling in C
File handling in CFile handling in C
File handling 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
 
Strings
StringsStrings
Strings
 
Structure in c language
Structure in c languageStructure in c language
Structure in c language
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
String C Programming
String C ProgrammingString C Programming
String C Programming
 
Function in C
Function in CFunction in C
Function in C
 
Exception handling in c++
Exception handling in c++Exception handling in c++
Exception handling in c++
 
String In C Language
String In C Language String In C Language
String In C Language
 
Tuple in python
Tuple in pythonTuple in python
Tuple in python
 
datatypes and variables in c language
 datatypes and variables in c language datatypes and variables in c language
datatypes and variables in c language
 
Strings in C
Strings in CStrings in C
Strings in C
 
Strings
StringsStrings
Strings
 
String functions in C
String functions in CString functions in C
String functions in C
 
Strings in c++
Strings in c++Strings in c++
Strings in c++
 
Decision Making and Looping
Decision Making and LoopingDecision Making and Looping
Decision Making and Looping
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Data Types and Variables In C Programming
Data Types and Variables In C ProgrammingData Types and Variables In C Programming
Data Types and Variables In C Programming
 
structure and union
structure and unionstructure and union
structure and union
 
String classes and its methods.20
String classes and its methods.20String classes and its methods.20
String classes and its methods.20
 

Viewers also liked

String in c
String in cString in c
String in c
Suneel Dogra
 
Handling Exceptions In C &amp; C++ [Part B] Ver 2
Handling Exceptions In C &amp; C++ [Part B] Ver 2Handling Exceptions In C &amp; C++ [Part B] Ver 2
Handling Exceptions In C &amp; C++ [Part B] Ver 2
ppd1961
 
String in programming language in c or c++
 String in programming language  in c or c++  String in programming language  in c or c++
String in programming language in c or c++
Samsil Arefin
 
String c++
String c++String c++
String c++
Sedu
 
Strinng Classes in c++
Strinng Classes in c++Strinng Classes in c++
Strinng Classes in c++Vikash Dhal
 
File handling in C++
File handling in C++File handling in C++
File handling in C++
Hitesh Kumar
 
Handling Exceptions In C &amp; C++[Part A]
Handling Exceptions In C &amp; C++[Part A]Handling Exceptions In C &amp; C++[Part A]
Handling Exceptions In C &amp; C++[Part A]
ppd1961
 
String operation
String operationString operation
String operation
Shakila Mahjabin
 
Exception Handling in C++
Exception Handling in C++Exception Handling in C++
Exception Handling in C++
Deepak Tathe
 
C programming - String
C programming - StringC programming - String
C programming - String
Achyut Devkota
 
Exception handling and templates
Exception handling and templatesException handling and templates
Exception handling and templatesfarhan amjad
 
String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)Ravi_Kant_Sahu
 
Exception handling in c++ by manoj vasava
Exception handling in c++ by manoj vasavaException handling in c++ by manoj vasava
Exception handling in c++ by manoj vasava
Manoj_vasava
 
C/C++ History in few slides
C/C++ History in few slides C/C++ History in few slides
History of c++
History of c++ History of c++
History of c++
Ihsan Wassan
 

Viewers also liked (20)

05 c++-strings
05 c++-strings05 c++-strings
05 c++-strings
 
String in c
String in cString in c
String in c
 
Strings in Java
Strings in JavaStrings in Java
Strings in Java
 
Handling Exceptions In C &amp; C++ [Part B] Ver 2
Handling Exceptions In C &amp; C++ [Part B] Ver 2Handling Exceptions In C &amp; C++ [Part B] Ver 2
Handling Exceptions In C &amp; C++ [Part B] Ver 2
 
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++
 
Case studies
Case studiesCase studies
Case studies
 
New presentation1
New presentation1New presentation1
New presentation1
 
String c++
String c++String c++
String c++
 
Strinng Classes in c++
Strinng Classes in c++Strinng Classes in c++
Strinng Classes in c++
 
File handling in C++
File handling in C++File handling in C++
File handling in C++
 
Handling Exceptions In C &amp; C++[Part A]
Handling Exceptions In C &amp; C++[Part A]Handling Exceptions In C &amp; C++[Part A]
Handling Exceptions In C &amp; C++[Part A]
 
String operation
String operationString operation
String operation
 
Exception Handling in C++
Exception Handling in C++Exception Handling in C++
Exception Handling in C++
 
C programming - String
C programming - StringC programming - String
C programming - String
 
Exception handling and templates
Exception handling and templatesException handling and templates
Exception handling and templates
 
String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)
 
Exception handling in c++ by manoj vasava
Exception handling in c++ by manoj vasavaException handling in c++ by manoj vasava
Exception handling in c++ by manoj vasava
 
String Handling
String HandlingString Handling
String Handling
 
C/C++ History in few slides
C/C++ History in few slides C/C++ History in few slides
C/C++ History in few slides
 
History of c++
History of c++ History of c++
History of c++
 

Similar to String Handling in c++

String handling
String handlingString handling
String handling
IyeTech - Pakistan
 
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
STRINGS IN C MRS.SOWMYA JYOTHI.pdfSTRINGS IN C MRS.SOWMYA JYOTHI.pdf
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
String notes
String notesString notes
String notes
Prasadu Peddi
 
Unitii string
Unitii stringUnitii string
Unitii string
Sowri Rajan
 
Strings part2
Strings part2Strings part2
Strings part2
yndaravind
 
Lecture 15_Strings and Dynamic Memory Allocation.pptx
Lecture 15_Strings and  Dynamic Memory Allocation.pptxLecture 15_Strings and  Dynamic Memory Allocation.pptx
Lecture 15_Strings and Dynamic Memory Allocation.pptx
JawadTanvir
 
String_C.pptx
String_C.pptxString_C.pptx
String_C.pptx
HARSHITHA EBBALI
 
PPS Presentation.pptx
PPS Presentation.pptxPPS Presentation.pptx
PPS Presentation.pptx
Sharan288059
 
Strings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothiStrings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothi
Sowmya Jyothi
 
String in programming language in c or c++
String in programming language in c or c++String in programming language in c or c++
String in programming language in c or c++
Azeemaj101
 
C q 3
C q 3C q 3
16 strings-and-text-processing-120712074956-phpapp02
16 strings-and-text-processing-120712074956-phpapp0216 strings-and-text-processing-120712074956-phpapp02
16 strings-and-text-processing-120712074956-phpapp02Abdul Samee
 
Strings
StringsStrings
Strings
Dhiviya Rose
 
Arrays & Strings.pptx
Arrays & Strings.pptxArrays & Strings.pptx
Arrays & Strings.pptx
AnkurRajSingh2
 
Strings
StringsStrings
Strings
Saranya saran
 
introduction to strings in c programming
introduction to strings in c programmingintroduction to strings in c programming
introduction to strings in c programming
mikeymanjiro2090
 
9 character string &amp; string library
9  character string &amp; string library9  character string &amp; string library
9 character string &amp; string library
MomenMostafa
 

Similar to String Handling in c++ (20)

String handling
String handlingString handling
String handling
 
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 notes
String notesString notes
String notes
 
Unitii string
Unitii stringUnitii string
Unitii string
 
Strings part2
Strings part2Strings part2
Strings part2
 
Lecture 15_Strings and Dynamic Memory Allocation.pptx
Lecture 15_Strings and  Dynamic Memory Allocation.pptxLecture 15_Strings and  Dynamic Memory Allocation.pptx
Lecture 15_Strings and Dynamic Memory Allocation.pptx
 
String_C.pptx
String_C.pptxString_C.pptx
String_C.pptx
 
PPS Presentation.pptx
PPS Presentation.pptxPPS Presentation.pptx
PPS Presentation.pptx
 
Strings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothiStrings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothi
 
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++
 
Strings in c++
Strings in c++Strings in c++
Strings in c++
 
C q 3
C q 3C q 3
C q 3
 
16 strings-and-text-processing-120712074956-phpapp02
16 strings-and-text-processing-120712074956-phpapp0216 strings-and-text-processing-120712074956-phpapp02
16 strings-and-text-processing-120712074956-phpapp02
 
Strings
StringsStrings
Strings
 
Arrays & Strings.pptx
Arrays & Strings.pptxArrays & Strings.pptx
Arrays & Strings.pptx
 
14 strings
14 strings14 strings
14 strings
 
Team 1
Team 1Team 1
Team 1
 
Strings
StringsStrings
Strings
 
introduction to strings in c programming
introduction to strings in c programmingintroduction to strings in c programming
introduction to strings in c programming
 
9 character string &amp; string library
9  character string &amp; string library9  character string &amp; string library
9 character string &amp; string library
 

String Handling in c++

  • 1.
  • 2. STRING HANDLING IN C++ By: Muhammad Fahim Student of BSCS AJKU University
  • 3. CONTENTS:  String  String inputs  Array of string  String function
  • 4. STRING A collection of characters written in double quotation is called string. it may consist of alphabatic characters, digits and special symbols.
  • 5. STRING DECLARATION C++ stores a string as an array of characters. An array is a group of contegious memory location that can be stored same type of data. So, the string declaration is same as array declaration. e.g  int a[20]  char ch[50] etc.
  • 6. STRING INITIALIZATION A string variable can be initialized with a string value as follows: char str[50]=“oop in c++”; It can also initialize without defining the length of string: Char a[]=“pakistan”;
  • 7. STRING INPUTS THE ‘CIN’ OBJECT CIN.GETLINE()  The cin object is used to input a string value without any blank space. It does not support a string with spaces.  The cin object is used to input a string value including blank space.  The syntax of this function is:  Cin.getline(str,len);
  • 8. CIN.GET() The get() function of cin object is used to input a single character. The syntax of this function is as follows: cin.get(ch);
  • 9. ARRAY OF STRINGS An array of string is actually a two dimensional array of characters. Each row of the array represents one string Syntax: char str[3][5];
  • 10. INITIALIZING ARRAY OF STRING: An array of string is initialize in different ways.it can initialize by assigning individual character to each index. It can also be initialize by complete string char str[2][3]={‘a’,’c’,’f’,’e’,’r’,’e’}; char str[2][3]={“ali”,”zan”};
  • 11. STRING FUNCTIONS (STRING.H) Memcpy(): The memcpy() is used to copy the number of specified character from first buffer to second buffer. Syntax: memcpy(buffer2,buffer1,size);
  • 12. STRCMP(): The strcmp() is used to compare two string character by character Syntax: strcmp(str1,str2);
  • 13. #include <iostream> #include <cstring> using namespace std; int main() { char string1[]; char string2[]; int result; cout << "enter first string: " << endl; cin >> string1; cout << "enter second string: " << endl; cin >> string2; result = strcmp( string1, string2 ); switch( result ) { case ( 1 ): cout << "First string is greater than second string " << endl; break; case ( -1 ): cout << "First string is less than second string " << endl; }
  • 14. STRCPY() The word strcpy stand for string copy.so the function strcpy() is used to copy one string to another. Syntax: strcpy(str1,str2);
  • 15. /* strcpy example */ #include <iostream> #include <string.h> int main () { char str1[]="Sample string"; char str2[40]; char str3[40]; strcpy (str2,str1); strcpy (str3,"copy successful"); Cout<<str1<<str2<<str3<<endl; }
  • 16. STRLEN(): The word strlen stands for string length. The function is used for find the length of string. This includes all character and spaces as well Syntax: strlen(str);
  • 17. #include<iostream> #include<cstring> using namespace std; int main() { char data[50]; int size=0;; cout<<"please input the wordn"; cin>>data[50]; size=strlen(data); cout<<"the number of letters is "<<size; return 0; }
  • 18. STRCAT(): Strcat() is used to append a copy of one string to the end of another; Syntax: strcat(str1,str2);
  • 19. //Example concatenates two strings to output //By asking the user for input #include <cstring> #include <iostream> using namespace std; int main() { char *s1 = new char[30]; char *s2 = new char[30]; cout<<"Enter string one(without spaces): "; cin>>s1; cout<<"Enter string two(without spaces): "; cin>>s2; cout<<strcat(s1, s2); }