在C ++中从字符串中删除尾随零
在此程序中,我们将看到如何在C++中从字符串中删除结尾的零。有时某些字符串可能包含尾随零,例如“00023054”。执行该程序后,它将仅返回“23054”。初始零被删除。
Input: A string with trailing zeros “000023500124” Output: “23500124”
算法
Step 1: Get the string Step 2: Count number of trailing zeros n Step 3: Remove n characters from the beginning Step 4: return remaining string.
范例程式码
#include<iostream>
using namespace std;
main() {
string my_str = "000023500124";
int num = 0;
cout << "带尾随零的数字:" << my_str << endl;
//计算字符串中尾随零的数量
while(my_str[num] == '0') {
num++;
}
my_str.erase(0, num); //erase characters from 0 to i index
cout << "没有尾随零的数字:" << my_str;
}输出结果
带尾随零的数字:000023500124 没有尾随零的数字:23500124
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短