0% found this document useful (0 votes)
101 views20 pages

C++ String Initialization and Functions

The document discusses various methods for initializing and manipulating C++ string objects. It begins by showing examples of compile-time and run-time string initialization. It then covers 15 common string functions in C++ like length(), size(), capacity(), append(), assign(), at(), begin(), end(), erase(), clear(), compare(), swap(), max_size(), resize(), and insert(). For each function, it provides the syntax and a short code example to demonstrate how it works.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
101 views20 pages

C++ String Initialization and Functions

The document discusses various methods for initializing and manipulating C++ string objects. It begins by showing examples of compile-time and run-time string initialization. It then covers 15 common string functions in C++ like length(), size(), capacity(), append(), assign(), at(), begin(), end(), erase(), clear(), compare(), swap(), max_size(), resize(), and insert(). For each function, it provides the syntax and a short code example to demonstrate how it works.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

STRING IN C++

COMPILE TIME INITIALIZATION OF STRING

#include <iostream>
#include <string>
using namespace std;

int main ()
{
char ch[50]="Renuka";
cout << "Hello " << ch << ".\n";
getchar();
return 0;
}
RUN TIME INITIALIZATION OF STRING

PROGRAM 1

#include <iostream>
#include <string>
using namespace std;

int main ()
{
char name[50];
cout << "Enter your name : ";
cin>>name;
cout<<"Hello "<<name<<endl;
system("pause");
return 0;
}
PROGRAM 2

#include <iostream>
#include <string>
using namespace std;

int main()
{
string fullName;
cout << "Type your full name: ";
getline (cin, fullName);
cout << "Your name is: " << fullName<<endl;
system ("pause");
return 0;
}
PROGRAM 3

#include<iostream.h>
#include<conio.h>
#include<string.h>

void main()
{
char name[100];
clrscr();
cout<<"Father of C++ : ";
cin.get(name,30);
cout<<"C++ is invented by "<<name;
getch();
}
STRING FUNCTIONS

1. LENGTH()
2. SIZE()
3. CAPACITY()

Syntax
string.length()
string.size()
string.capacity()

PROGRAM

#include <iostream>
#include <string>
using namespace std;

int main()
{
string fullName;
cout<<"Type your full name: ";
getline (cin, fullName);
cout << "Your name is: " << fullName<<endl;
cout << "Using size()"<<endl<<"The length of
your name : " << fullName.size()<<endl;
cout << "Using length()"<<endl<<"The length
of your name : " << fullName.length()<<endl;
cout << "Using capacity()"<<endl<<"The length
of your name : " << fullName.capacity();
cout<<endl;
system ("pause");
return 0;
}
4. APPEND()

Syntax
string1.append(string2)

PROGRAM

#include <iostream>
#include <string>
using namespace std;

int main()
{
string fname,lname;
cout << "Type your first name: ";
getline (cin, fname);
cout << "Type your last name: ";
getline (cin, lname);
fname.append(lname);
cout << "Hello "<<fname;
cout<<endl;
system ("pause");
return 0;
}
5. ASSIGN()

Syntax
string1.assign(string2)
string1.assign(string2, position of the
character ,number of characters of string to
be copied)

PROGRAM

#include <iostream>
#include <string>
using namespace std;

int main()
{
string str = "C is a programming language";
cout<<"Original string : "<<str<<endl;
string str1;
str1.assign(str,7,20) ;
cout<<"Updated string : "<<str1<<endl;
cout<<endl;
system ("pause");
return 0;
}
6. AT()

Syntax
string1.at(string2)

PROGRAM

#include <iostream>
#include <string>
using namespace std;

int main()
{
string str = "Welcome";
cout<<"String is Welcome :"<<endl;
for(int i=0; i<str.length(); i++)
{
cout<<"index : "<<i<<"\t"<<"Characetr :
"<<str.at(i)<<endl;
}
cout<<endl;
system ("pause");
return 0;
}
7. BEGIN()

Syntax
iterator itvariable = string.begin();

8. END()

Syntax
iterator itvariable = string.end();

PROGRAM

#include <iostream>
#include <string>
using namespace std;

int main()
{
string str="Welcome To C++ Programming
Language";
cout<<"String :"<<str<<endl;

string::iterator first=str.begin();
cout<<"1st character of String :
"<<*first<<endl;
string::iterator last=str.end();
cout<<"Last character of Welcome : "<<*(last-
1)<<endl;
cout<<endl;
system ("pause");
return 0;
}
9. ERASE()

Syntax
string.erase(position,length);

10. CLEAR()

Syntax
string.clear();

PROGRAM

#include <iostream>
#include <string>
using namespace std;

int main()
{
string fullname;
int i;
cout<<"Type your full name: ";
getline (cin, fullname);
cout<<"Your name is "<<fullname<<endl;
cout<<"Erasing some characters from name :
"<<endl;
fullname.erase(4,3);
cout<<"Type your full name:
"<<fullname<<endl;
cout<<"Don't like your name.......Press 0 to
clear it : "<<endl;
cin>>i;
if(i==0)
fullname.clear();
cout<<"Your name is "<<fullname<<endl;
system ("pause");
return 0;
}
11. COMPARE()

Syntax :
int k= string1.compare(string2);

PROGRAM

#include <iostream>
#include <string>
using namespace std;
int main()
{
string str1,str2;
int k;
cout << "Enter first string : ";
cin>>str1;
cout << "Enter second string : ";
cin>>str2;
k= str1.compare(str2);
if(k==0)
{
cout<<endl<<"Both the strings are
equal"<<endl;
}
else
{
if(k>0)
{
cout<<"Both the strings are
unequal...."<<endl;
cout<<"First string is greater as compared to
second string"<<endl;
}
else
{
cout<<"Both the strings are
unequal...."<<endl;
cout<<"First string is smaller as compared to
second string"<<endl;
}
}
system ("pause");
return 0;
}
12. SWAP()

Syntax
string1.swap(string2);

PROGRAM

#include <iostream>
#include <string>
using namespace std;
int main()
{
string str1,str2;
int k;
cout<<"Enter first string : ";
cin>>str1;
cout<<"Enter second string : ";
cin>>str2;
cout<<"Before swap"<<endl;
cout<<"first string : "<<str1<<endl;
cout<<"second string : "<<str2<<endl;
str1.swap(str2);
cout<<"After swap"<<endl;
cout<<"first string : "<<str1<<endl;
cout<<"second string : "<<str2<<endl;
system("pause");
return 0;
}
13.MAX_SIZE()

Syntax
string.max_size()

PROGRAM

#include <iostream>
#include <string>
using namespace std;

int main()
{
string str1;
int k;
cout<<"Enter first string : ";
cin>>str1;
cout<<"Maximum size of the string is
:"<<str1.max_size()<<endl;
system("pause");
return 0;
}
14. RESIZE()

Syntax
string.resize(position of starting character)

PROGRAM

#include <iostream>
#include <string>
using namespace std;

int main()
{
string str="javatpoint";
cout<<"String is :"<<str<<endl;
str.resize(4);
cout<<"After resizing, string is"<<str<<endl;
cout<<endl;
system("pause");
return 0;
}
15. INSERT()

Syntax
string1.insert(position to insert,string2);

PROGRAM

#include <iostream>
#include <string>
using namespace std;

int main()
{
string str1= "C tutorial";
cout<<"String :" <<str1<<'\n';
cout<<"After insertion"<<endl;
cout<<"String :"<<str1.insert(1,"++");
cout<<endl;
system ("pause");
return 0;
}

You might also like