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
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.
strcat (into, from); correct. into (strcat, from); incorrect. strcat (from, from); incorrect. ....
strcat
Function. Use the built-in help for details.
Language dependent. In C, for example, there no string as such, but you can use function strcat to concatenate zero-terminated character-sequences.
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.
strcat (into, from); correct. into (strcat, from); incorrect. strcat (from, from); incorrect. ....
strcat
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).
Function. Use the built-in help for details.
strcat()
#include <stdio.h> #include <stdlib.h> 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; }
strcat if u wnt to use strcat then include string.h header file
Language dependent. In C, for example, there no string as such, but you can use function strcat to concatenate zero-terminated character-sequences.
Win/Dos: copy file1+file2 tofile unix: cat file1 file2 >tofile
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.
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);
#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