Skip to content

Commit 8be6cd2

Browse files
authored
Merge pull request #16 from Manish4Kumar/patch-1
string problems
2 parents bc32003 + e231cde commit 8be6cd2

File tree

1 file changed

+138
-0
lines changed

1 file changed

+138
-0
lines changed

Diff for: DataStructure/c++/string/README.md

+138
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
C++ provides following two types of string representations −
2+
3+
The C-style character string.
4+
The string class type introduced with Standard C++.
5+
The C-Style Character String
6+
The C-style character string originated within the C language and continues to be supported within C++. This string is actually a one-dimensional array of characters which is terminated by a null character '\0'. Thus a null-terminated string contains the characters that comprise the string followed by a null.
7+
8+
The following declaration and initialization create a string consisting of the word "Hello". To hold the null character at the end of the array, the size of the character array containing the string is one more than the number of characters in the word "Hello."
9+
10+
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
11+
If you follow the rule of array initialization, then you can write the above statement as follows −
12+
13+
char greeting[] = "Hello";
14+
Following is the memory presentation of above defined string in C/C++ −
15+
16+
String Presentation in C/C++
17+
Actually, you do not place the null character at the end of a string constant. The C++ compiler automatically places the '\0' at the end of the string when it initializes the array. Let us try to print above-mentioned string −
18+
19+
Live Demo
20+
#include <iostream>
21+
22+
using namespace std;
23+
24+
int main () {
25+
26+
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
27+
28+
cout << "Greeting message: ";
29+
cout << greeting << endl;
30+
31+
return 0;
32+
}
33+
When the above code is compiled and executed, it produces the following result −
34+
35+
Greeting message: Hello
36+
C++ supports a wide range of functions that manipulate null-terminated strings −
37+
38+
Sr.No Function & Purpose
39+
1
40+
strcpy(s1, s2);
41+
42+
Copies string s2 into string s1.
43+
44+
2
45+
strcat(s1, s2);
46+
47+
Concatenates string s2 onto the end of string s1.
48+
49+
3
50+
strlen(s1);
51+
52+
Returns the length of string s1.
53+
54+
4
55+
strcmp(s1, s2);
56+
57+
Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2.
58+
59+
5
60+
strchr(s1, ch);
61+
62+
Returns a pointer to the first occurrence of character ch in string s1.
63+
64+
6
65+
strstr(s1, s2);
66+
67+
Returns a pointer to the first occurrence of string s2 in string s1.
68+
69+
Following example makes use of few of the above-mentioned functions −
70+
71+
Live Demo
72+
#include <iostream>
73+
#include <cstring>
74+
75+
using namespace std;
76+
77+
int main () {
78+
79+
char str1[10] = "Hello";
80+
char str2[10] = "World";
81+
char str3[10];
82+
int len ;
83+
84+
// copy str1 into str3
85+
strcpy( str3, str1);
86+
cout << "strcpy( str3, str1) : " << str3 << endl;
87+
88+
// concatenates str1 and str2
89+
strcat( str1, str2);
90+
cout << "strcat( str1, str2): " << str1 << endl;
91+
92+
// total lenghth of str1 after concatenation
93+
len = strlen(str1);
94+
cout << "strlen(str1) : " << len << endl;
95+
96+
return 0;
97+
}
98+
When the above code is compiled and executed, it produces result something as follows −
99+
100+
strcpy( str3, str1) : Hello
101+
strcat( str1, str2): HelloWorld
102+
strlen(str1) : 10
103+
The String Class in C++
104+
The standard C++ library provides a string class type that supports all the operations mentioned above, additionally much more functionality. Let us check the following example −
105+
106+
Live Demo
107+
#include <iostream>
108+
#include <string>
109+
110+
using namespace std;
111+
112+
int main () {
113+
114+
string str1 = "Hello";
115+
string str2 = "World";
116+
string str3;
117+
int len ;
118+
119+
// copy str1 into str3
120+
str3 = str1;
121+
cout << "str3 : " << str3 << endl;
122+
123+
// concatenates str1 and str2
124+
str3 = str1 + str2;
125+
cout << "str1 + str2 : " << str3 << endl;
126+
127+
// total length of str3 after concatenation
128+
len = str3.size();
129+
cout << "str3.size() : " << len << endl;
130+
131+
return 0;
132+
}
133+
When the above code is compiled and executed, it produces result something as follows −
134+
135+
str3 : Hello
136+
str1 + str2 : HelloWorld
137+
str3.size() : 10
138+

0 commit comments

Comments
 (0)