Practice programming question: null terminated strings and command line arguments

  1. Write an InitCharArr function that takes two parameters: a character array, text, and an integer, N, and initializes the first N characters of the array to '\0'.

  2. The function below copies the contents of str2 onto the end of str1. Modify it to accept a third parameter, int n, that specifies a maximum length for the resulting str1.
    void append(char str1[], char str2[])
    {
       int pos1 = 0; // tracks current pos in str1
       int pos2 = 0; // tracks current pos in str2
    
       // find the end of str1
       while (str1[pos1] != '\0') pos1++;
    
       // copy str2 contents until end of str2
       while (str2[pos2] != '\0') {
          str1[pos1] = str2[pos2];
          pos1++;
          pos2++;
       }
    
       // end str1
       str1[pos1] = '\0';
    }
    

  3. Show the output the following function would produce if we made the call f1("abc", 3, "12345", 5)
    void f1(char str1[], int n1, char str2[], int n2)
    {
       if (n1 < 1) {
          printf("%s\n", str2);
       } else if (n2 < 1) {
          printf("%s\n", str1);
       } else {
          n1--;
          n2--;
          printf("%c%c", str1[n1], str2[n2]);
          f1(str1, n1, str2, n2);
       }
    }
    
    void f1(char str1[], int n1, char str2[], int n2)
    {
       if (n1 < 1) {
          cout << str2 << endl;
       } else if (n2 < 1) {
          cout << str1 << endl;
       } else {
          n1--;
          n2--;
          cout << str1[n1] << str2[n2];
          f1(str1, n1, str2, n2);
       }
    }
    

  4. Write a function that takes a null-terminated character array as a parameter, and counts and returns the number of uppercase alphabetic characters the array contains prior to the null terminator.

  5. Write suitable comments for the itoa and ftoa functions below
    string ftoa(float f)
    {
       const int decimals = 6;
       bool neg = false;
       if (f < 0) {
          neg = true;
          f = -f;
       }
       int d, digit;
       long intf = floor(f);
       f = f - intf;
       for (d = 0; d < decimals; d++) f = f * 10;
       long fracf = floor(f);
       if ((f - floor(f)) >= 0.5) fracf++;
       string rev = "";
       for (d = 0; d < decimals; d++) {
          digit = fracf % 10;
          fracf = fracf / 10;
          rev += (char)(digit + '0');
       }
       rev += ".";
       while (intf > 0) {
          digit = intf % 10;
          intf = intf / 10;
          rev += (char)(digit + '0');
       } 
       string result = "";
       if (neg) result = "-";
       int len = rev.length();
       while (len > 0) {
          len--;
          result += rev[len];
       }
       return result;
    }
    
    string itoa(int i)
    {
       string rev = "";
       bool neg = false;
       if (i < 0) {
          neg = true;
          i = - i;
       }
       while (i > 0) {
          int digit = i % 10;
          i = i / 10;
          rev += (char)(digit + '0');
       }
       string result = "";
       if (neg) result = "-";
       int len = rev.length();
       while (len > 0) {
          len--;
          result += rev[len];
       }
       return result;
    }
    

  6. Write a program that expects at least two command line arguments (displaying an error message if insufficient arguments are supplied when the program is invoked), and displays the arguments that are in odd-numbered positions of argv. E.g. if the program is invoked as
    ./someprogram these are the arguments it is given
    Then it would display
    these the it given

  7. Write a program that accepts any number of command line arguments and uses atof to display the numeric translation of each, e.g.
    ./myprog 13 27.6 foo
    would result in output like 0, 13, 27.6, 0 (since atof(argv[0]) and atof(argv[3]) would each return 0).