Strings: - String Is A Collection of Characters. - 2 Types
Strings: - String Is A Collection of Characters. - 2 Types
#include <iostream>
using namespace std;
int main()
{
char str[100];
cout << "Enter a string: ";
cin.get(str, 100);
cout << "You entered: " << str << endl;
return 0;
}
objects of string class
string Object
#include <iostream>
using namespace std;
int main()
{
string str;
cout << "Enter a string: ";
getline(cin, str);
cout << "You entered: " << str << endl;
return 0;
}
Reading multiple lines
#include <iostream>
using namespace std;
Const int MAX=2000;
Char str[MAX];
int main()
{
Cout<<“Enter a string”;
Cin.get(str, MAX, ’$’);
Cout<<“You entered”<<str;
return 0;
}
output
• Enter a string
Aaaaaaaaaa
Bbbbbbbbbb
Cccccccccccc
$
You entered
Aaaaaaaaaa
Bbbbbbbbbb
Cccccccccccc
Copy String Object
#include <iostream>
using namespace std;
int main()
{
string s1, s2;
cout << "Enter string s1: ";
getline (cin, s1);
s2 = s1;
cout << "s1 = "<< s1 << endl;
cout << "s2 = "<< s2;
return 0;
}
output
Enter string s1: C++ Strings
s1 = C++ Strings
s2 = C++ Strings
Copy C-Strings
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char s1[100], s2[100];
cout << "Enter string s1: ";
cin.getline(s1, 100);
strcpy(s2, s1);
cout << "s1 = "<< s1 << endl;
cout << "s2 = "<< s2;
return 0;
}
output
Enter string s1: C-Strings
s1 = C-Strings
s2 = C-Strings
Arrays of strings
#include <iostream>
using namespace std;
int main()
{
char colour[4][10] = { "Blue", "Red", "Orange", "Yellow" };
for (int i = 0; i < 4; i++)
cout << colour[i] << "\n";
return 0;
}
Output
Output:
Blue
Red
Orange
Yellow
Drawbacks:
• Both the number of Strings and Size of String are fixed.
• A 2D array is allocated, whose second dimension is equal to
maximum sized string which causes wastage of space.
Instead of previous --- to save space since the
size is fixed
int main()
{
string colour[4] = { "Blue", "Red",
"Orange", "Yellow" };
for (int i = 0; i < 4; i++)
cout << colour[i] << "\n";
}
concatenation
• string firstName = "John ";
string lastName = "Doe";
string fullName = firstName + lastName;
cout << fullName;
• USE strcat()
String compare
#include<iostream.h>
#include<string.h>
void main()
{
char str1[100], str2[100];
cout<<"Enter first string : ";
gets(str1);
cout<<"Enter second string : ";
gets(str2);
if(strcmp(str1, str2)==0)
{
cout<<"Both the strings are equal";
}
else
{
cout<<"Both the strings are not equal";
}
}
Find Frequency of Characters of a String Object
#include <iostream>
using namespace std;
int main()
{
string str = "C++ Programming is awesome";
char checkCharacter = 'a';
int count = 0;
for (int i = 0; i < str.size(); i++)
{
if (str[i] == checkCharacter)
{
++ count;
}
}
cout << "Number of " << checkCharacter << " = " << count;
return 0;
}
Using strlen
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char str[] = "C++ Programming is awesome";
// you can also use str.length()
cout << "String Length = " << strlen(str);
return 0;
}
Output: String Length = 26
Reverse a string
#include<iostream>
#include<string.h>
using namespace std;
int main ()
{
char str[50], temp;
int i, j;
cout << "Enter a string : ";
gets(str);
j = strlen(str) - 1;
for (i = 0; i < j; i++,j--)
{
temp = str[i];
str[i] = str[j];
str[j] = temp;
}
cout << "\nstring : " << str;
return 0;
}