I have a task in rows, in which I need to enter two lines and, after each word of the first line, insert the second line

My idea was to do everything through the third line.
That is, first find the number of letters in the second line and the address of the last letter of the first line, and then copy from the first to the third, by element, until I reach the space. Then, via strcat, copy the entire second line to the first line and continue elementwise to copy, and how to go to the last element of the first line (determining whether you’ve reached the address), insert a space, second line and '\ 0'

#include<stdio.h> #include<iostream> #include<string.h> int main() { char *a, s1[60], s2[10], s3[100]; int i,j,n = 0; printf("Input the first line: \n"); scanf("%s", &s1); printf("Input the second line: \n"); scanf("%s", &s2); //ввод строк for (i = 0; s2[i] != '\0'; i++) n++; //сколько элементов во второй строке for (i = 0; s1[i] != '\0'; i++) a=&s1[i]; //адрес последнего элемента первой строки j=0; for (i = 0; s1[i] < n; i++) { if (&s1[i]==a) //проверка, не последний ли это элемент строки {s3[j]=s1[i]; j++; s3[j]=' '; j++; strcat(s3, s2); j=j+n; s3[j]='\0'; } else if (s1[i]==' ') //проверка, не закончилось ли слово {s3[j]=s1[i]; j++; strcat(s3, s2); j=j+n; s3[j]=s1[i]; j++; } else //копирование строк {s3[j]=s1[i]; j++; } } printf("final line: %s\n", s3); system("pause"); return 0; } 

As a result, when you enter lines
Displays like. One time, I even got 'th

And when checking how the lines were entered, it shows that there was only one word (abc) in the first one, and the rest were simply ignored.

PSSori, if my mistake is very stupid - I'm just learning)

  • If anyone is interested, the error was first in scanf instead of gets, and secondly in the crookedly exposed cycle conditions - Abysswalker

0