-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ25.cpp
More file actions
57 lines (46 loc) · 1.29 KB
/
Q25.cpp
File metadata and controls
57 lines (46 loc) · 1.29 KB
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
// 25. Write a program to split a string into an array of words. Example: Input: inputString = "Hello world, welcome to JavaScript!";Output:'Hello', 'world', 'welcome', 'to', 'JavaScript'
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
int main() {
string inputString;
cout << "Enter a string: ";
getline(cin, inputString);
stringstream ss(inputString);
string word;
vector<string> words;
while (ss >> word) {
words.push_back(word);
}
cout << "Words in the string: ";
for (const string& w : words) {
cout << "'" << w << "' ";
}
cout << endl;
return 0;
}
#include <iostream>
#include <cstring>
using namespace std;
void splitManualArray(string str) {
int start = 0;
int length = str.length();
for (int i = 0; i < length; i++) {
if (str[i] == ' ' || ispunct(str[i])) {
if (i > start) {
cout << "'" << str.substr(start, i - start) << "', ";
}
start = i + 1;
}
}
if (start < length) {
cout << "'" << str.substr(start) << "'" << endl;
}
}
int main() {
string inputString = "Hello world, welcome to JavaScript!";
cout << "Words (Manual Array Split): ";
splitManualArray(inputString);
return 0;
}