-
Notifications
You must be signed in to change notification settings - Fork 1
/
String.cpp
76 lines (59 loc) · 2.18 KB
/
String.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
69
70
71
72
73
74
75
76
// author: jaydattpatel
#include <iostream>
#include <cstring>
using namespace std;
int main ()
{
int a=2022,diff,len;
char char0[100];
char char1[50] = "HeLlo";
char char2[] = "WoRld";
char char3[] = {'h', 'e', 'l', 'l', 'o', '\0'};
char char4[6] = {'h', 'e', 'l', 'l', 'o', '\0'};
string str1 = "String-1";
string str2("String-2");
string str3,str4;
// sprintf(string formation will be saved into char0 )
sprintf(char0,"\nsprintf(string,..): %s %s %s %d ",char1,"Welcome to ",char2,a);// sprintf to make string format
cout<<char0<<endl;
// strcat only character array allowed
strcat(char1, char2); //joint the both array and store in first one
cout<<"\nstrcat(char1, char2) : "<<char1<<endl;
str1.append(str2);
cout << "\nstr1.append(str2) : "<<str1;
cout << "\nstr1.length() : "<< str1.length();
cout << "\nstr1.size() : "<< str1.size();
cout << "\nstr1.capacity() : "<< str1.capacity();
cout << "\nstr1.max_size() : "<< str1.max_size();
str1.resize(8);
cout << "\nstr1.resize(8) : "<<str1;
str1.shrink_to_fit();
cout << "\nstr1.shrink_to_fit()";
cout << "\nstr1.size() : "<< str1.size();
cout << "\nstr1.capacity() : "<< str1.capacity();
cout << "\nstr1.max_size() : "<< str1.max_size();
str3 = str1 + str2;
cout << "\n\nstr3 = str1 + str2 : "<<str3;
if(str1 == str2)
cout<<"\n(str1 == str2) : "<<(str1 == str2)<<" are same : " <<str1<<" , "<<str2;
else
cout<<"\n(str1 == str2) : "<<(str1 == str2)<< "are not same : "<<str1<<" , "<<str2;
for(int i = 0; i<8;i++)
str3.pop_back();
cout << "\nstr3.pop_back() : "<<str3;
for(int i = 0; i<str2.length();i++)
str3.push_back(str2[i]);
cout << "\nstr3.push_back(str2[i]) : "<<str3;
str3.copy(char4, 6, 0); //string to character array
cout << "\nstr3.copy(char4, 6, 0) : "<<char4;
str1.swap(str2);
cout << "\nstr1.swap(str2) :";
cout << "\nstr1: "<<str1<< "\nstr2: "<<str2;
cout << "\n\nEnter the string cin.get(char0, 50) : ";
cin.get(char0, 100);
cout<<"\n"<<char0<<endl;
cout << "\nEnter the string (press $ to terminate) : ";
cin.getline(char0,100,'$');
cout<<"\n"<<char0<<endl;
return 0;
}