在C程序中,字符串的字母数字缩写是什么?

在c程序中,字符串的字母数字缩写是什么?

在这里,我们将看到一个与给定字符串字母数字缩写相关的有趣问题。字符串长度小于10。我们将打印出所有的字母数字缩写。

字母数字缩写是由字符和数字混合形成的。该数字的值是被省略的字符数。可能有任意数量的被省略的子字符串。没有两个子字符串是相邻的。让我们看一下获取这个概念的算法。

算法

printAbbreviation(s, index, max, str) −

begin   if index is same as max, then      print str   end if   add s[index] at the last of str   printAbbreviation(s, index + 1, max, str)   delete last character from str   count := 1   if str is not empty, then      if the last character of str is a digit, then         add last digit with the count value         delete last character from str      end if   end if   add count after the str   printAbbreveation(s, index + 1, max, str)end

示例

#include using namespace std;void printAbbreviation(const string& s, int index, int max_index, string str) {   if (index == max_index) { //if string has ended      cout << str << endl;      return;   }   str.push_back(s[index]); // push the current character to result   printAbbreviation(s, index + 1, max_index, str); //print from next index   str.pop_back(); //remove last character   int count = 1;   if (!str.empty()) {      if (isdigit(str.back())) { //if the last one is digit, then         count += (int)(str.back() - '0'); //count the integer value of that digit         str.pop_back(); //remove last character      }   }   char to_char = (char)(count + '0'); //make count to character   str.push_back(to_char);   printAbbreviation(s, index + 1, max_index, str); //do for next index}void printCombination(string str) {   if (!str.length()) //if the string is empty      return;   string str_res;   printAbbreviation(str, 0, str.length(), str_res);}int main() {   string str = "HELLO";   printCombination(str);}

输出

HELLOHELL1HEL1OHEL2HE1LOHE1L1HE2OHE3H1LLOH1LL1H1L1OH1L2H2LOH2L1H3OH41ELLO1ELL11EL1O1EL21E1LO1E1L11E2O1E32LLO2LL12L1O2L23LO3L14O5

以上就是在C程序中,字符串的字母数字缩写是什么?的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1445204.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年12月17日 22:17:44
下一篇 2025年12月17日 22:17:57

相关推荐

发表回复

登录后才能评论
关注微信