Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- char *concat(char **s, int n)
- {
- unsigned long length = 0;
- for (int i = 0; i < n; i++) { // Получаем длину итоговой строки
- length += strlen(s[i]);
- }
- char *str = (char *)malloc(length); // Выделяем память для новой строки
- for (int i = 0; i < n; i++) {
- if (i == 0) {
- strcpy(str, s[i]); // Инициализируем результирующую строку
- } else {
- strcat(str, s[i]); // Дописываем
- }
- }
- return str;
- }
- int main()
- {
- int amount;
- scanf("%d\n", &amount); // Ввод количества
- char **array = (char **)malloc(sizeof(char *) * amount); // Создаем массив из строк
- for (int i = 0; i < amount; i++) {
- array[i] = (char *)malloc(sizeof(char) * 512); // Создаем строки
- gets(array[i]); // Читаем
- }
- printf("%s\n", concat(array, amount)); // Печатаем результат
- for (int i = 0; i < amount; i++) { // Освобождаем память
- free(array[i]);
- }
- free(array);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement