answersLogoWhite

0


Best Answer
Run through the char array

Though i cannot think of a reason do to it (why not use the available methods in the string.h ?) , it is possible to do it manually.
Loop through your character arrays and compare / copy.
Suppose you are char orig[100];
strcmp:
for (int i=0; orig[i] != 0; i++)
if (orig[i] != other[i]) return false;
return true; // because reached the end of the string.

and copy will be similar:
first get the length:
int length = 0;
for (int i=0; orig[i] !=0; i++, length++);
char *other = new char[length];
for (int i=0; i < length; i++)
other[i] = orig[i]; // will also copy the \0


AnswerWriting your own string functions is useful mainly for the challenge or practice. The versions that come with your compiler are most likely well optimized for your system, so you should not consider writing your own for performance reasons. That said, strcmp for one is simple enough to write:

int strcmp(const char *a, const char *b)
{
do
if (*a != *b) return *a - *b;
while (a++, *b++);

return 0;

}

strcpy is even simpler:

char *strcpy(char *dest, const char *src)
{
char *s = dest;
while (*dest++=*src++)
;

return s;

}

strcat is similar to strcpy, but it first finds the end of the dest string before copying the src string to it. I'll leave that as an exercise to the reader.

User Avatar

Wiki User

14y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How to write your own function for standard library functions strcat strcpy strcmp?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is the correct call to the library function strcat?

strcat (into, from); correct. into (strcat, from); incorrect. strcat (from, from); incorrect. ....


What is concatenate function in c?

strcat


What is strcat?

Function. Use the built-in help for details.


What symbol is use to join strings?

Language dependent. In C, for example, there no string as such, but you can use function strcat to concatenate zero-terminated character-sequences.


Which function joins two arrays of character in c language?

C provides the strcat() function, and its prototype is:char *strcat (char *destination, const char *source);destination must have enough memory allocated for itself and source. If you want to join two character arrays into a newly allocated string, use the following function:char *strcombine(char *str1, char *str2){/* size_t is a standard size or length integer type */size_t len;/* this is the pointer to the new string */char *returnstr;/* figure out the new string length */len=strlen(str1)+strlen(str2)+1;/* allocate memory for the return string and init to a length of 0 */returnstr=(char*)malloc(len);*returnstr=0;/* concatenate str1 and str2 to returnstr */strcat(returnstr, str1);strcat(returnstr, str2);/* return the new string */return returnstr;}Note that typecasting the malloc() return value as (char*) is just a formality under C; under C++, however, it's required to avoid warnings.Also, the above function does not test to see if str1and/or str2 are NULL values.

Related questions

What is the correct call to the library function strcat?

strcat (into, from); correct. into (strcat, from); incorrect. strcat (from, from); incorrect. ....


What is concatenate function in c?

strcat


What are built-in functions in c programs?

There are no built-in functions in C as such. What we call built-in functions are actually part of the C standard function library, which is just a function library like any other, but one that ships with all implementations of C. The functions we specifically regard as being built-in are those functions that do not require us to include any specific library headers. These functions are imported by default, hence they are all considered built-in. They are as follows: The string management functions (strcpy, strncpy, strcmp, strncmp, strlen, strcat, strncat, strchr, strrchr, strstr and strtok), memory management functions (malloc, calloc, realloc and free), buffer manipulation functions (memcpy, memcmp, memchr, memset and memmove), character functions (isalnum, isalpha, iscntrl, isdigit, isgraph, islower, isprint, ispunct, isspace, isupper, isxdigit, tolower and toupper) and error handling functions (perror, strerror).


What is strcat?

Function. Use the built-in help for details.


Which function joins one string with another to produce single string that contains both?

strcat()


Random sentence generator in c plus plus?

#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; const char *noun[]={"boy", "girl", "dog", "town", "car"}; const char *verb[]={"drove", "jumped", "ran", "walked", "skipped"}; const char *preposition[]={"to", "from", "under", "over", "on"}; const char *article[]={"the", "a", "one", "some", "any"}; char sentence[50]; int main(void) { strcat(sentence, *(article+(rand() %5))); strcat(sentence, " "); strcat(sentence, *(noun+(rand() %5))); strcat(sentence, " "); strcat(sentence, *(verb+(rand() %5))); strcat(sentence, " "); strcat(sentence, *(preposition+(rand() %5))); strcat(sentence, " "); strcat(sentence, *(article+(rand() %5))); strcat(sentence, " "); strcat(sentence, *(noun+(rand() %5))); printf("%s",sentence); getch(); return 0; }


How do you concatenate strings in C programming?

strcat if u wnt to use strcat then include string.h header file


What symbol is use to join strings?

Language dependent. In C, for example, there no string as such, but you can use function strcat to concatenate zero-terminated character-sequences.


Concatenate two files without using strcat function?

Win/Dos: copy file1+file2 tofile unix: cat file1 file2 &gt;tofile


Which function joins two arrays of character in c language?

C provides the strcat() function, and its prototype is:char *strcat (char *destination, const char *source);destination must have enough memory allocated for itself and source. If you want to join two character arrays into a newly allocated string, use the following function:char *strcombine(char *str1, char *str2){/* size_t is a standard size or length integer type */size_t len;/* this is the pointer to the new string */char *returnstr;/* figure out the new string length */len=strlen(str1)+strlen(str2)+1;/* allocate memory for the return string and init to a length of 0 */returnstr=(char*)malloc(len);*returnstr=0;/* concatenate str1 and str2 to returnstr */strcat(returnstr, str1);strcat(returnstr, str2);/* return the new string */return returnstr;}Note that typecasting the malloc() return value as (char*) is just a formality under C; under C++, however, it's required to avoid warnings.Also, the above function does not test to see if str1and/or str2 are NULL values.


Why strcat(string'!') not work in C program?

The strcat() function has the following protocol:char* strcat (char* destination, char* source);The function appends the source string to the destination string and returns the destination string.The destination string must be a null-terminated character array of sufficient length to accommodate strlen (source) plus strlen (destination) characters, plus a null-terminator. The existing null-terminator and subsequent characters of destination are overwritten by characters from the source string, up to and including the source string's null-terminator.strcat (string, '!') will not work because '!' is a character literal (ASCII code 33 decimal), not a null-terminated character array. Use "!" instead of '!'.Example:char string[80]; // character arraystrcpy (string, "Hello world");strcat (string, "!");puts (string);


What is header function in c language?

#include- Standard Input /Output Functionsclearerr()fclose()feof()ferror()fflush()fgetc()fgetpos()fgets()fopen()fprintf()fputc()fputs()fread()freopen()fscanf()fseek()fsetpos()ftell()fwrite()getc()getchar()gets()perror()printf()putc()putchar()puts()remove()rename()rewind()scanf()setbuf()setvbuf()sprintf()sscanf()tmpfile()tmpnam()ugetc()vfprintf()vprintf()vsprintf()#include-Standard Mathematical Functionsabs()acos()asin()atan()atan2()ceil()cos()cosh()div()exp()fabs()floor()fmod()frexp()labs()ldexp()ldiv()log()log10()modf()modf()pow()sin()sinh()sqrt()tan()tanh()#include- Standard String Handling Functions-charactersatof()atoi()atol()isalnum()isalpha()iscntrl()isdigit()isgraph()islower()isprint()ispunct()isspace()isupper()isxdigit()memchr()memcmp()memcpy()memmove()memset()strcat()strchr()strcmp()strcoll()strcpy()strcspn()strerror()strlen()strncat()strncmp()strncpy()strpbrk()strrchr()strspn()strstr()strtod()strtok()strtol()strtoul()strxfrm()tolower()toupper()#include-Date and Time Functionsasctime()clock()ctime()difftime()gmtime()localtime()mktime()strftime()time()#include- Memory Functionscalloc()free()malloc()realloc()#include-(Other Functions-exit())#include-(Other Functions-Standard Library Functions)C standard library