Saturday, 28 January 2017

String concatenation in C using strcat & pointers – 2 programs ~ mkniit ~ gniitsolution

String concatenation in C programming language using strcat & pointers. This is to concatenate the given input strings as one. It is a simple concatenation source code that generates the final output.
Here we have included the 2 example programs. They are,
  • String concatenation using strcat,
  • String concatenation using pointers

STRING CONCATENATION IN C

This C program is used to concatenate the two string values into the third string and print the value of string and combined two strings by using the strcat function & pointers. The header file is #include<string.h>. For example first value is ‘string’ and second value is ‘concatenation’ after strcat it will concatenate and stored in third value as ‘string concatenation’.

String concatenation codes:

1) C program for string concatenation using strcat function.
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
//Header files
#include <stdio.h>
#include <string.h>
 
int main()
{
   //Program variable
   char b[50], c[50];
 
   printf("Enter the first value of string\n");//Display function
   gets(b);//Getting input function
 
   printf("Enter the second value of string\n");
   gets(b);
 
   strcat(b,c);//concatenation function
 
   printf("After string  concatenation is %s\n",b);
 
   return 0;
}
2) Program in string concatenation using pointers
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//Header files
#include<stdio.h>
#include<conio.h>
 
char *xs(char*,char*);//Global variable
void main()
{
char *s4,*s5,*s6;//Program variable
clrscr();
puts("\t enter the first value of string:\n");
gets(s4);
puts("\t enter the second value of string:");
gets(s5);
s6=xs(s4,s5);
printf("after concatenation of string is %s",s6);
getch();
}
 
char *xs(char *s1,char *s2)
{
    char *p=s1;
    /*int j,h=0;
    while(*(s1+h)!='\0')
    {
        h++;
 
    }
    j=h;
    h=0;
    while(*(s2+h)!='\0')
    {
        *(s1+j)=*(s2+h);
        h++;
        j++;
 
    } */
 
    while(*s1!='\0')
        s2++;
 
    while(*s1!='\0')
    {
        *s1=*s2;
        s1++;
        s2++;
    }
 
    *s4='\0';
    //fs4--;
 
    return (p);
}

    1 comment:

    1. awesome post for those who loves c language for such info visit: kidsfront.com

      ReplyDelete