C Programming Language

strcat()

Syntax:
#include<string.h>
char *strcat( char *str1, const char *str2 );

                    strcat(str1,str2) concatenates a copy of the string pointed to by str2 to the the string pointed to by str1 and terminates str1 with a null. The first character of str2 overwrites the null terminator originally ending sr1. The str2 is untouched by the operation.In this function we will pass two arguments str1,str2 as parameters.

For example, Consider this program


#include<stdio.h>
#include<conio.h>
int main()
{   
    char str1[30]="www.cprogramming";
    char str2[15]="expert.com";         
    clrscr();
    printf("%d\t%d\nn",strlen(str1),strlen(str2));
    printf("\n\nstrcat(str1,str2) : %s\n\n",strcat(str1,str2));
    printf("\n\nstr1 : %s\t\tstr2 : %s\n\n",str1,str2);
    getch();
    return 0;
}

Here
str1[30]= www.cprogramming and str2[15]=expert.com
if we are using function as strcat(str1,str2) and print the output. It will be www.cprogrammingexpert.com.
i.e the last character in the first parameter get erased and second parameter concatenated in memory str1 is stored like this "www.cprogramming\0" and str2 like this "expert.com\0" after concatenation str1 is "www.cprogrammingexpert.com\0" and str2 is"expert.com\0".
So make sure that the size of str1 is large enough to accomodate all the characters in str1 and str2.


str1[0]  <- 'w'
str1[1] <- 'w'
str1[2] <- 'w'
str1[3] <- '.'
str1[4] <- 'c'
str1[5] <- 'p'
str1[6] <- 'r'
str1[7] <- 'o'
str1[8] <- 'g'
str1[9] <- 'r'
str1[10]  <-'a'                   
str1[11]   <-'m'   
str1[12]   <-'m'   
str1[13]   <-'i'   
str1[14]   <-'n'   
str1[15]   <-'g'   
str1[16]  <- '\0' 
str1[17]  <- 'garbage value'
str1[18]  <- 'garbage value'
str1[19]  <- 'garbage value'
str1[20]  <- 'garbage value'
str1[21]  <- 'garbage value'
str1[22]  <- 'garbage value'
str1[23]  <- 'garbage value'
str1[24]  <- 'garbage value'
str1[25]  <- 'garbage value'
str1[26]  <- 'garbage value'
str1[27]  <- 'garbage value'
str1[28]  <- 'garbage value'
str1[29]  <- 'garbage value'

str2 is

str2[0]  <- 'e'
str2[1] <- 'x'
str2[2] <- 'p'
str2[3] <- 'e'
str2[4] <- 'r'
str2[5] <- 't'
str2[6] <- '.'
str2[7] <- 'c'
str2[8] <- 'o'
str2[9] <- 'm'
str2[10]   <- '\0' 
str2[11]  <- 'garbage value'
str2[12]  <- 'garbage value'
str2[13]  <- 'garbage value'
str2[14]  <- 'garbage value'

After concatenation

str1[0]  <- 'w'
str1[1] <- 'w'
str1[2] <- 'w'
str1[3] <- '.'
str1[4] <- 'c'
str1[5] <- 'p'
str1[6] <- 'r'
str1[7] <- 'o'
str1[8] <- 'g'
str1[9] <- 'r'
str1[10]  <-'a'                   
str1[11]   <-'m'   
str1[12]   <-'m'   
str1[13]   <-'i'   
str1[14]   <-'n'   
str1[15]   <-'g'   
str1[16]  <- 'e' 
str1[17]  <- 'x'
str1[18]  <- 'p'
str1[19]  <- 'e'
str1[20]  <- 'r'
str1[21]  <- 't'
str1[22]  <- '.'
str1[23]  <- 'c'
str1[24]  <- 'o'
str1[25]  <- 'm'
str1[26]  <- '\0'
str1[27]  <- 'garbage value'
str1[28]  <- 'garbage value'
str1[29]  <- 'garbage value'

In this example I have declared size of str1 equal to 30 and str2 equal to 15.

Now you may think why I have declared str1 as 30 and str2 as 15. It's just because I want you to undestand the concepts .OK

So if you know exact size of the string declare that size to save memory. Here str1[27] is enough, str2[11] is enough.

One more thing , what will happen if you are declaring the string like char *str1,*str2 , here no garbage values will come.
Before concatenation size of str1 is 16 & str2 is 10 and a fter concatenation size of str1 is 26 & str2 is 10. so we Saved memory.

Example:

strcat() function in c programming  
strupr() Home strncat()

c program stats