Your task in this assignment is to complete your own versions of these four most
ID: 662374 • Letter: Y
Question
Your task in this assignment is to complete your own versions of these four most commonly used standard string functions. (C++)
Your version:
int mystrlen( const char *s)
int mystrcmp( const char *s1, const char *s2)
char *mystrcpy( char *s1, const char *s2)
char *mystrcat( char *s1, const char *s2)
Each of your functions should have the same behavior as the corresponding above C standard string function. I was giving a .cpp file and odnt know where to start
C standard string library functions:
int strlen( const char *s)
int strcmp( const char *s1, const char *s2)
char *strcpy( char *s1, const char *s2)
char *strcat( char *s1, const char *s2)
Your version:
int mystrlen( const char *s)
int mystrcmp( const char *s1, const char *s2)
char *mystrcpy( char *s1, const char *s2)
char *mystrcat( char *s1, const char *s2)
Each of your functions should have the same behavior as the corresponding above C standard string function. I was giving a .cpp file and odnt know where to start
C standard string library functions:
int strlen( const char *s)
int strcmp( const char *s1, const char *s2)
char *strcpy( char *s1, const char *s2)
char *strcat( char *s1, const char *s2)
Explanation / Answer
strings the header files defines several functions to manipulate Cstrings.
strcpy - is a function used to copy strings
strcat - is used for concatenation process to concatenate strings
strcmp - is used for comparision it compare two blocks of memory
strlen - is used to get string length
A C-string is a very simple and an array of n characters it represents a strings upto n-1 characters.The characters in string are stored sequentially from begining of array,followed by a binary 0 char.
String operations must strictly respect the '' terminator.Which follows even if it's part of allocated array, is completely undefined.In extreme edge cases,even reading next character beyond '' could cause a segment fault.With that warning in mind,functions are quite simple.
Strcpy() copies chars upto including '' in source string.
Strlen count chars upto,but it doesn't includes '' in argument string.
strcmp is more intresting.
A little bit of though should show you that you can compare chars at the same index position and proceed until either and unequal compare occurs, or first argument string char is ''.A little more is that it should convince you that it is better in this to compare for equality, and now test for terminator even though that's backward from normal practice with C strings.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.