You can use the strcpy()
function of the standard string.h
library to copy each of the strings contained between the two-dimensional arrays texto[][]
and string[][]
.
Never use the gets()
function! It was considered% of% due to the deprecated
risk it represents. Use buffer overflow
or scanf()
of the default library fgets()
.
Here is a tested code with the correct fixes:
#include <stdio.h>
#include <string.h>
#define MAX 3
#define LEN 80
char texto[MAX][LEN];
int main(){
char string[MAX][LEN];
int i;
for( i = 0; i < MAX; i++ ){
printf("Informe string %d: ", i );
scanf( "%s", texto[i] );
}
printf("\nStrings digitadas foram:\n");
for( i = 0; i < MAX; i++ )
printf("[%d] %s \n", i, texto[i] );
for( i = 0; i < MAX; i++ )
strcpy( string[i], texto[i] );
printf("\nStrings copiadas foram:\n");
for( i = 0; i < MAX; i++ )
printf("[%d] %s \n", i, string[i] );
return 0;
}
However, if the challenge is not to use the stdio.h
function, you can try something like:
#include <stdio.h>
#define MAX 3
#define LEN 80
char texto[MAX][LEN];
int main(){
char string[MAX][LEN];
int i, j;
for( i = 0; i < MAX; i++ ){
printf("Informe string %d: ", i );
scanf( "%s", texto[i] );
}
printf("\nStrings digitadas foram:\n");
for( i = 0; i < MAX; i++ )
printf("[%d] %s \n", i, texto[i] );
for( i = 0; i < MAX; i++ )
for( j = 0; j < LEN; j++ )
string[i][j] = texto[i][j];
printf("\nStrings copiadas foram:\n");
for( i = 0; i < MAX; i++ )
printf("[%d] %s \n", i, string[i] );
return 0;
}
Output:
Informe string 0: aeiou
Informe string 1: abcdefghijklmnopqrstuvwxyz
Informe string 2: 1234567890
Strings digitadas foram:
[0] aeiou
[1] abcdefghijklmnopqrstuvwxyz
[2] 1234567890
Strings copiadas foram:
[0] aeiou
[1] abcdefghijklmnopqrstuvwxyz
[2] 1234567890