Before me I had a simple task in the given line to insert a number of spaces between words so that the length of the line was 60 characters. But when you start, you get the error "stack around the variable 's' was corrupted". Who can tell what the problem is?
#include "stdafx.h" #include<iostream> #include<string.h> using namespace std; void change(char arr[61][130], char s[61], int i) { int k = 0; while (strlen(s)<60) { if (k == i - 1) k = 0; strcat_s(arr[k], sizeof arr[k], " "); memset(s, 0, sizeof(s)); for (int j = 0; j < i; j++) strcat_s(s, sizeof arr[j], arr[j]); k++; } printf("%s","A new string: "); printf("%s\n", s); } void break_on_array(char s[61]) { char*p; char*np = NULL; char*delim = " "; int i = 0; char arr[61][130]; p = strtok_s(s, delim, &np); while (p != NULL) { strcpy_s(arr[i], sizeof arr[i], p); p = strtok_s(NULL, delim, &np); i++; } change(arr, s, i); } int main() { setlocale(LC_ALL, "rus"); char s[61]; printf("%s\n", "Введите строку: "); gets_s(s); break_on_array(s); return 0; }
iostreamnamespace std? ... - Harry#include<iostream>is C ++. But in C ++ you can'tchar*delim = " ";. - AnTchar*delim = " ";". Must beconst char *delim = " ";? - wololochar *specially for such cases. In C ++ 11, this conversion has been removed, but some compilers still support it in compatibility mode. This, however, is no reason to do so in C ++ code. Yes, and in the C code you should avoid pointing to string literals throughchar *. And ideone is generally a profanation. coliru.stacked-crooked.com/a/c328322d8058fb56 - AnT