Delete a specific number from beginning of string of numbers

In summary: Gimme a string of digits: "; cin >> digits; int pos_first_nonzero = digits.find_first_not_of("0"); if (pos_first_nonzero == string::npos) // string is all zeroes { digits = "0"; } else if (pos_first_nonzero != 0) { digits = digits.erase(0, pos_first_nonzero); }
  • #1
sweetjones
44
0
I am creating a calculator and have 4 textboxes. I need to know what functions I need to delete the number '0' from a string of numbers such as, "0852" so it can look like "852." My goal is for the program to check the user input after the user has pressed the 'enter' button to go to the next textbox. I know there are functions that can check each character in a string and search for a specific character. So I just need to know the functions. I'm sure I can implement it in my program correctly. thanks in advance!

P.S. This is for C++ MFC VS2005.
 
Last edited:
Technology news on Phys.org
  • #2
Im not farmiliar with c++, but Id do something like this:


Code:
string number;

function checkFirstDigit(sting number) {
  if (first character of number is a 0) {
    number = (number with first character removed); 
    return checkFirstDigit(number);
  }
  else {
    return number;
}

again, i don't know c++ so I don't know the syndax for comparing strings/modifying strings, so this is more simple pseudo code.
 
  • #3
swraman said:
Im not farmiliar with c++, but Id do something like this:


Code:
string number;

function checkFirstDigit(sting number) {
  if (first character of number is a 0) {
    number = (number with first character removed); 
    return checkFirstDigit(number);
  }
  else {
    return number;
}

again, i don't know c++ so I don't know the syndax for comparing strings/modifying strings, so this is more simple pseudo code.

Thanks for the pseudo Code, but that's about all I have done so far. I can't seem to figure this out. I am sure I would need something like a 'for loop' for an array, but what function is able to delete a specific digit in a specific position?

PLEASE SOMEONE HELP!
 
  • #4
For those of you who has the same problem I had, here is the solution I came up with:

#include "stdafx.h"
#include <iostream>
#include "conio.h"

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
char num[9];

cin >> num;

if(num[0] == '0')
{
for(int i = 0; i<9; i++)
{
num = num[i+1];
}
}

cout << num << endl;

getch();
return 0;
}


Now all I have to do is implement this into my GUI's edit boxes.
 
  • #5
sweetjones said:
Code:
		for(int i = 0; i<9; i++)
		{
			num[i] = num[i+1];
		}
When i=8, you are setting num[8] = num[9]. However, num[9] is outside the bounds of the array--it could be filled with any garbage data. This can lead to strange bugs in your program.
 
  • #6
Here's a solution that uses the standard C++ 'string' type, which has member functions for searching and slicing and dicing strings:

Code:
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string digits;
    cout << "Gimme a string of digits: ";
    cin >> digits;
    int pos_first_nonzero = digits.find_first_not_of("0");
    if (pos_first_nonzero == string::npos)  // string is all zeroes
    {
        digits = "0";
    }
    else if (pos_first_nonzero != 0)
    {
        digits = digits.erase(0, pos_first_nonzero);
    }
    // If pos_first_nonzero == 0, 
    // there are no leading zeroes and we don't do anything.
    cout << digits << endl;
    return 0;
}

Actually, we can simplify this a bit if we don't mind the program going through the motions of "erasing" zero characters when there are no leading zeroes:

Code:
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string digits;
    cout << "Gimme a string of digits: ";
    cin >> digits;
    int pos_first_nonzero = digits.find_first_not_of("0");
    if (pos_first_nonzero == string::npos)  // string is all zeroes
    {
        digits = "0";
    }
    else
    {
        digits = digits.erase(0, pos_first_nonzero);
    }
    cout << digits << endl;
    return 0;
}
 
Last edited:
  • #7
mXSCNT said:
When i=8, you are setting num[8] = num[9]. However, num[9] is outside the bounds of the array--it could be filled with any garbage data. This can lead to strange bugs in your program.

Yeah i noticed that, but the way this little piece of code is going to be used is in 4 limited numeric textboxes. The user won't be able to input no more than 9 numeric characters. Of course I will be doing a lot of testing, since this is will be used in a calculator. thank you for your reply. i will keep this in mind.
 
  • #8
jtbell said:
Here's a solution that uses the standard C++ 'string' type, which has member functions for searching and slicing and dicing strings:

Code:
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string digits;
    cout << "Gimme a string of digits: ";
    cin >> digits;
    int pos_first_nonzero = digits.find_first_not_of("0");
    if (pos_first_nonzero == string::npos)  // string is all zeroes
    {
        digits = "0";
    }
    else if (pos_first_nonzero != 0)
    {
        digits = digits.erase(0, pos_first_nonzero);
    }
    // If pos_first_nonzero == 0, 
    // there are no leading zeroes and we don't do anything.
    cout << digits << endl;
    return 0;
}

Actually, we can simplify this a bit if we don't mind the program going through the motions of "erasing" zero characters when there are no leading zeroes:

Code:
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string digits;
    cout << "Gimme a string of digits: ";
    cin >> digits;
    int pos_first_nonzero = digits.find_first_not_of("0");
    if (pos_first_nonzero == string::npos)  // string is all zeroes
    {
        digits = "0";
    }
    else
    {
        digits = digits.erase(0, pos_first_nonzero);
    }
    cout << digits << endl;
    return 0;
}

WOW! Yours is much safer than my messy junk, as mXSCNT pointed out. I will try to implement your example instead since my original datatype is a string, well a CString. The way i was doing it, was making me convert a CString to char. So I like your method better. Thank you all for your input. I'll you all posted.
 
  • #9
Beware that 'CString' is (I think) a Microsoft thing and not a standard C++ thing, and is different from 'string'. So you'll have to convert between them. I expect that Microsoft provides string-manipulation functions for 'CString's, but I don't know anything about them.
 

1. How do I delete a specific number from the beginning of a string of numbers?

To delete a specific number from the beginning of a string of numbers, you can use the string's slice() method. This method takes two parameters: the starting index and the ending index. To delete the first number, you would use string.slice(1), which will return the string starting from the second character.

2. Can I use a regular expression to delete a specific number from the beginning of a string of numbers?

Yes, you can use a regular expression with the replace() method to delete a specific number from the beginning of a string of numbers. The regular expression would match the first number and then you can use an empty string as the replacement, effectively deleting it.

3. What happens if the string does not start with the specified number?

If the string does not start with the specified number, the methods mentioned above will still work. The slice() method will simply return the entire string, while the replace() method will not make any changes to the string.

4. Can I delete more than one number from the beginning of a string of numbers?

Yes, you can delete more than one number from the beginning of a string of numbers by adjusting the parameters of the slice() or replace() methods. For example, to delete the first two numbers, you would use string.slice(2) or string.replace(/^.{2}/, '').

5. Is there a way to delete a specific number from the beginning of a string of numbers without modifying the original string?

Yes, you can use the substring() method to return a new string without modifying the original one. This method takes two parameters: the starting index and the ending index. To delete the first number, you would use string.substring(1).

Similar threads

  • Programming and Computer Science
Replies
10
Views
2K
  • Programming and Computer Science
4
Replies
118
Views
6K
  • Programming and Computer Science
Replies
5
Views
846
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
24
Views
1K
  • Programming and Computer Science
Replies
18
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
3
Replies
81
Views
5K
  • Programming and Computer Science
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
Back
Top