Question

In: Computer Science

Programming in C:- 1. Suppose that a variable 's' is an array of strings (i.e. a...

Programming in C:-

1. Suppose that a variable 's' is an array of strings (i.e. a pointer array where each element points to a string).

Write an expression that obtains the length of the third string of 's'. You may assume that string.h has been included.

2.

Consider the following function whose purpose is to return an array of 3 strings that are initialized to the strings in the character arrays s1, s2, and s3:

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

char** stringstoarray(char* s1, char* s2, char* s3) {

    // dynamically allocate space for the pointer array itself.
    char** strings = _________          
  

    strings[ 0 ] = strdup(s1);  // initialize first string
    strings[ 1 ] = strdup(s2);  // initialize first string
    strings[ 2 ] = strdup(s3);  // initialize third string

    return strings;

}

In order for the function for the function to carry out its purpose, fill in the blank with the necessary code.

3.

Suppose that a variable 's' is an array of strings (i.e. a pointer array where each element points to a string).

Write an expression that accesses the 4th character of the 8th string of 's'.

4.

Write the declaration of an array of 128 strings. Use 's' for the name of the variable.

Solutions

Expert Solution

Programming in C:-

1.
    {
        int totalstrings;
        // totalstrings is no of strings in array of string s;
        char **s = malloc(totalstrings * sizeof(char *));
        int i;
        // Alloctaing space for each string in array;
        for (i = 0; i < totalstrings; ++i)
        {
            s[i]=(char *)malloc(stringsize+1); //stringsize is size of one string;
        }
        // In order to return lenth of third string of s (array of string)
      
        return (s[2].length());
    }

2.

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

    char** stringstoarray(char* s1, char* s2, char* s3)
    {

    // dynamically allocate space for the pointer array itself.
    char** strings=malloc(3 * sizeof(char *));        
    int i;
    //totalstrings is number of total string
    totalstrings=3;
    for (i = 0; i < totalstrings; ++i)
    {
        strings[i] = (char *)malloc(stringsize+1); //stringsize is size of single string to be put in array
    }
  
    strings[ 0 ] = strdup(s1); // initialize first string
    strings[ 1 ] = strdup(s2); // initialize first string
    strings[ 2 ] = strdup(s3); // initialize third string

    return strings;

}

3.

    // In order to access 4th character of 8th string in array 's'
    return s[7][3];
4.
    char **s= malloc(128 * sizeof(char *));
    int i;
    for(i=0;i<128;i++)
    {
    //stringsize is size of one string
    s[i]=(char *)malloc(stringsize+1);
}


Related Solutions

This is C++ programming Given an int variable k, an int array incompletes that has been...
This is C++ programming Given an int variable k, an int array incompletes that has been declared and initialized, an int variable nIncompletes that contains the number of elements in the array, an int variable studentID that has been initialized, and an int variable numberOfIncompletes, Write code that counts the number of times the value of studentID appears in incompletes and assigns this value to numberOfIncompletes. You may use only k, incompletes, nIncompletes, studentID, and numberOfIncompletes. I tried this code...
in C programming language char character [100] = "hello"; a string array variable It is given....
in C programming language char character [100] = "hello"; a string array variable It is given. By writing a function called TranslateString, By accessing the pointer address of this given string, returning the string's address (pointer address) by reversing the string Write the function and use it on the main function. Function void will not be written as. Return value pointer address it will be. Sweat operation on the same variable (character) It will be made. Declaration of the function...
1.Declare a two-dimensional array of Strings namedchessboard.
1. Declare a two-dimensional array of Strings named chessboard.2. Declare a two-dimensional array of integers named tictactoe.3. Declare and create a two-dimensional array of chars,tictactoe, with 3 rows, each with 3 elements.4. Create a two-dimensional array of ints, plan, with 2 rows, and and 3 columns and initialize the first row to 8, 20, 50 and the second row to 12, 30, 75. Use member initializer syntax.
Create a C++ program that makes use of both concepts i.e. Array of structure and array...
Create a C++ program that makes use of both concepts i.e. Array of structure and array within the structure by using the following guidelines: 1. Create an Array of 5 Structures of Student Records 2. Each structure must contain: a. An array of Full Name of Student b. Registration Number in proper Format i.e 18-SE-24 c. An array of Marks of 3 subjects d. Display that information of all students in Ascending order using “Name” e. Search a particular student...
Programming in C++ Write a program that prints the values in an array and the addresses...
Programming in C++ Write a program that prints the values in an array and the addresses of the array’s elements using four different techniques, as follows: Array index notation using array name Pointer/offset notation using array name Array index notation using a pointer Pointer/offset notation using a pointer Learning Objectives In this assignment, you will: Use functions with array and pointer arguments Use indexing and offset notations to access arrays Requirements Your code must use these eight functions, using these...
The purpose of this C++ programming assignment is to practice using an array. This problem is...
The purpose of this C++ programming assignment is to practice using an array. This problem is selected from the online contest problem archive, which is used mostly by college students worldwide to challenge their programming ability and to prepare themselves for attending programming contests such as the prestige ACM International Collegiate Programming Contest. For your convenience, I copied the description of the problem below with my note on the I/O and a sample executable. Background The world-known gangster Vito Deadstone...
c++ Redo Programming Exercise 14 by first sorting the array before determining the array elements that...
c++ Redo Programming Exercise 14 by first sorting the array before determining the array elements that are the sum of two other elements. Use a selection sort algorithm, discussed in this chapter to sort the array. Instructions and code for Programming Exercise 14 have been included for your convenience. Exercise 14 Write a program that prompts the user to enter 50 integers and stores them in an array. The program then determines and outputs which numbers in the array are...
Suppose that each row of an n × n array A consists of 1’s and 0’s...
Suppose that each row of an n × n array A consists of 1’s and 0’s such that, in any row of A, all the 1’s come before any 0’s in that row. Assuming A is already in memory, describe a method running in O(n log n) time (not O(n2) time!) for counting the number of 1’s in A.
Exercises on Arrays –using C++programming 1. You want an array with the numbers 100 – 105....
Exercises on Arrays –using C++programming 1. You want an array with the numbers 100 – 105. In the boxes below, fill in what your array should have. Fill in the index of each element below it. Array Index Open up your editor and write the following program: Declare an array that has the numbers 100 to 105. (How many elements are there in the array?) Print the array. Save your file and test it. Compare your results with your table...
In C Programing Create a stack using an array. Define the index variable of the array...
In C Programing Create a stack using an array. Define the index variable of the array name to be stacktop. Initially set stacktop to -1. Next create two functions push and pop. Both take as input 3 items: a pointer to the stack in memory, stacktop, and maxstack. Make sure to inc stacktop by one and also remember that stacktop[0] is the bottom of the stack and by stack rule cannot be accessed until all the other items are popped....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT