-
Notifications
You must be signed in to change notification settings - Fork 0
/
Remove_leading0.cpp
66 lines (57 loc) · 1.36 KB
/
Remove_leading0.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// C++ program to remove leading zeros using recursion
// from a given string
#include <iostream>
#include<string>
using namespace std;
string removeZero(string str)
{
// Count leading zeros
if(str.empty())
return str;
if (str[0] == '0') //if first index is 0 then perform removingzero func on the rest of the string
return removeZero(str.substr(1));
// If the first character is not '0', return the string as it is
return str;
return str;
}
// Driver code
int main()
{
string str;
cout << "enter the number: " << endl;
getline(cin,str);
str = removeZero(str);
cout << "Number after removing leading zeroes: " << str << endl;
return 0;
}
// if simply using itertaion can use s.erase() function to remove like s.erase(0,i) means remove 0 till the end of string
// C++ program to remove leading zeros
// from a given string
//#include <iostream>
//
//using namespace std;
//
//string removeZero(string str)
//{
// // Count leading 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;
//}
//