-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7.strings cpp.cpp
68 lines (53 loc) · 2.03 KB
/
7.strings cpp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include<iostream>
// To use strings, you must include an additional header file in the source code, the <string> library:
#include<string>
using namespace std;
// strings in cpp
// Declaring the string
// Accessing the string
int main (){
//C++ Strings
// Strings are used for storing text.
// A string variable contains a collection of characters surrounded by double quotes:
// Declaring the string in the cpp
string greeting ="hello";
// print string
cout<<greeting;
// in string text are store in array like strucure
// so we can access the each part of string by its index example cout<<A[0]; prints output : A
/* 0 1 2 3 4
A k a s h
*/
string A = "Akash";
// her we print string A as 2nd nymber element
// hense it print small a since it available at 2nd place
cout<<A[2];
// for finding the lenght of the string
// we use nameofstring.size() or .length() function
int len = A.length();
cout<<len;
// or
cout<<A.length();
// getting input for string form the console
string first_name;
cin>>first_name;
cout<<"your name is :"<<first_name;
// the probleme is her that if we want print
// john king but it canoot get after the space
// so getting after the space we use getline() function
string fullName;
cout << "Type your full name: ";
getline (cin, fullName);
cout << "Your name is: " << fullName;
// changing the elements in string
//Declaring the string
string akash = "gite";
int leng = akash.size();
// changing the element at last place
akash[len-1] = 'o';
// print the string
cout<<akash;
//or we can directly print the string last element
cout<<akash[len-1];
return 0;
}