-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIndividual7anan2.cpp
More file actions
66 lines (50 loc) · 1.82 KB
/
Individual7anan2.cpp
File metadata and controls
66 lines (50 loc) · 1.82 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
58
59
60
61
62
63
64
65
66
/*
Author: Hanan Mohamed Abdelrahim
ID:20170375
This program gets a string using a vector function and split the target with the given delimiter.
For example: the input split("10,20,30,40" , ",") should output "10" , "20" , "30" , "40". */
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<string> split(string target, string delimeter); /// vector function takes two strings and split the target
int main()
{
vector<string> temp; ///vector to print the splitted target
string target;
string delim;
cout << "Enter string : ";
getline(cin, target);
cout << "Enter delimiter : ";
getline(cin, delim);
temp = split(target, delim);
///----------------------------------------------------------
cout << "\nThe string after Splitting : \n\n[ ";
for (int i = 0; i < temp.size(); i++)
{
cout << "\""<<temp[i]<<"\"";
if( i != temp.size()-1)
cout << " , ";
}
cout << " ]\n\n";
///----------------------------------------------------
}
vector<string> split(string target, string delimeter)
{
string temp; ///a temporary string to hold each letter from the vector
vector<string> splitTarget; ///an empty vector to hold the splitted target
for (int i = 0; i<target.size(); i++)
{
if (target[i] != delimeter[0])
{
temp+=target[i]; ///temp string will contain each letter from target before the delimiter
}
else if(target[i] == delimeter[0])
{
splitTarget.push_back(temp); /// when the delimiter is found, the word will be saved in the empty vector splitTarget
temp=""; ///empty the string to hold the new word
}
}
splitTarget.push_back(temp);
return splitTarget ;
}