The task is as follows. I need to enter 2 sentences, the second consists of 3 words, and the 2 and 3 words in the second sentence should be swapped. Here is the code itself on pure C (!), Added comments:

#include <stdio.h> #include <string.h> #include <conio.h> int main() { char str1[100] = "\0", str2[100] = "\0", str3[100] = "\0"; puts("Vvedite str1: "); gets(str1); puts("Vvedite str2: "); gets(str2); int i, n = 0, k = 0, h = 0, l = strlen(str2); //n - ΠΏΠΎΠ»ΠΎΠΆΠ΅Π½ΠΈΠ΅ ΠΏΠ΅Ρ€Π²ΠΎΠ³ΠΎ ΠΏΡ€ΠΎΠ±Π»Π΅Π»Π° //k - ΠΏΠΎΠ»ΠΎΠΆΠ΅Π½ΠΈΠ΅ Π²Ρ‚ΠΎΡ€ΠΎΠ³ΠΎ ΠΏΡ€ΠΎΠ±Π΅Π»Π° //h - счСтчик ΠΏΡ€ΠΎΠ±Π΅Π»ΠΎΠ² //l - количСство Π±ΡƒΠΊΠ°Ρ„ Π² str2 for (i = 1; i < l; i++) { if (str2[i] == ' ') { h++; if (h == 1) { n = i+1; } if (h == 2) { k = i+1; } } } //Ρ†ΠΈΠΊΠ» поиска полоТСния ΠΏΠ΅Ρ€Π²ΠΎΠ³ΠΎ ΠΈ Π²Ρ‚ΠΎΡ€ΠΎΠ³ΠΎ ΠΏΡ€ΠΎΠ±Π΅Π»Π° printf("\nn = %d, k = %d, h = %d, l = %d", n, k, h, l); int y = k - n; //y - количСство Π±ΡƒΠΊΠ² Π²ΠΎ Π²Ρ‚ΠΎΡ€ΠΎΠΌ словС for (i = n + 1; i < k; i++) { strcpy_s(str3, y, str2); } //Ρ†ΠΈΠΊΠ», ΠΊΠΎΠΏΠΈΠΏΠ°ΡΡ‚ΠΈΡ€ΡƒΡŽΡ‰ΠΈΠΉ Π²Ρ‚ΠΎΡ€ΠΎΠ΅ слово Π² str3, Π² ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΌ Π΅ΡΡ‚ΡŒ ошибка Β―\_(ツ)_/Β― printf("\n", str3); _getch(); return 0; } 

I work in a Visual Studio 2015 environment. And it requires writing some operators with underscores and "s" ("_s"). Normal strcpy can not be used, because there is some garbage with security. As I found out, strcpy_s has its own format, that is,

 strcpy_s('ΠΊΡƒΠ΄Π° вставим', 'сколько Π±Π°ΠΉΡ‚ ΠΊΠΎΠΏΠΈΡ€ΡƒΠ΅ΠΌ', 'ΠΎΡ‚ΠΊΡƒΠ΄Π° ΠΊΠΎΠΏΠΈΡ€ΡƒΠ΅ΠΌ'); 

But my compiler displays an error. Mistake

What needs to be corrected to complete the task (copy the second word -> paste it into str3)?

  • one
    So you kind of clearly wrote that the buffer is too small. See in debugger how (and where) you go beyond the line. Nobody guesses how many characters you entered in str2 and how long it is for you in the end. Well, there are no psychics here: ( - Max ZS
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky ♦

2 answers 2

You misunderstood the documentation on strcpy_s: the second parameter shows not how much to copy, but how much the target buffer can accept . As a result, you can get a situation where y = 5, and str2 is longer than 5 characters. That is, the buffer cannot accept the string and an exception is thrown.

    As it turned out, everything can be done in the usual algorithmic way.

     //здСсь Π΄ΠΎΠ»ΠΆΠ΅Π½ Π±Ρ‹Ρ‚ΡŒ Ρ„Ρ€Π°Π³ΠΌΠ΅Π½Ρ‚ ΠΊΠΎΠ΄Π° ΠΎΡ‚ Π½Π°Ρ‡Π°Π»Π° Π΄ΠΎ счСтчика ΠΏΡ€ΠΎΠ±Π΅Π»ΠΎΠ² int y = 0; //Π½ΠΎΠΌΠ΅Ρ€ элСмСнта str3 for (i = k; i < l; i++) { str3[y] = str2[i]; y++; } //ΠΊΠΎΠΏΠΈΡ€ΡƒΠ΅ΠΌ 3 слово Π² пустой массив str3[y] = ' '; //добавляСм ΠΏΡ€ΠΎΠ±Π΅Π» y++; for (i = n; i < k; i++) { str3[y] = str2[i]; y++; } //ΠΊΠΎΠΏΠΈΡ€ΡƒΠ΅ΠΌ Π²Ρ‚ΠΎΡ€ΠΎΠ΅ слово y = 0; //обнуляСмся for (i = n; i < l; i++) { str2[i] = str3[y]; y++; } //начиная ΠΎΡ‚ ΠΏΠ΅Ρ€Π²ΠΎΠ³ΠΎ ΠΏΡ€ΠΎΠ±Π΅Π»Π°, замСняСм Π²Ρ‚ΠΎΡ€ΠΎΠ΅ ΠΈ Ρ‚Ρ€Π΅Ρ‚ΡŒΠ΅ слово Π½Π° содСрТимоС str3 puts("\n"); puts(str2); //ΠΈ Π΄Π°, Π½Π΅ Π·Π°Π±Ρ‹Π²Π°ΠΉΡ‚Π΅ ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚ΡŒ puts ΠΈ gets вмСсто printf ΠΈ scanf //ΠΈ Ρ‚.Π΄.