给定一组元素个数不定的字符串数组,每个字符串的长度不定;请统计出该字符串数组中的英文字母子串、数字子串和其他字符子串的总数; 输出为以","符号分隔3个数值,分别代表英文字母子串、数字子串和其他字符子串的数量; 实现时无需考虑非法输入。
:
输入为:
字符串数组
例子:abcd1244!! BKMKLK0987%
:
输出为以","符号分隔3个数值,分别代表英文字母子串、数字子串和其他字符子串的数量。
输入例子:
abcd1244!! BKMKLK0987%
输出例子:
输入一个字符串,对其进行逐词的反转后,然后返回。 比如:
输入:"the sky is blue"
输出:"blue is sky the"
注意: 1)“词”是指:任何不含空格的连续字符串
2)输入字符串可以有首尾空格,但是返回的字符串不能有首尾空格
3)输入字符串中两个词之间可以有多个空格,但是返回的字符串中词只能用单个空格隔开
输入描述:
函数原型:void ReverseWords(char *s);
输入与输出均为s。
输出描述:
输入与输出均为s。
输入例子:
the sky is blue
输出例子:
blue is sky the
形如 (OP P1 P2 ...),括号内元素由单个空格分割。
其中第一个元素OP为操作符,后续元素均为其参数,参数个数取决于操作符类型
注意:参数 P1, P2 也有可能是另外一个嵌套的 (OP P1 P2 ...)
当前OP类型为 quote / reverse / search / combine 字符串相关的操作:
- quote: 引用一个字符串,即返回字符串本身内容
参数个数 1
- reverse: 把字符串反转,并返回
参数个数 1
- search: 在第一个字符串中查找第二个字符串的第一次出现,返回从这开始到结束的所有字符串
如果查找不到,返回空字符串
参数个数 2
- combine: 把所有字符串组合起来
参数个数不定,但至少 1 个
其中P1, P2 等参数可能是带双引号的字符串,如 "abc",也有可能是另外一个 (OP P1 P2 ...)
上述字符串包括引号;引号中间的所有字符,均为 ASCII 可打印字符,且不会再出现引号 (")
输出也为带双引号的字符串
举例:
输入字符串 输出结果
(quote "!@#$%") "!@#$%"
(reverse "a b c") "c b a"
(search "abcdef" "cd" ) "cdef"
(search "abcdef" "xy" ) ""
(combine "a" "b" "cde) ") "abcde) "
(search (combine "1234567890" "abcdefgh" "1234567890") (reverse "dc")) cdefgh123456789
输入描述:
合法C字符串,字符串长度不超过512;用例保证了无语法错误.
输出描述:
合法C字符串,需要带括号
输入例子:
(search "huawei" "we")
输出例子:
"wei"
:
输入为:
字符串数组
例子:abcd1244!! BKMKLK0987%
:
输出为以","符号分隔3个数值,分别代表英文字母子串、数字子串和其他字符子串的数量。
输入例子:
abcd1244!! BKMKLK0987%
输出例子:
2,2,2
#include<iostream>
#include<string>
using namespace std;
int type(char ch)
{
if (ch >= '0'&&ch <= '9')return 1;
else if ((ch >= 'a'&&ch <= 'z') || (ch >= 'A'&&ch <= 'Z'))return 0;
else return -1;
}
int main()
{
string s;
while (getline(cin,s))
{
cout << s << endl;
int a = 0, b = 0, c = 0;
int i = 0;
while (i < s.length())
{
if (type(s[i]) == 1)
{
while (i < s.length() && type(s[i]) == 1)i++;
a++;
continue;
}
else if (type(s[i]) == 0)
{
while (i < s.length()&&type(s[i]) == 0)i++;
b++;
continue;
}
else
{
while (i < s.length()&&type(s[i]) == -1)i++;
c++;
continue;
}
}
cout << a << " " << b << " " << c << endl;
}
}
输入一个字符串,对其进行逐词的反转后,然后返回。 比如:
输入:"the sky is blue"
输出:"blue is sky the"
注意: 1)“词”是指:任何不含空格的连续字符串
2)输入字符串可以有首尾空格,但是返回的字符串不能有首尾空格
3)输入字符串中两个词之间可以有多个空格,但是返回的字符串中词只能用单个空格隔开
输入描述:
函数原型:void ReverseWords(char *s);
输入与输出均为s。
输出描述:
输入与输出均为s。
输入例子:
the sky is blue
输出例子:
blue is sky the
#include<iostream>
#include<string>
#include<vector>
using namespace std;
void split(string &s)
{
vector<string>vs;
string str;
int len = s.length();
for (int i = 0; i < len; )
{
if (s[i] == ' ')
{
int j = i;
while (j<len && s[j] == ' ')j++;
i = j;
continue;
}
else
{
int j = i;
while (j<len && s[j] != ' ')j++;
str = s.substr(i, j - i);
//cout << str << endl;
vs.push_back(str);
i = j;
continue;
}
}
for (int i = vs.size() - 1; i >= 1; i--)cout << vs[i] << " ";
cout << vs[0] << endl;
}
int main()
{
string s;
while (getline(cin, s))
{
split(s);
}
}
LISP语言唯一的语法就是括号要配对。
形如 (OP P1 P2 ...),括号内元素由单个空格分割。
其中第一个元素OP为操作符,后续元素均为其参数,参数个数取决于操作符类型
注意:参数 P1, P2 也有可能是另外一个嵌套的 (OP P1 P2 ...)
当前OP类型为 quote / reverse / search / combine 字符串相关的操作:
- quote: 引用一个字符串,即返回字符串本身内容
参数个数 1
- reverse: 把字符串反转,并返回
参数个数 1
- search: 在第一个字符串中查找第二个字符串的第一次出现,返回从这开始到结束的所有字符串
如果查找不到,返回空字符串
参数个数 2
- combine: 把所有字符串组合起来
参数个数不定,但至少 1 个
其中P1, P2 等参数可能是带双引号的字符串,如 "abc",也有可能是另外一个 (OP P1 P2 ...)
上述字符串包括引号;引号中间的所有字符,均为 ASCII 可打印字符,且不会再出现引号 (")
输出也为带双引号的字符串
举例:
输入字符串 输出结果
(quote "!@#$%") "!@#$%"
(reverse "a b c") "c b a"
(search "abcdef" "cd" ) "cdef"
(search "abcdef" "xy" ) ""
(combine "a" "b" "cde) ") "abcde) "
(search (combine "1234567890" "abcdefgh" "1234567890") (reverse "dc")) cdefgh123456789
输入描述:
合法C字符串,字符串长度不超过512;用例保证了无语法错误.
输出描述:
合法C字符串,需要带括号
输入例子:
(search "huawei" "we")
输出例子:
"wei"
#include<iostream>
#include<string>
#include<vector>
using namespace std;
const string operat[4] = { "quote", "reverse", "search", "combine" };
void split(string &s, vector<string>&vs)
{
vs.clear();
int len = s.length();
//cout << s << endl;
vector<string>v;
for (int i = 1; i < len - 1; )
{
if (s[i] == ' ')
{
i++;
}
else if (s[i] == '"')
{
int j = i + 1;
// cout << "j=" << j << endl;
while (j < len - 1 && s[j] != '"')
{
j++;
// cout << "j=" << j << endl;
}
string str = s.substr(i + 1, j - i - 1);
//cout << str << endl;
v.push_back(str);
i = j;
i++;
}
else if (s[i] == '(')
{
int count = 0;
int j = i;
while (j < len - 1)
{
if (s[j] == char(34))count++;
if (s[j] == ')'&&count % 2 == 0)break;
j++;
//cout << "j=" << j << " count=" << count << endl;
}
string str = s.substr(i, j - i + 1);
//cout << str << endl;
v.push_back(str);
i = j + 1;
}
else
{
if (i == 1)
{
while (s[i] != ' ')i++;
string str = s.substr(1, i - 1);
//cout << str << endl;
v.push_back(str);
}
}
}
vs = v;
}
string operation(string &opt, vector<string>&vs)
{
if (opt == operat[0])return vs[0];
else if (opt == operat[1])
{
reverse(vs[0].begin(), vs[0].end());
return vs[0];
}
else if (opt == operat[2])
{
int n = vs[0].find(vs[1]);
if (n == string::npos)return "";
else
{
//cout << "n=" << n << endl;
string str = vs[0].substr(n);
return str;
}
}
else
{
string str;
for (int i = 0; i < vs.size(); i++)str += vs[i];
return str;
}
}
string solve(string s)
{
int len = s.length();
int i = 0;
bool flag = true;
vector<string>vs;
split(s, vs);
vector<string>rs;
for (int i = 1; i < vs.size(); i++)
{
//cout << vs[i] << endl;
if (vs[i][0] == '(')
{
string str = solve(vs[i]);
rs.push_back(str);
}
else
{
rs.push_back(vs[i]);
}
}
//cout << vs[0] << endl;
//for (int i = 0; i < rs.size(); i++)cout << rs[i] << " ";
//cout << endl;
string str = operation(vs[0], rs);
cout <<'"'<< str <<'"'<< endl;
return str;
}
int main()
{
string s;
while (getline(cin,s))
{
solve(s);
}
}