4
I would like answers in C, C#, or just an algorithm, but preferably an implementation already in the C# language because that’s what I’m using.
For example, suppose I have to find out if a number is read the same way in both directions - a silly programming exercise that I can never solve.
4004
I figured I could do the following:
- Convert to string.
- I place the string in an array of characters.
- Using an appropriate function I discover the size of the array.
- I create another array of the same size.
- I copy bytes of
array1
to thearray2
, in reverse order (position 0 inarray1
becomes the last ofarray2
, position 1 becomes penultimate, etc...) - I compare the resulting strings, if they are equal the number is a palindrome.
I think it works, but besides not knowing how to do it in language, I think there must be some simpler way.
Why
texto.Substring(i, 1)
and nottexto[i]
? It is more efficient to compare the character than to create and allocate 2*n sub strings. One more thing, the breakage condition can bei > texto.Length /2
. And since OP is not experienced in either programming or language, I think a Portuguese explanation of the algorithm would help.– dcastro
It’s just that I based myself on an algorithm that I already had in another language. In fact I can optimize.
– Maniero