cut out digits

can anybody tell me how to cut out the middle digits of a number
eg: 255543
=23
Hi...
You should use the function itoa. After that cutting any digit is very easy.

E.g
1
2
3
char str[20];
itoa(str, 356819 , 10);
//str = "356819" 

Hope this helps (please note the code :))
Last edited on
From what I have seen itoa is outdated and is not standard C/C++.
I suggest using stringstreams instead (You will need to include sstream).

1
2
3
4
5
int x = 12345;

stringstream SS;
SS << x;
string y = SS.str();


Then you can just use the erase functions from string.

http://www.cplusplus.com/reference/string/string/
http://www.cplusplus.com/reference/sstream/stringstream/
Last edited on
It's easy even without the strings library:
1
2
3
4
5
6
7
8
9
10
11
12
13
int cutMiddleDigits(int input)
{
  int OnesDigit = input%10;
  int TensDigit;

  while (input > 0)
  {
    TensDigit = input%10;
    input /= 10;
  }

  return 10*TensDigit + OnesDigit;
}
Last edited on
Topic archived. No new replies allowed.