-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClass713.cpp
executable file
·133 lines (86 loc) · 2.22 KB
/
Class713.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//============================================================================
// Name : Class713.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <math.h>
#include <string>
#include <iostream>
using namespace std;
#define PI 3.14159265
void sample1( ) {
double degree, result;
degree = 60.0;
result = cos (degree * PI/180);
cout << result << endl;
return;
}
void string1( ) {
string str;
int n;
n = 10;
str = "Hello";
string space = "";
for (int i = 0; i < 10; i++) {
space += " ";
cout << space << str << endl;
// cout << str << " " << i << endl;
}
return;
}
void string2 ( ) {
string first, last;
first = "John";
last = "Smith";
string fullname1 = first + " " + last; //John Smith
string fullname2 = last + ", " + first; //Smith, John
cout << fullname1 << endl << fullname2 << endl;
string fullname3 = "";
// fullname3 = first[0];
// fullname3 = fullname3 + ". " + last;
fullname3.append(1, first[0]); //change 1 to 3 and see what happens
fullname3.append(". " + last);
//or
//fullname3 = fullname3 + ". " + last;
cout << fullname3 << endl;
/*
Note:
We cannot the string concatenation operation as
string s = first[0] + ". " + last;
because of data type mismatch. Expression first[0] is char, not string
*/
return;
}
void string3( ) {
cout << endl << endl;
string str = "This is a test";
cout << "'" << str << "' has " << str.size() << " characters." << endl;
cout << str.substr(0,4) << endl;
cout << "First blank space in the string at position " << str.find(" ") << endl;
cout << str << endl;
return;
}
void string4( ) {
//find the number of blank spaces in a given string
string str = "Hello world how are you .";
string searchStr = " ";
int cnt = 0;
int loc = str.find(searchStr);
while (loc >= 0) { /*not found*/
cnt++;
str = str.substr(loc+1,str.size( ));
loc = str.find(searchStr);
}
cout << cnt << endl;
return;
}
int main() {
//sample1( );
//string1( );
//string2( );
//string3( );
string4();
return 0;
}