Top Answer
User Avatar
Wiki User
Answered 2009-10-13 07:42:14
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];
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)
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

Your Answer


Still Have Questions?

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?


What is strcat?

Function. Use the built-in help for details.

What do you mean by functions in c?

A function is a self-contained block of code that performs a particular task.Once a function has been designed and packed,it can be treated as a "Black Box" that takes some data from the main program and returns a value.The inner details of operation are invisible to the rest of the program.All that the program knows about a function is: What goes in and what comes out.Every C program can be Consider a set of statements.C functions can be classified into 2 categories,namely ,library functions and user-defined functions.main is an example of user-defined functions.printf and scanf belong to the category of library functions.sqrt,cos,strcatThe main distinction between these two categories is that library functions are not required to be written by a programmer whereas a user-defined function has to be developed by the user at the time of writing a program.However, a user-defined function can later become a part of the C program library.

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).

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


What are the String Functions?

String functions are the set of functions that we generally need while playing with strings like: strlen - finds length of the string strcat - for concatenation strcpy - for copying strcmp - for comparing, etc. Thanks

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 String concat program in c without using function?

I guess you mean without using the libraryfunction strcat():/* concatenate srce onto the end of dest*/char *dest;char *srce;.../* do whatever until concatenation wanted, then:*/{char *ptr = dest - 1, *ptr2 = srce;while (*(++ptr));while (*ptr++ = *ptr2++);}

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.

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 a string function why you use string function?

In C, functions exist to perform operations. Some functions manipulate variables in various ways. C itself does not have any functions. Basic functions are provided by libraries, which your own program files will then use. Some of these functions work with strings. Because strings are character arrays, they cannot be acted upon by simple operators. Even while other languages have concatenation operators (+ or .), C requires that you use strcat() or write your own string concatenator to join two strings together. In short, string functions come in handy when you want to work with strings in C. See the related links below for more information on C string functions.

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.

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

C program to concatenate two strings through pointers?


Write a program in C to concatenante 2 strings?

Here is very small application written in C, that concatenates strings.#include #include int main() {char str[100];strcpy(str, "Hello ");strcat(str, "World!");printf("%s\n", str);return 0;}The result is "Hello World!".Concatenation is done by function strcat which is defined in string.h.

What is the C program for bit stuffing?

#include#includemain(){char a[20],fs[50]="",t[6],r[5];int i,j,p=0,q=0;clrscr();printf("enter bit string : ");scanf("%s",a);strcat(fs,"01111110");if(strlen(a){strcat(fs,a);}else{for(i=0;i{for(j=i;j{t[p++]=a[j];}t[p]='\0';if(strcmp(t,"11111")==0){strcat(fs,"111110");i=j-1;}else{r[0]=a[i];r[1]='\0';strcat(fs,r);}p=0;}for(q=i;q{t[p++]=a[q];}t[p]='\0';strcat(fs,t);}strcat(fs,"01111110");printf("After stuffing : %s",fs);getch();}

Program in c to implement character stuffing?

// BY: BIBHAKAR JHA // Objective : to implement character stuffing in c //PROGRAM CODE : #include&lt;stdio.h&gt; #include&lt;conio.h&gt; void main() { int i, j, p=0, q=0; char ary1[50], ary2 [50]=" ", a[6], b[5], char1; clrscr(); printf("\n Please Enter bit sequence :\n"); scanf("%s", &amp;ary1); strcat(ary2, "01111110"); if(strlen(ary1)&lt;5) { strcat(ary2, ary1); } else { for(i=0; i&lt;strlen(ary1)-4; i++) { for (j=1; j&lt;i+5; j++) { a[p++] = ary1[j]; } a[p]='\0'; if(strcat(a, "11111") ==0) { strcat(ary2, "111110"); i=j-1; } else b[0] = ary1[i]; b[1] ='\0'; strcat(ary2, b); } } for(q=i; q&lt;strlen(ary1); q++) { a[p++] =ary1[q]; } a[p] ='\0'; strcat(ary2,a); strcat(ary2,"01111110"); printf("\n if string after stutffing: %s", ary2); getch(); }

What is the task of dll and ocx in programming?

Dll is "dynamic link library" it provides all the information about built in methods which we used in program at run time. these could be keywords(main, if , for etc) or functions( pow(), main(), strcat() etc) or classes in opps. An OCX is an Object Linking and Embedding (OLE) custom control, a special-purpose program that can be created for use by applications running on Microsoft's Windows systems.

To write a program in C to concatenate two strings?

You can use functions like strcat or sprintf, or do it manually: q= to; p= from1-1; while (*++p) *q++= *p; p= from2-1; while (*++p) *q++= *p; *q= '\0';

C prog to concatenate two string without using buid in function?

//String Concatination#include#includeusing namespace std;char* strcat(char*,char*);int main(){char str1[100];char str2[100];coutcin>>str1;coutcin>>str2;coutgetch();return 0;}char* strcat (char* frst, char* scnd){char* rslt = frst;while (*frst++ != '\0');frst--;while ((*frst++ = *scnd++) != '\0');return rslt;}

Write a function for concatnating the string s1 s2 and storing the result in string s3?

char *strmerge (char *s3, const char *s1, const char *s2) { strcpy (s3, s1); strcat (s3, s2); return s3; }

To copy two strings using strcpy?

Using strcpy and strcat. Or sprintf. Or strlen+memcpy. There are more than solutions.