I think the best answer I can give you is that different people learn this stuff at different rates. Comparing yourself to others is not really the best way to judge if you want to do this for a living.

Your code example is trivial for me. But I've been a professional programmer for nearly 30 years. I've been programming for 41 years. I programmed at least a little bit almost every single day of those 41 years (I'm not sure if I have ever spent even 1 month away from the keyboard). When you have done the same thing as I have done, then this problem will be trivial to you too.

Doing a 4 year CS degree is waaaaay too short a time to decide whether or not you are going to be successful at this stuff. Yeah, some people are super talented and pick it up right away. But then, some of the people that look like they are super talented are that way because they've been programming since they were able to read.

If you are struggling now, then you will struggle at work too. You will probably have to work harder than the people who are talented or the people who appear talented because they have already put in a decade or two before you even started. You may find that the 9-5 work day is not enough for you to make the kind of progress you want in your career. There is nothing wrong with this.

The question you have to ask yourself is, "Do I want to do this?" If you are struggling, do you want to struggle? I struggle too -- even after more than 4 decades of programming every day. I pick away at it slowly, by slowly, by slowly. I have this super, ridiculous ego that fuels me, but I've also gotten old enough to realise that my ego is just that thing that let's me ignore how crap I am -- sometimes you have to do that. "Yeah, whatever. I seem to suck now, but just wait until you see my super awesome computer program that I'm doing on the side." (And... I never finish my side projects either... Don't tell anybody!)

On the other hand, there are lots of jobs in this world. You don't have to be a programmer. There are lots of reasons not to be a programmer and none of them revolve around whether or not you are talented at the job. It's lonely. It's stressful. It's stupifyingly hard to concentrate every second of the day all day long. People yell at you for fucking things up even when you've done a good job. Nobody can agree on what's good and what's not good and often sit around arguing about it all day long. because it's a super ridiculously details oriented job, most of your successful colleagues will have some kind of OCD related mental problem. Because it's a super ridiculously details oriented job, nobody will understand what you do for a living and everyone will think to themselves, "But that's easy. I could do that in 15 minutes. God you suck". The pay is good, but it's never enough to put up with the crap you have to take -- seriously, ask people why they want a raise; the answer is always, "Because if I'm going to have to put up with that crap, I want a lot more money".

So, be a programmer if you want to be a programmer. Not being talented is the least of your worries. That's just a matter of putting in the time and effort. Your rewards will be internal. If you love what you are doing, then the rewards will be great.

And I have a confession. If I did that coding example, I would definitely make an off by one mistake. Definitely. :-)