Question

C programming Write a function that takes in a 2D char array (string array) and return...

C programming
Write a function that takes in a 2D char array (string array) and return a 1D char array with all the elements connected together

Hint:

strlen(-char*-) //returns the length of a string
strcat(-char* accum-, something to add) //works like string+= in java

Homework Answers

Answer #1

#include <stdio.h>
#include <string.h>
//function to convert 2D array to 1D array
void towDArrTo1DArr(char c[2][3], char str[6])
{
//outer for loop
for(int i=0; i<2; i++)
{
//inner for loop
for(int j=0; j<3; j++)
{
//append at the end of the stirng
strncat(str, &c[i][j], 1);
}
}
}

int main()
{
//2d array declaration and initialization
char c[2][3] = {{'a', 'b', 'c'}, {'d', 'e', 'f'}};
  
//1d array declaration
char str[2];
  
//function calling
towDArrTo1DArr(c, str);

//display result
printf("%s", str);
  
return 0;
}

OUTPUT:

abcdef

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write Java program Lab51.java which takes in a string from the user, converts it to an...
Write Java program Lab51.java which takes in a string from the user, converts it to an array of characters (char[] word) and calls the method: public static int countVowels(char[]) which returns the number of vowels in word. (You have to write countVowels(char[]) ).
Write a function makeDoubles in c++, that takes an integer parameter called size and: allocates a...
Write a function makeDoubles in c++, that takes an integer parameter called size and: allocates a new array of that many double's initializes all elements to be 0 returns this array
C++ Write a function that takes in 3 arguments: a sorted array, size of the array,...
C++ Write a function that takes in 3 arguments: a sorted array, size of the array, and an integer number. It should return the position where the integer value is found. In case the number does not exist in that array it should return the index where it should have been if it were present in this sorted array. Use pointer notation of arrays for this question. c++ code
1.Write a function which takes a string that contains two words separated by any amount of...
1.Write a function which takes a string that contains two words separated by any amount of whitespace, and returns a string in which the words are swapped around and the whitespace is preserved. Hint: use regular expressions where \s detects the whitespaces in a string. Example: given "Hello     World", return "World         Hello" 2.Pandas exercises: Write a python program using Pandas to create and display a one-dimensional array-like object containing an array of data. Write a python program using Pandas to...
Write a method which takes string as an argument and extracts all words of length between...
Write a method which takes string as an argument and extracts all words of length between 4 to 5 and contains vowels in it. Method returns an array of type string containing words which satisfied above criteria. Show these words in main(). using c#  
Programming in C language (not C++) Write a main function with a function call to a...
Programming in C language (not C++) Write a main function with a function call to a function called Calculation which has two integer arguments/ parameters. The function returns a character. Declare and initialize the necessary data variables and assign balues needed to make it executable and to prevent loss of information. //input 2 integer values //returns the characterequivalent of the higher of the two integer values char Calculation(int, int);
In C programming language write a function that takes two arrays as input m and n...
In C programming language write a function that takes two arrays as input m and n as well as their sizes: size_m and size_n, respectively. Then it checks for each element in m, whether it exists in n. The function should update a third array c such that for each element in m: the corresponding position/index in c should be either 1, if this element exists in m, or 0, if the element does not exist in n
1. Please write the following in C++ also please show all output code and comment on...
1. Please write the following in C++ also please show all output code and comment on code. 2. Also, use CPPUnitLite to write all test and show outputs for each test. Write CppUnitLite tests to verify correct behavior for all the exercises. The modifications are aimed at making the exercises more conducive to unit tests. Write a function that swaps (exchanges the values of two integers). Use int* as the argument type. Write a second swap function using a reference...
Write a Python function that takes a filename as a parameter and returns the string 'rows'...
Write a Python function that takes a filename as a parameter and returns the string 'rows' if a file was written row by row, and 'columns' if the file was written column by column. You would call the function with a command like filetype = myfunc(filename).
Explain your code with comments. Solve in C++. Write a function named myFunc3() that takes a...
Explain your code with comments. Solve in C++. Write a function named myFunc3() that takes a 2D integer array NUMBERS[][50], and it size n and m. Then the function will print the sum of each row in one line.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT