2018年9月13日 星期四

itsa [C_ST125-易]字串切割

 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
#include<iostream>
#include<string>
#include<sstream>
using namespace std;

void replaceD(string &s)
{
 int i = 0;
 while (i < s.length())
 {
  if (s[i] == ':' || s[i] == ',' || s[i] == ';')
   s[i] = ' ';
  i++;
 }
}

int main()
{
 int N, x = 0;
 string s, str[100];
 cin >> N;
 getline(cin, s);
 for (int i = 0; i < N; i++)
 {
  x = 0;
  getline(cin, s);
  cout << "Original string: " << s << endl;
  replaceD(s);
  stringstream ss(s);
  while (ss >> str[x])
   x++;
  cout << "Tokens found:\n";
  for (int j = 0; j < x; j++)
   cout << str[j] << endl;
 }
 return 0;
}

沒有留言:

張貼留言