The nth digit

Can anyone help me to create a function that I put a number, the position of the digit, and the function returns me the digit that is in this position (from left to right)
ex:(3658,0) = 3 The digit in position 0 is 3
Hi aidam,
Here it is... just go check here https://www.punksheep.com/partage/C++/nthDigit.zip
==> just delete the #include "stdafx.h" :D I'm on Vindowz hehehe

By the way, is there a pastebin here ?
Last edited on
I couldn't open.
i dont know what is happening
oouhhhh aidam,
You're right ! sorry... I don't know what the pity my ftp client did with the file :(... and by now I found how to paste a source code so here it is :

1
2
3
4
5
6
char nthDigitFromLeftToRightOf(int number, int const posDigit)
{
	auto numberString = std::to_string(number);
	auto const stringLength = numberString.length();
	return (stringLength <= posDigit) ? '0' : numberString[posDigit];
}


Sorry again man... I'm on my way to check what's happening to my website :(
Thank you ;)

You can find the full source code with the function for both ways here https://www.punksheep.com/partage/C++/nthDigit.zip

See ya ;)

P.S. : here the full code
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
//[brief]	return the nth digit of a number (both ways)
//[forum]	http://www.cplusplus.com/forum/general/235078
//
//[author] punks Marc
//[contact email]	punksheep@kantamal.net
//[web site]	http://www.punksheep.com
//
//[note] delete the line <<#include "stdafx.h">> if it doesn't compile.
#include "stdafx.h"

#include <string>
#include <iostream>	//dump

char nthDigitFromLeftToRightOf(int number, int const posDigit);
char nthDigitFromRightToLeftOf(int number, int const posDigit);

int main()
{
	std::cout << "nth digit from left to right --> " << nthDigitFromLeftToRightOf(1234, 1) << " " << nthDigitFromLeftToRightOf(1234, 3) << " " << nthDigitFromLeftToRightOf(1234, 5) << std::endl;	//dump
	std::cout << "the other way around <-- " << nthDigitFromRightToLeftOf(1234, 1) << " " << nthDigitFromRightToLeftOf(1234, 3) << " " << nthDigitFromRightToLeftOf(1234, 5) << std::endl;	//dump
	return 0;
}


char nthDigitFromLeftToRightOf(int number, int const posDigit)
{
	auto numberString = std::to_string(number);
	auto const stringLength = numberString.length();
	return (stringLength <= posDigit) ? '0' : numberString[posDigit];
}

char nthDigitFromRightToLeftOf(int number, int const posDigit)
{
	auto numberString = std::to_string(number);
	auto const stringLength = numberString.length();
	return (stringLength <= posDigit) ? '0' : numberString[stringLength - 1 - posDigit];
}
Last edited on
thank u very much guy !!
thanks to you too aidam.
I didn't figure out my "transfert" problem... it's a problem with encoding characters !!! wow... 2018 and still some transfer problem even with utf-8 files >_<
So now, I put the zip version, it's a lot better and yes, a little bit smaller too :oP~

Good journey to you on your path of the C++ \o/
Topic archived. No new replies allowed.