Input digit extraction and manipulator

Dears,
I want to know how to write a method to manipulate and extract certain number form user input digits in different way.

1- How to write a method named digiti that inputs integers n and i. The method should return the ith digit of n.

For example, if the inputs are 89745 and 2, The method should return 7. Note that zeroth digit is 5, first digit is 4 and second digit is 7.

2- How to write a method named digitRemoveK that inputs integers n and k. The method should remove the kth digit in n. For example, if the inputs are 61748 and 3, The method should return 6748. Note that the third digit is removed from 61748.

- How to write a method named digitRemove that inputs integers n and k. The method should remove all digits with value k in n. For example, if the inputs are 647544 and 4, it should return 675. Note that all occurrences of 4 in 647544 are removed.

Thanks
Mooz

Last edited on
The key is to process these as strings, not integers. Then you can turn them back into integers if you need that.

1)
string s;
cin >> s;
s[0]; //its really '5' so you want to digitize that if you need it numerically.
depending on what you are doing you may need to see if the input is valid, or you can force them to input an integer and convert that into a string, whatever works best for you.

2)
a)use standard string tools to remove it similar to 1
b) use the string.erase(remove_if() ) paradigm

you have to write the remover function or lambda

here is a really crude way to do it

struct bighelp
{
static char sc;
};
char bighelp::sc;

bool remover(char c)
{
bighelp b;
return c == b.sc;
}

then just make a bighelp and set sc to what you want to remove based off user input. This is a shameless dodge of a global. Someone here probably has a much, much cleaner way, but I am not great at string processing and am stuck with a 2012 compiler to boot.
Last edited on
Topic archived. No new replies allowed.