Write a program in C using strcat function.
Answers
⭐char strg1[40] = "Hello";
/*
returns a pointer (which is discarded) to the string literal
"Hello World" and now strg1 contains "Hello World"
*/
strcat(strg1, " World");
/*
returns a pointer (which is discarded) to the string
to "Hello World :)" and now strg1 contains "Hello World :)"
*/
strcat(strg1, " :)");
strcat("Yello", " World"); // wrong
#include<stdio.h>
#include<string.h>
int main()
{
char strg1[40];
char strg2[40];
printf("Enter first string: ");
gets(strg1);
printf("Enter second string: ");
gets(strg2);
printf("\nConcatenating first and second string .. \n\n");
strcat(strg1, strg2);
printf("First string: %s\n", strg1);
printf("Second string: %s", strg2);
// signal to operating system program ran fine
return 0;
}
char *my_strcat(char *strg1, char *strg2)
{
char *start = strg1;
while(*strg1 != '\0')
{
strg1++;
}
while(*strg2 != '\0')
{
*strg1 = *strg2;
strg1++;
strg2++;
}
*strg1 = '\0';
return start;
}
Program in "C" using "strcat" Function
#include <stdio.h>
#include <string.h>
main()
{
char s1[20], s2[20];
printf("\nEnter first string: ");
gets(s1);
printf("\nEnter second string: ");
gets(s2);
strcat(s1, s2);
printf("\nThe concatenated string is: %s", s1);
getch();
}
The output will be as follows:
Enter first string: Programming
Enter second string: .com
The concatenated string is: Programming.com