You can take Two String using either:
char *str1 = "Kapil";
char *str2 = "Thakar";
or
char str1[] = "Kapil";
char str2[] = "Thakar";
Now to concat these two string you need to create another string or you can use the same string. Here I have taken a new string to hold the combined string.
We don't the size of the new concat string, Concat string should have string length bigger than a sum of the string of string 1 & string 2. So we can either take
char str3[20]; //Here 20 is bigger than sum of string 1 & string 2.
or
char *str3 = (char *) malloc (length);
where length is sum of both string 1 & string 2.
Here you can not use
char str3[length] as you need to know the array size at compile time. So If you do like that then it will give you an error.
If we don't do this, then it will result in the segmentation fault. So It is required.
We can now copy the each character from string 1 & then we start copying text from string 2.
At the end, we need to add the null character to terminate the string properly. Else garbage character will be printed on the screen till it will find the null character.
#include "stdafx.h"
#include <conio.h>
#include "stdlib.h"
#include "stdio.h"
#include "string.h"
int main()
{
char *str1 = "Kapil";
char *str2 = "Thakar";
int length = (strlen(str1)+strlen(str2));
char *str3 = (char *) malloc (length);
int i = 0;
for(i=0; i<length; i++)
{
if(i < strlen(str1))
*(str3+i) = *(str1+i);
else
*(str3+i) = *(str2+ (i - strlen(str1)));
}
*(str3+i) = '\0';
printf("\n%d,%s", length,str3);
getch();
return 0;
}
For any Query please feel free to comment.
Related Post:
char *str1 = "Kapil";
char *str2 = "Thakar";
or
char str1[] = "Kapil";
char str2[] = "Thakar";
Now to concat these two string you need to create another string or you can use the same string. Here I have taken a new string to hold the combined string.
We don't the size of the new concat string, Concat string should have string length bigger than a sum of the string of string 1 & string 2. So we can either take
char str3[20]; //Here 20 is bigger than sum of string 1 & string 2.
or
char *str3 = (char *) malloc (length);
where length is sum of both string 1 & string 2.
Here you can not use
char str3[length] as you need to know the array size at compile time. So If you do like that then it will give you an error.
If we don't do this, then it will result in the segmentation fault. So It is required.
We can now copy the each character from string 1 & then we start copying text from string 2.
At the end, we need to add the null character to terminate the string properly. Else garbage character will be printed on the screen till it will find the null character.
#include "stdafx.h"
#include <conio.h>
#include "stdlib.h"
#include "stdio.h"
#include "string.h"
int main()
{
char *str1 = "Kapil";
char *str2 = "Thakar";
int length = (strlen(str1)+strlen(str2));
char *str3 = (char *) malloc (length);
int i = 0;
for(i=0; i<length; i++)
{
if(i < strlen(str1))
*(str3+i) = *(str1+i);
else
*(str3+i) = *(str2+ (i - strlen(str1)));
}
*(str3+i) = '\0';
printf("\n%d,%s", length,str3);
getch();
return 0;
}
For any Query please feel free to comment.
Related Post:
0 comments :
Post a Comment