在C++中删除字符串中的尾随零点

给定一串数字,从中删除尾随的零。

null

例如:

Input : 00000123569
Output : 123569

Input : 000012356090
Output : 12356090

1) 计算尾随的零。 2) 使用 字符串擦除功能 删除大于计数的字符。

下面是C++实现。

// C++ program to remove trailing/preceding zeros
// from a given string
#include<iostream>
using namespace std;
string removeZero(string str)
{
// Count trailing zeros
int i = 0;
while (str[i] == '0' )
i++;
// The erase function removes i characters
// from given index (0 here)
str.erase(0, i);
return str;
}
// Driver code
int main()
{
string str;
str = "00000123569" ;
str = removeZero(str);
cout << str << endl;
return 0;
}


输出:

123569

本文由 普拉哈尔·阿格拉瓦尔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献。极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享