Array String
Array String
Example
int arr[5];
Here,
int: It is the type of data to be stored in the array. We can also use other
data types such as char, float, and double.
arr: It is the name of the array.
5: It is the size of the array which means only 5 elements can be stored in
the array.
#include <iostream>
using namespace std;
int main()
{
int arr[3];
return 0;
}
Output
arr[0]: 10
arr[1]: 20
arr[2]: 30
// C++ Program to Illustrate How to Traverse an Array
#include <iostream>
using namespace std;
int main()
{
return 0;
}
Output
2 4 6 8 10 12 14 16 18 20
int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
// Length of an array
int n = sizeof(arr) / sizeof(arr[0]);
return 0;
}
Output
Size of arr[0]: 4
Size of arr: 20
Length of an array: 5
int main()
{
// Defining an array
int arr[] = { 1, 2, 3, 4 };
// Define a pointer
int* ptr = arr;
return 0;
}
Output
Memory address of arr: 0x7fff2f2cabb0
Memory address of arr: 0x7fff2f2cabb0
int main()
{
// Declaring 2D array
int arr[4][4];
return 0;
}
Output
0 1 2 3
1 2 3 4
2 3 4 5
3 4 5 6
int main()
{
// creating 2d and 3d array
int arr1[2][4];
int arr2[2][4][8];
return 0;
}
Output
Size of array arr1: 32 bytes
Size of array arr2: 256 bytes
int main()
{
int count = 1;
// Declaring 2D array
int array1[3][4];
return 0;
}
Output
1 2 3 4
5 6 7 8
9 10 11 12
C++ Program to Multiply Two Matrix Using Multi-
dimensional Arrays
#include <iostream>
using namespace std;
int main()
{
int a[10][10], b[10][10], mult[10][10], r1, c1, r2, c2, i, j, k;
cout << "Enter rows and columns for first matrix: ";
cin >> r1 >> c1;
cout << "Enter rows and columns for second matrix: ";
cin >> r2 >> c2;
cout << "Enter rows and columns for first matrix: ";
cin >> r1 >> c1;
cout << "Enter rows and columns for second matrix: ";
cin >> r2 >> c2;
}
return 0;
}
Output
Output Matrix:
24 29
6 25
int main()
{
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
cout << endl << "Enter elements of 1st matrix: " << endl;
return 0;
}
Output
Strings in C++
C++ strings are sequences of characters stored in a char array. Strings are
used to store words and text. They are also used to store data, such as
numbers and other types of information. Strings in C++ can be defined either
using the std::string class or the C-style character arrays.
std::string Class
These are the new types of strings that are introduced in C++ as std::string
class defined inside <string> header file. This provides many advantages
over conventional C-style strings such as dynamic size, member functions,
etc.
Syntax:
std::string str("GeeksforGeeks");
Example:
// C++ program to create std::string objects
#include <iostream>
using namespace std;
int main()
{
string str("GeeksforGeeks");
cout << str;
return 0;
}
Output
GeeksforGeeks
One more way we can make strings that have the same character repeating
again and again.
Syntax:
std::string str(number,character);
Example:
#include <iostream>
using namespace std;
int main()
{
string str(5, 'g');
cout << str;
return 0;
}
Output:
ggggg
int main()
{
string s = "GeeksforGeeks";
string str("GeeksforGeeks");
return 0;
}
Output
s = GeeksforGeeks
str = GeeksforGeeks
int main()
{
return 0;
}
Output
s1 = gfg
s2 = gfg
s3 = gfg
s4 = gfg
int main() {
string s;
cout<<"Enter String"<<endl;
cin>>s;
Output
Enter String
String is:
Output:
Enter String
GeeksforGeeks
String is: GeeksforGeeks
2. Using getline
The getline() function in C++ is used to read a string from an input stream. It
is declared in the <string> header file.
Syntax:
getline(cin,s);
Example:
// C++ Program to demonstrate use of getline function
#include <iostream>
using namespace std;
int main()
{
string s;
cout << "Enter String" << endl;
getline(cin, s);
cout << "String is: " << s << endl;
return 0;
}
Output
Enter String
String is:
Output:
Enter String
GeeksforGeeks
String is: GeeksforGeeks
3. Using stringstream
The stringstream class in C++ is used to take multiple strings as input at
once.
Syntax:
stringstream stringstream_object(string_name);
Example:
// C++ Program to demonstrate use of stringstream object
#include <iostream>
#include <sstream>
#include<string>
int main()
{
Output
GeeksforGeeks
to
the
Moon