Day 88: How do you Reverse an Array in C?

I got back to working on both C and JavaScript again. I fell off over the weekend. In C I struggled with saving some example program for converting an int to a “string” in C. I got stuck at reversing array values. This is probably something that I should learn and is a common problem. I’ll look into this a bit tomorrow but I can’t lie – it pissed me off. I planned to spend 10 minutes on this and ended up spending damn near an hour. Live and learn I guess. I have still got a lot to learn. I included the example so maybe you won’t make may mistakes when you try this LOL.

TLDR;

Okay, so here are the highlights of what I did:

  • I spent a surprisingly long time trying to store an example of “how to convert an integer into a string” in C. My first method that did not rely on the sprintf function failed. I couldn’t figure out how to reverse the order of my char array. I think that this is a common coding challenge so I will look into it more tomorrow. The code is below.
  • Read through the docs for the Function constructor and Modules in JavaScript. These docs were from MDN. I also started to read the NodeJS docs on CommonJS. I am searching for a bit more information on how CommonJS is written to supplement the information I got while re-reading chapter 10 of “Eloquent JavaScript” by Marijn Haverbeke. I am getting more comfortable with the material but I believe there are some pieces left out that would help strengthen my connection with the material. I will hopefully finish reading through the docs tomorrow and move forward to chapter 11 in the book.

Convert int to String Program in C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(){

  char revStr[10];
  char str[10];
  int number, R;
  number = R = 1500;

  // Method 1: Loop and divide out each digit to copy ANSI
  //           Unfortunately I can't figure out how to get this working correctly

  for(int i=0; i<sizeof(str); ++i){        // Paint over str memory
    revStr[i] = ' ';
    str[i] = ' ';
  }

  for(int i=0;; ++i){        // Copy digits into str as ANSI digit chars
    if(R <= 0)
      break;
    revStr[i] = '0' + (R % 10);
    R = (int)(R / (float)10);
    printf("R = %i, Char = %c\n", R, revStr[i]);
  }
  printf("result: %s\n", revStr);

  int last = sizeof(str) - 1;

  for(int i=last; i >= 0; --i){        // Reverse the copied array
    if(revStr[i] > '9' || revStr[i] < '0')
      continue;
    str[last - i] = revStr[i];
    printf("i: %i\tChar = %c\n", i, str[i]);
  }
  printf("result: %s\n", str);

  // Method 2: use the sprintf function
  sprintf(str, "%i", number);
  printf("---------\n");
  printf("result: %s\n", str);

  return 0;
}

# Output:

R = 150, Char = 0
R = 15, Char = 0
R = 1, Char = 5
R = 0, Char = 1
result: 0051      `�a
i: 3	Char =  
i: 2	Char =  
i: 1	Char =  
i: 0	Char =  
result:       15000051      	
---------
result: 1500


Goal For Round 6 of the #100DaysofCode Challenge

This is my sixth round of the “#100daysofcode” challenge. I will be continuing my work from round five into round six. I am currently working through the book “Cracking the Coding Interview” by Gayle Laakmann McDowell. My goal is to become more familiar with algorithms and data structures. This goal was derived from my goal to better understand operating systems and key programs that I use in the terminal regularly e.g. Git. This goal was in term derived from my desire to better understand the fundamental tools used for coding outside of popular GUIs. This in turn was derived from my desire to be a better back-end developer.

I have no idea if my path is correct but I am walking down this road anyways. Worst case scenario I learn a whole bunch of stuff that will help me out on my own personal projects.