Thursday 14 May 2015

How to concatenate two strings?

The following function will concatenate two strings and return the concatenated strings:

char* strcat(char* str1,char* str2)
{
             int len=0,i=0;
             while(str1[len])     //until a null character founds
                       ++len;

              while(str2[i])         //until a null character founds
              {
                          str1[len]=str2[i];
                          ++i;
                          ++len;
               }
           
               str1[len]='\0';       //terminate the string
               return str1;
}
                 
for e.g.:

strcat("hello","hi");

will return :

hellohi
             

No comments:

Post a Comment