在C ++程序中解密字符串后找到第k个字符
在本教程中,我们将讨论解密字符串后查找第k个字符的程序。
为此,我们将提供一个由字符,数字和整数K组成的字符串。我们的任务是解密给定的字符串并在第K个位置找到该字符。
示例
#include <cstdlib>
#include <iostream>
using namespace std;
//正在查找解密的Kth字符
char findKthChar(string s, int k) {
int len = s.length();
int i = 0;
int total_len = 0;
while (i < len) {
if (isalpha(s[i])) {
total_len++;
if (total_len == k)
return s[i];
i++;
}
else {
int n = 0;
while (i < len && !isalpha(s[i])) {
n = n * 10 + (s[i] - '0');
i++;
}
int next_total_len = total_len * n;
if (k <= next_total_len) {
int pos = k % total_len;
if (!pos) {
pos = total_len;
}
return findKthChar(s, pos);
}
else {
total_len = next_total_len;
}
}
}
return -1;
}
int main() {
string s = "ab2c3";
int k = 5;
cout << findKthChar(s, k);
return 0;
}输出结果
c
热门推荐
4 图书馆简短祝福语
7 火把节祝福语简短
10 年底了祝福语简短的
11 天仙配简短祝福语
12 姐妹订婚刺绣祝福语简短
13 节日祝福语大全简短创意
14 小学同学简短祝福语
15 文艺乔迁祝福语简短精辟
16 朋友出国读书祝福语简短
17 花童结婚祝福语大全简短
18 婚礼红包祝福语简短霸气