C program for String Concatenation using strcat function

The strcat() function appends the src string to the dest string, overwriting the terminating null byte (‘\0’) at the end of dest, and then adds a terminating null byte. The strings may not overlap, and the dest string must have enough space for the result. If dest is not large enough, program behavior is unpredictable
The strcat() and strncat() functions return a pointer to the resulting string dest.

The declaration of strcat looks like,

 $ char *strcat(char *dest, const char *src); 
 $ vim strcat_test.c 
#include <stdio.h>
#include <string.h>

int main(void) {
        char str[128]="hello";
        printf("First string: \"%s\" : length:%d \n", str, strlen(str));
        printf("String to append: \"%s\" : length:%d \n", "world", strlen("world"));
        printf("Concatnated string: %s \n", strcat(str, "world"));

        printf("strlen of final string = %d\n", strlen(str));
        return 0;
}
 $ $ gcc -o strcat_test strcat_test.c 
 $ ./strcat_test

First string: "hello" : length:5 
String to append: "world" : length:5 
Concatnated string: helloworld 
strlen of final string = 10

Android Android Commands Android Java Applications Application Libraries Application Stack / User Interface Bash / Shell Scripts Commands and Packages Compilation Content Management System - CMS Core Kernel C Programs Development & Build Development Environment Setup Errors & Failures Flutter git Go Language Programs Hardware Platforms HTML JAVA Programs Kernel & Device Drivers Linux, OS Concepts and Networking Linux Device Drivers Linux Host, Ubuntu, SysAdmin Linux Kernel Linux Networking Middleware Libraries, HAL Monetization / Google AdSense Multimedia - Audio, Video, Images NDK / Middleware / HAL OS Concepts PHP Programming Languages RaspberryPi Scripting and Automation Search Engine Optimisation ( SEO ) Social Media Socurce Code Management ( SCM ) System Administration, Security Testing and Debugging Uncategorized User Interface Web design and development Wordpress Yocto / Bitbake / Openembedded

Leave a Reply / Ask Question