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
#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
Get Answers For Free
Most questions answered within 1 hours.