CONCATENATION OF TWO STRINGS USING C PROGRAM






Concatenation of two strings in c programming language

#include<stdio.h>
int main(){
  int i=0,j=0;
  char str1[20],str2[20];
  puts("Enter first string");
  gets(str1);
  puts("Enter second string");
  gets(str2);
  printf("Before concatenation the strings are\n");
  puts(str1);
  puts(str2);
  while(str1[i]!='\0'){
      i++;
  }
  while(str2[j]!='\0'){
      str1[i++]=str2[j++];
  }
  str1[i]='\0';
  printf("After concatenation the strings are\n");
  puts(str1);
 return 0;
}





3. Write a c program to delete the all consonants from given string.

15 comments:

Anonymous said...

It was i think a bit lengthy method..
you could have used for loop..

Priyanka kumari said...

To concatenate two string you can use function : strncat
It has been defined inside string.h

Unknown said...

i am stisfied with your explanation.

Anonymous said...

could u pls tel me how to do widout usin puts n gets??????

Anonymous said...

i am satisfied...

Unknown said...
This comment has been removed by the author.
Anonymous said...

Thanks...i am satisfied......

Anonymous said...

void main()
{
char str1[30], str2[10];
int i, j;
clrscr();
printf("Enter first string: ");
gets(str1);
printf("Enter second string: ");
gets(str2);
for(i = 0; str1[i]; i++); // to find null of str 1
for(j = 0; str2[j]; j++)
str1[i+j] = str2[j];
str1[i+j] = 0;
printf("The concatenated string is %s\n", str1);
getch();
}

Anonymous said...

char array2[250], array1[250]="hellobrother";
int i,j=0;
for(i=5;i<=250;i++)
{
array2[j]=array1[i];
j++;
}
puts(array2);

//using pointer for

char str2[250]="how r u felling today";
char *ptr=str2;
int i,j;
printf("%u \n",ptr);

while(*ptr!='f')
{
ptr++;
}
printf("%u \n",ptr);
for(int x=0;ptr[x]!='\0';x++)
{
printf("%c",ptr[x]);
}

Unknown said...

write

Unknown said...

#include
#include

void main()
{
int i=0,j=0;
char str1[20],str2[20];

puts( "Enter first string=");
gets(str1);

puts( "Enter second string=");
gets(str2);

while (str1[i]!='\0')
{
i ;
}

while (str2[j]!='\0')
{
str1[i ]=str2[j ];
}
str1[i]= '\0';

puts(str1);
getch();
}

Unknown said...
This comment has been removed by the author.
Unknown said...
This comment has been removed by the author.
Rani banga said...

I have a doubt that in d 2nd while loop if we have done str2[j++] then how would the first character of str2 be included in str1

Rani banga said...

I have a doubt that in d 2nd while loop if we have done str2[j++] then how would the first character of str2 be included in str1