how to get strcat fn? | Sololearn: Learn to code for FREE!

+1

how to get strcat fn?

hello there!! I have been trying to get access on strcat fn but everytime it is giving me error and i have mentioned string and stdio header file but no success can anyone plzz explain it out ☚ī¸đŸ™?

9/27/2021 9:09:10 AM

Iffat

11 Answers

New Answer

+3

The function strcat is a standard function from the C standard library. It is declared in <string.h> for C and <cstring> for C++. This function is used to concatenate C style strings (null terminated char arrays). See: http://www.cplusplus.com/reference/cstring/strcat/

+3

https://code.sololearn.com/cx6px7qwLXX3/?ref=app Slick Aleksei Radchenkov

+3

so true Martin Taylor i thought about it in the c example but neglected to transfer it over https://code.sololearn.com/cgN0708FPDP2/?ref=app

+2

Iffat, the gets() function was deprecated decades ago and was removed from the C and C++ standard years ago. Why are you even using char arrays when C++ has a string class? string str; cin >> str; string str1 = "Hello"; string str2 = "World!"; str1 += " " str1 += str2; cout << str1;

+2

Ifatt and Slick, That's really bad use of strcat. In both of your programs the char arrays str1 and str2 are too small. char str1[10], str2[10]; If I write my first name and surname into the two strings.. str1 contains "Martin" (6 chars + null) str2 contains "Taylor" (6 chars + null) then perform strcat(str1, str2) it will write past the end of the char array str1 contains "MartinTaylor" (12 chars + null) but str1 only has 10 chars. This will result in memory corruption with unpredictable results because the behaviour is undefined. You should not blindly concatenate one char array into another without ensuring that there is sufficient space. Even copying Iffat or Slick twice will exceed the array str1 since both names are 5 chars in length so 10 chars + 1 more for the terminating null char requires a minimum of 11 chars.

+2

Martin Taylor tysm

+2

Slick tysm

+1

Please post the code here

+1

We can't help you unless you post your code here, however one of the most popular problems with strcat() is luck of memory, resulting in segmentation fault: https://stackoverflow.com/questions/13901209/segmentation-fault-strcat

+1

Looking for the same issue. Bumped into your thread. Thanks for creating it. Looking forward for solution. https://www.prepaidgiftbalance.net/

0

i used cin instead of gets spelling mistakes missing colon https://code.sololearn.com/c7Wm6TL6wXQ2/?ref=app