0% found this document useful (0 votes)
143 views

Strings: - String Is A Collection of Characters. - 2 Types

Strings can be represented as arrays of characters (C-strings) or as objects of the string class. There are different ways to input, output, manipulate and compare strings in C++. Common string operations include concatenation, copying, finding length, reversing order, and checking for equality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
143 views

Strings: - String Is A Collection of Characters. - 2 Types

Strings can be represented as arrays of characters (C-strings) or as objects of the string class. There are different ways to input, output, manipulate and compare strings in C++. Common string operations include concatenation, copying, finding length, reversing order, and checking for equality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Strings

• String is a collection of characters.


• 2 types
– Strings that are objects of string class (The
Standard C++ Library string class)
– C-strings (C-style Strings)
C-strings
• arrays of type char terminated with null
character
char str[] = "C++";
– str is a string and it holds 4 characters.(3 character,
the null character \0)
Same as,
– char str[4] = "C++";
– char str[] = {'C','+','+','\0'};
– char str[4] = {'C','+','+','\0'};
Reading a word
#include <iostream>
using namespace std;
int main()
{
char str[100];
cout << "Enter a string: ";
cin >> str;
cout << "You entered: " << str << endl;
cout << "\nEnter another string: ";
cin >> str;
cout << "You entered: "<<str<<endl;
return 0;
}
Reading embedded blanks
C++ String to read a line of text

#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;
}

You might also like