Day 85: int to string conversions are No Joke

Everyday I code in C I’m reminded of how much I take for granted while using other languages. I worked on storing int values as strings and what should have been 1 – 2 lines of code became 8 – 9 lines. Wild. I also read some more from chapter 9 of “Eloquent JavaScript”. Saw some wild regular expressions and came to appreciate my how my effort in learning other languages is helping improve my foundational knowledge in programming. In my view all programming languages like all human languages share commonalities. Becoming familiar with those common concepts helps solidify your understanding of all languages. The rest is mainly just syntax and methodology. At least that’s my opinion.

TLDR;

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

  • Continued my work on my binary tree’s Horizontal Pretty Print function. I worked on converting the node values from their original int data type to a char[] (a.k.a. string) data type. The idea is to compile all the values on the same level into singular rows separated by a ‘Field Separator’ character. This will prep the data to be properly pulled, formatted, and printed (a.k.a. Pretty Printed) as a horizontal tree structure diagram in the shell.
  • Finished reading chapter 9 of “Eloquent JavaScript” by Marijn Haverbeke. The chapter covers regular expressions which I have a decent amount of experience with. I still need to takes notes but I did complete a rough outline. I really enjoyed my pacing with writing lighter notes for now. I am much more interested in the exposure to new information. Perhaps I’ll stick to this strategy until I come across very difficult concepts.

Converting an int into a char[] in C

This section will briefly cover my code and the challenges I faced producing the desired result. A string representation of the int value held in a node of my tree.

Again I am trying to gather all the values at the same level of a tree into a string representing the row. For example:

    1
   / \
  2   3

Results in:

row 1: "1,"
row 2: "2,3,"

However there is no default string type in C. I learned from searching online and previously seeing an example of type converting ints and chars that we can loop and use ASCII character values. Here is what I ended up with for the first node (root) with a value of 15:

void collectValues(treenode* root){
  char treeVals[10][COLUMNS];
  int level = 0;
  char separator = ',';
  
  // Get Node value, convert to string, concatenate with separator, append to the end of the treeVals row.

  // 1. convert value to a string
  char nodeValStr[10];
  int i = 0;
  int R = root->value;
  while(R > 0){
    nodeValStr[i] = '0' + (R % 10); 
    R = (int)(R / (float)10);
    printf("R = %i, Char = %c\n", R, nodeValStr[i]);
    ++i;
  }
  printf("result: %s\n", strrev(nodeValStr));
}

# Output:

R = 1, Char = 5
R = 0, Char = 1
result: 15

The looping involves taking the digit of least significance (5 for 15) using the modulus operator (%) and converting that into the ASCII value for the char ‘5’. Once we remove the least significant digit with a division by 10 and a round down (floor) by type conversion. We get our next digit to convert. Finally after everything I printed the flipped char[] to get my desired string of "15".

Now… that was the semi-hard way. The easier version, which I learned way later was to use the sprintf() function from one of the standard libraries including as a header file. I don’t even know which one it is associated with LOL. Anyways, here is the abstracted version. Wayyyyyy simpler!

void collectValues(treenode* root){
  char test[10];
  sprintf(test, "%i", root->value);
  printf("result: %s\n", test);
}

# Output:

result: 15

References:


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.