第一题:
#include <iostream> #include <string> using namespace std; bool IsVoChar(char c) { return (c == ‘a‘) || (c == ‘e‘) || (c == ‘o‘) || (c == ‘i‘) || (c == ‘u‘) || (c == ‘A‘) || (c == ‘E‘) || (c == ‘O‘) || (c == ‘I‘) || (c == ‘U‘); } string reverseVoChar(string s) { int slen = s.length(); for (int l = 0, r = slen - 1; l < r; ++l) { if (IsVoChar(s[l])) { while ((l < r) && !IsVoChar(s[r])) --r; swap(s[l], s[r--]); } } return s; } int main() { string str1, str2; cin >> str1; str2 = reverseVoChar(str1); cout << str2 << endl; system("pause"); return 0; }
第二题:
第三题:
#include <iostream> #include <sstream> using namespace std; string DelChar(string str) //从字符串中提取出数字,剔除掉字母 { for (int i = 0; i < str.length();) { int temp = (int)str[i]; if (temp >= 48 && temp <= 57) { i++; continue; } else { str.erase(i, 1); continue; } } return str; } string DecToHex(string str) //将字符串转化为十六进制 { int i = 0; string result = ""; i++; int n = 0; for (int i = str.length() - 1, j = 1; i >= 0; i--) { n += (str[i] - ‘0‘) * j; j *= 10; } char _16[] = { ‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘, ‘A‘, ‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘ }; const int radix = 16; while (n) { int i = n % radix; result = _16[i] + result; n /= radix; } return result; } int main() { string str1, str2; cin >> str1; str1 = DelChar(str1); //cout << str1 << endl; str2 = DecToHex(str1); cout << str2 << endl; system("pause"); return 0; }
原文地址:https://www.cnblogs.com/si-lei/p/9601841.html
时间: 2024-11-08 12:28:09