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++

String Handling in c++

  • 2.
    STRING HANDLING INC++ By: Muhammad Fahim Student of BSCS AJKU University
  • 3.
    CONTENTS:  String  Stringinputs  Array of string  String function
  • 4.
    STRING A collection ofcharacters written in double quotation is called string. it may consist of alphabatic characters, digits and special symbols.
  • 5.
    STRING DECLARATION C++ storesa 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 stringvariable 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() functionof cin object is used to input a single character. The syntax of this function is as follows: cin.get(ch);
  • 9.
    ARRAY OF STRINGS Anarray 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 OFSTRING: 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(): Thememcpy() is used to copy the number of specified character from first buffer to second buffer. Syntax: memcpy(buffer2,buffer1,size);
  • 12.
    STRCMP(): The strcmp() isused to compare two string character by character Syntax: strcmp(str1,str2);
  • 13.
    #include <iostream> #include <cstring> usingnamespace 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 strcpystand 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 strlenstands 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; intmain() { 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 usedto append a copy of one string to the end of another; Syntax: strcat(str1,str2);
  • 19.
    //Example concatenates twostrings 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); }