I am new to C++ and I have started to work with strings recently, but I have problems with strcpy_s()
. In Visual Studio if I use the old strcpy()
it said that it is unsafe and after reading more on the internet and I found out why so I started to use no more the strcpy()
.
In strcpy()
, I have to tell it the size of buffer, but with this I have problems,because if I use strlen()
it said that the buffer it is too small, even if I put a=strlen(string)+1
,so I found out about another one called size_t()
,and now I have no more problems with the error related to the buffer, but I got another error.
Code:
#include <iostream>
#include <cstring>
#include <conio.h>
using namespace std;
int main()
{
int i, size_of_word;
char word[202];
cin.get(word, 201, '\n');
cin.get();
i = 0;
while (i < strlen(word) - 1)
{
if (word[i] == word[i + 1])
{
size_of_word = size_t(word + i ) + 1;
strcpy_s(word + i + 2, size_of_word, word + i);
}
else
{
i++;
}
}
cout << word;
}