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
C Programming: Write a function that takes in an array of integers and an integer containing...
C Programming: Write a function that takes in an array of integers and an integer containing the count of elements, then have it returns the sum of all the even values inside the array.
***C Programming*** Given a 2D array. Write a function that will take the 2D array as...
***C Programming*** Given a 2D array. Write a function that will take the 2D array as argument and then print the maximum number in each row of that 2D array.
C Programming 1. Write a void function that takes in a 2D array of character and...
C Programming 1. Write a void function that takes in a 2D array of character and have it print out each array on a new numbered line on the console. 2. Illustrate the stack and the heap allocation. Specify what each variable value holds and where different references are pointing to. int main() { int myarray[3]; myfunction (myarray); } int myfunction(int* in) { int i; for (i = 0; i<3; i+= 1) { in[i] = i; } // illustrate the...
FOR C PROGRAMMING LANGUAGE Write a recursive C PROGRAMMING LANGUAGE function int sumStrlens(const char* str1, const...
FOR C PROGRAMMING LANGUAGE Write a recursive C PROGRAMMING LANGUAGE function int sumStrlens(const char* str1, const char* str2) which returns the sum of strlen(str1) and strlen(str2). You can assume that strlen(str2) is always strictly greater than strlen(str1) (strlen(str2) > strlen(str1)). You MAY NOT use any function from string.h. You MAY NOT use any square brackets([]) or any type of loops in function.
Task 2: Compare strings. Write a function compare_strings() that takes pointers to two strings as inputs...
Task 2: Compare strings. Write a function compare_strings() that takes pointers to two strings as inputs and compares the character by character. If the two strings are exactly same it returns 0, otherwise it returns the difference between the first two dissimilar characters. You are not allowed to use built-in functions (other than strlen()) for this task. The function prototype is given below: int compare_strings(char * str1, char * str2); Task 3: Test if a string is subset of another...
C programming question: Write a function char isValid (char c), return a with a char value...
C programming question: Write a function char isValid (char c), return a with a char value of 1 if it is a valid char, and return 0 if it is invalid. (DO NOT USE POINTER!) valid char are '1' '2' 3' '4' '5' 'A' 'B' 'C' for example: lower case is not valid '1' is valid 'A' is valid 'B' is valid 'ABC' is not valid 'D' is not valid '9' is not valid '1234' is not valid For Testing:...
THIS IS JAVA PROGRAMMING Write a method that finds the shortest word in an array of...
THIS IS JAVA PROGRAMMING Write a method that finds the shortest word in an array of String values and returns the length, in characters, of that word. The method should use the following header. public static int minLength(String[] array) Write a test program that prompts the user to enter ten strings, store them in an array and invokes this method to return the shortest length, and displays that value. (A near complete program can be found attached to the dropbox)
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 template function maxn() that takes as its arguments an array of items of type...
Write a template function maxn() that takes as its arguments an array of items of type T and an integer representing the number of elements in the array and that returns the largest item in the array. The number of elements should take the default value of 10. The program should include a specialization that takes an array of strings as an argument and returns the longest string. (If there is a tie, the function should return the first one...
Perl Programming Language 1) Write a Perl script to concatenate "This is String One" and "This...
Perl Programming Language 1) Write a Perl script to concatenate "This is String One" and "This is String Two". Print all three strings, one per line with its length. Output: This is String One (print length here) This is String Two (print length here) This is String One This is String Two (print length here) 2) Write a Perl function that creates an array using qw function with the month names. It then prints the elements 4 and 7 (May...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT