Home C-tutorials C String Concatenation
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

C String Concatenation

Advertisement
In this section you will study how to concatenate the two strings in C. You can see in the given example, we have declared two strings in order to combine the strings.

C String Concatenation

     

In this section you will study how to concatenate the two strings in C. You can see in the given example, we have declared two strings in order to combine the strings. For this, we have used the library function strcat() provided by the header file <string.h>. This method takes two char arguments and returns the concatenated string as a char.  

Here is the code:

STRINGCO.C

#include <stdio.h>
#include <conio.h>
#include <string.h>
int main() {
  char st1[] "RoseIndia";
  char st2[] "Technologies";
  strcat(st1, st2);
  printf("The Concatenated string is: %s\n", st1);
  getch();
  return 0;
}

Output will be displayed as:

STRINGCO.EXE

Download Source Code

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: February 6, 2009

Related Tutorials

Ask Questions?    Discuss: C String Concatenation   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
hasan
September 20, 2011
i phone

i phone is my best sub.
Amateur C Programmer
October 27, 2011
Huge security problem and bad memory usage

strcat uses the memory allocated by str1, it does not create more memory to use. The code here will overwrite other memory not owned by the program.
Ganixra
November 1, 2011
I need string comparison of c

As i need comparison of given two strings with the list of array of elements. please help me
subha das aadhikary
November 5, 2011
BCA

#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; }
hyderabad
January 4, 2012
string cacatenation

give more examples to string concatenation.thanks in advance...
sahiba
May 27, 2012
computer science and engineering

I think dis iz the best and easy way for understanding CONCATINATION.........
tr0ll
July 4, 2012
strcat

does it add a space? :P
carel
July 26, 2012
small error

In your code example, the output will not have a space between "RoseIndia" and "Technologies"... regards Carel
Coolio
June 4, 2014
This program is a faulty, please remove

For the love of god, remove this horrible example of a buffer overflow.
DMCA.com