際際滷

際際滷Share a Scribd company logo
String Handling in c++
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 (20)

Dynamic method dispatch
Dynamic method dispatchDynamic method dispatch
Dynamic method dispatch
yugandhar vadlamudi
Files and streams
Files and streamsFiles and streams
Files and streams
Pranali Chaudhari
Exception Handling in object oriented programming using C++
Exception Handling in object oriented programming using C++Exception Handling in object oriented programming using C++
Exception Handling in object oriented programming using C++
Janki Shah
Exception handling in c++
Exception handling in c++Exception handling in c++
Exception handling in c++
imran khan
Templates in C++
Templates in C++Templates in C++
Templates in C++
Tech_MX
Strings in C language
Strings in C languageStrings in C language
Strings in C language
P M Patil
Command line arguments
Command line argumentsCommand line arguments
Command line arguments
Ashok Raj
Constructor and destructor
Constructor  and  destructor Constructor  and  destructor
Constructor and destructor
Shubham Vishwambhar
Constructors in C++
Constructors in C++Constructors in C++
Constructors in C++
RubaNagarajan
Union in C programming
Union in C programmingUnion in C programming
Union in C programming
Kamal Acharya
File handling-c
File handling-cFile handling-c
File handling-c
CGC Technical campus,Mohali
C++ STATEMENTS
C++ STATEMENTS C++ STATEMENTS
C++ STATEMENTS
Prof Ansari
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
Ritika Sharma
STRINGS IN PYTHON
STRINGS IN PYTHONSTRINGS IN PYTHON
STRINGS IN PYTHON
TanushTM1
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdfMANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
Member Function in C++
Member Function in C++ Member Function in C++
Member Function in C++
NikitaKaur10
String in c
String in cString in c
String in c
Suneel Dogra
Abstraction java
Abstraction javaAbstraction java
Abstraction java
MahinImran
Constructor in java
Constructor in javaConstructor in java
Constructor in java
Pavith Gunasekara
Function in c
Function in cFunction in c
Function in c
savitamhaske
Exception Handling in object oriented programming using C++
Exception Handling in object oriented programming using C++Exception Handling in object oriented programming using C++
Exception Handling in object oriented programming using C++
Janki Shah
Exception handling in c++
Exception handling in c++Exception handling in c++
Exception handling in c++
imran khan
Templates in C++
Templates in C++Templates in C++
Templates in C++
Tech_MX
Strings in C language
Strings in C languageStrings in C language
Strings in C language
P M Patil
Command line arguments
Command line argumentsCommand line arguments
Command line arguments
Ashok Raj
Constructors in C++
Constructors in C++Constructors in C++
Constructors in C++
RubaNagarajan
Union in C programming
Union in C programmingUnion in C programming
Union in C programming
Kamal Acharya
C++ STATEMENTS
C++ STATEMENTS C++ STATEMENTS
C++ STATEMENTS
Prof Ansari
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
Ritika Sharma
STRINGS IN PYTHON
STRINGS IN PYTHONSTRINGS IN PYTHON
STRINGS IN PYTHON
TanushTM1
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdfMANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
Member Function in C++
Member Function in C++ Member Function in C++
Member Function in C++
NikitaKaur10
Abstraction java
Abstraction javaAbstraction java
Abstraction java
MahinImran

Viewers also liked (20)

05 c++-strings
05 c++-strings05 c++-strings
05 c++-strings
Kelly Swanson
Strings
StringsStrings
Strings
Nilesh Dalvi
Strings in Java
Strings in JavaStrings in Java
Strings in Java
Abhilash Nair
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
Case studies
Case studiesCase studies
Case studies
Andrea Arteaga
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 templates
farhan 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
String Handling
String HandlingString Handling
String Handling
Bharat17485
C/C++ History in few slides
C/C++ History in few slides C/C++ History in few slides
C/C++ History in few slides
LGS, GBHS&IC, University Of South-Asia, TARA-Technologies
History of c++
History of c++ History of c++
History of c++
Ihsan Ali
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++
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
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 templates
farhan 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
String Handling
String HandlingString Handling
String Handling
Bharat17485
History of c++
History of c++ History of c++
History of c++
Ihsan Ali

Similar to String Handling in c++ (20)

Strings
StringsStrings
Strings
Imad Ali
String handling
String handlingString handling
String handling
IyeTech - Pakistan
Principals of Programming in CModule -5.pdfModule-4.pdf
Principals of Programming in CModule -5.pdfModule-4.pdfPrincipals of Programming in CModule -5.pdfModule-4.pdf
Principals of Programming in CModule -5.pdfModule-4.pdf
anilcsbs
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
POEPPSGNHwkejnkweoewnkenjwjewkjewoewkjewijewjk
POEPPSGNHwkejnkweoewnkenjwjewkjewoewkjewijewjkPOEPPSGNHwkejnkweoewnkenjwjewkjewoewkjewijewjk
POEPPSGNHwkejnkweoewnkenjwjewkjewoewkjewijewjk
hanumanthumanideeph6
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
Strings in c
Strings in cStrings in c
Strings in c
vampugani
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 string _updated_Somesh_SSTC_ Bhilai_CG
C string _updated_Somesh_SSTC_ Bhilai_CGC string _updated_Somesh_SSTC_ Bhilai_CG
C string _updated_Somesh_SSTC_ Bhilai_CG
drsomeshdewangan
String class and function for b.tech iii year students
String class and function  for b.tech iii year studentsString class and function  for b.tech iii year students
String class and function for b.tech iii year students
Somesh Kumar
PROBLEM SOLVING USING A PPSC- UNIT -3.pdf
PROBLEM SOLVING USING A PPSC- UNIT -3.pdfPROBLEM SOLVING USING A PPSC- UNIT -3.pdf
PROBLEM SOLVING USING A PPSC- UNIT -3.pdf
JNTUK KAKINADA
UNIT 4C-Strings.pptx for c language and basic knowledge
UNIT 4C-Strings.pptx for c language and basic knowledgeUNIT 4C-Strings.pptx for c language and basic knowledge
UNIT 4C-Strings.pptx for c language and basic knowledge
2024163103shubham
Strings in c++
Strings in c++Strings in c++
Strings in c++
International Islamic University
C q 3
C q 3C q 3
C q 3
Rahul Vishwakarma
Strings
StringsStrings
Strings
Imad Ali
Principals of Programming in CModule -5.pdfModule-4.pdf
Principals of Programming in CModule -5.pdfModule-4.pdfPrincipals of Programming in CModule -5.pdfModule-4.pdf
Principals of Programming in CModule -5.pdfModule-4.pdf
anilcsbs
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
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
POEPPSGNHwkejnkweoewnkenjwjewkjewoewkjewijewjk
POEPPSGNHwkejnkweoewnkenjwjewkjewoewkjewijewjkPOEPPSGNHwkejnkweoewnkenjwjewkjewoewkjewijewjk
POEPPSGNHwkejnkweoewnkenjwjewkjewoewkjewijewjk
hanumanthumanideeph6
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
Strings in c
Strings in cStrings in c
Strings in c
vampugani
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 string _updated_Somesh_SSTC_ Bhilai_CG
C string _updated_Somesh_SSTC_ Bhilai_CGC string _updated_Somesh_SSTC_ Bhilai_CG
C string _updated_Somesh_SSTC_ Bhilai_CG
drsomeshdewangan
String class and function for b.tech iii year students
String class and function  for b.tech iii year studentsString class and function  for b.tech iii year students
String class and function for b.tech iii year students
Somesh Kumar
PROBLEM SOLVING USING A PPSC- UNIT -3.pdf
PROBLEM SOLVING USING A PPSC- UNIT -3.pdfPROBLEM SOLVING USING A PPSC- UNIT -3.pdf
PROBLEM SOLVING USING A PPSC- UNIT -3.pdf
JNTUK KAKINADA
UNIT 4C-Strings.pptx for c language and basic knowledge
UNIT 4C-Strings.pptx for c language and basic knowledgeUNIT 4C-Strings.pptx for c language and basic knowledge
UNIT 4C-Strings.pptx for c language and basic knowledge
2024163103shubham

String Handling in c++

  • 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); }