Answer the question
In order to leave comments, you need to log in
How can I insert a certain word into a string at the place I need?
Greetings to all! The following task is on the agenda: "Develop a function that inserts a given word into a sentence. The new word should become the nth word in the sentence (n is given as the third parameter of the function). From the keyboard, enter three character strings and a word. Using the developed function, insert an additional word into the entered lines so that in the first line it becomes the first, in the second - the third, and in the last - the fifth"
There are no problems with the first sentence. But with the second and third, it does not work out. I tried to count the number of spaces in sentences, but it doesn't even work.
Tell me where and what is wrong?
Thanks in advance to everyone who will answer)))
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
# define MAX 1000
struct Stroka{
char First[100];
char Second[100];
char Third[100];
char Word[20];
}list[MAX];
int i = 0;
void Poshuk();
void Poshuk1();
void Poshuk2();
int main(){
for (i; i < 1; i++) {
system("chcp 1251");
printf("Первое предложение: ");
gets_s(list[i].First);
printf("Второе предложение: ");
gets_s(list[i].Second);
printf("Третье предложение: ");
gets_s(list[i].Third);
printf("Слово: ");
gets_s(list[i].Word);
getchar();
}
Poshuk();
Poshuk1();
Poshuk2();
return 0;
}
void Poshuk() {
for(int j = 0; j < i; j++) {
printf_s("Обработанное: %s %s", list[j].Word, list[j].First);
}
getchar();
}
void Poshuk1() {
char* p2 = list[i].Word, * p1 = list[i].Second;
for (int i = 0; i < 3; i++) {
if (*p1 == ' ') {
p1++;
}
}
strcpy(p1, p2);
printf_s("Обработанное: %s", &p1);
getchar();
}
void Poshuk2() {
char* p2 = list[i].Word, * p1 = list[i].Third;
for (int i = 0; i < 5; i++) {
if (*p1 == ' ') {
p1++;
}
}
strcpy(p1, list[i].Word);
printf_s("Обработанное: %s", &p1);
getchar();
}
Answer the question
In order to leave comments, you need to log in
Not so in almost every line.
First, why do you need a four-line structure? What for to you an array from it on 1000 elements.
What the hell is this all about?
for (i; i < 1; i++) {
Does this cycle of one iteration bother you at all?
Further, the assignment clearly states
Develop a function that inserts a given word into a sentence. The new word must become the nth word in the sentence (n is given as the third parameter of the function)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question