Thread: Concatenating 2 strings

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    141

    Concatenating 2 strings

    Hi,

    This question may be silly but I want to know about any fucntion that concatenates 2 strings without changing any of them!
    strcat changes the destination string.
    I need something like dest = src1+src2.

    Thanks,
    Angkar

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    strcat does not change the string, per se. The first character of str2 overwrites the null character in str1, and appends the rest of the string with a null character at the end.

    There's not really a way around it. Other C functions and stuff depend on the null character to flag the end of the string. In the case of strings, that's all null is there for.
    Last edited by whiteflags; 04-23-2006 at 05:20 PM.

  3. #3
    Registered User 00Sven's Avatar
    Join Date
    Feb 2006
    Posts
    127
    strcat It takes two arguments. The first is the one to be copied to. The second one is what you want added onto the end. So
    Code:
    strcat(string,"test");
    Adds "test' to the end of string. it returns a pointer to the string with the new ending.
    ~Sven
    Last edited by 00Sven; 04-23-2006 at 05:24 PM.
    Windows XP Home Edition - Dev-C++ 4.9.9.2
    Quote Originally Posted by "The C Programming Language" by Brian W. Kernignhan and Dennis M. Ritchie
    int fflush(FILE *stream)
    On an output stream, fflush causes any buffered but unwritten data to be written; On an input stream, the effect is undefined. It returns EOF for a write error, and zero otherwise. fflush(NULL) flushes all output streams.
    board.theprogrammingsite.com

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    So, any suggestion how to keep both the strings as they are and generate a new string that is S1 + S2?

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I think you are trying to concatenate a string that is too big. The first array has to be big enough to fit both of the strings:
    Code:
    char bigstr[80] = strcat( "Pretend I am a really, really long", " run-on sentence." );
    puts(bigstr);
    Last edited by whiteflags; 04-23-2006 at 05:43 PM.

  6. #6
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    Nope I'm using a string in loop...say "Hi"

    So I'm getting :

    HiMatt
    HiMattChris
    HiMattChrisKellie
    .
    .
    .
    as o/p instead of
    HiMatt
    HiChris
    HiKellie
    .
    .
    .

  7. #7
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Code:
    char src1[] = "";
    char src2[] = "";
    char dest[sizeof(src1) + sizeof(src2) - 1];
    
    strcpy(dest, src1);
    strcat(dest, src2);
    Note: sizeof should be a constant expression, but still my compiler doesn't like it so change if necessary.

  8. #8
    Registered User 00Sven's Avatar
    Join Date
    Feb 2006
    Posts
    127
    The only way to do this would be to copy the string that you want something to be added to and then use strcat.
    Code:
    char str1[10] = "Original";
    char str2[10] = "with new ending";
    char final[20];
    char temp[10];
    strcpy(temp,str1);
    final = strcat(temp, str2);
    ~Sven
    Windows XP Home Edition - Dev-C++ 4.9.9.2
    Quote Originally Posted by "The C Programming Language" by Brian W. Kernignhan and Dennis M. Ritchie
    int fflush(FILE *stream)
    On an output stream, fflush causes any buffered but unwritten data to be written; On an input stream, the effect is undefined. It returns EOF for a write error, and zero otherwise. fflush(NULL) flushes all output streams.
    board.theprogrammingsite.com

  9. #9
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Quote Originally Posted by 00Sven
    Code:
    char final[20];
    final = strcat(temp, str2);
    Since final becomes a constant char pointer here this will cause an error.

  10. #10
    Registered User 00Sven's Avatar
    Join Date
    Feb 2006
    Posts
    127
    You can also just use strcat(temp,str2) wherever you would use the variable.
    ~Sven
    Windows XP Home Edition - Dev-C++ 4.9.9.2
    Quote Originally Posted by "The C Programming Language" by Brian W. Kernignhan and Dennis M. Ritchie
    int fflush(FILE *stream)
    On an output stream, fflush causes any buffered but unwritten data to be written; On an input stream, the effect is undefined. It returns EOF for a write error, and zero otherwise. fflush(NULL) flushes all output streams.
    board.theprogrammingsite.com

  11. #11
    Dump Truck Internet valis's Avatar
    Join Date
    Jul 2005
    Posts
    357
    There is no need for a temporary string, strcat(dest, src) appends src to the end of dest, which it sees as the null terminator, which is 0. So if your dest string is "hi" just set dest[2] = 0.

    For example:
    Code:
    int i;
    char hi[32] = "hi";
    char *hiList[] = {" bob", " janet", " jim"};
    
    for (i = 0; i < 3; i++)
    {
        hi[2] = 0;
        printf("%s\n", strcat(hi, hiList[i]));
    }

  12. #12
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152
    Perhaps a sprintf?

    Code:
    {
       sprintf( new_string, "%s%s", str1, str2 );
    }

  13. #13
    Registered User
    Join Date
    Feb 2006
    Location
    Great India
    Posts
    24
    What i have learned after looking at this long discussion is that AngKar needs a function that can concatenate two strings in third string ? That means the original strings should not be changed.

    OK try the following code :

    Code:
    #include<stdio.h>
    #include<conio.h>
    
    int main()
    {
    
    	char a[20],b[20],result[40];
    	int i=0,j=0;
    	clrscr();
    	printf("\n Enter first string less than 20 chars\t");
    	gets(a);
    	printf("\n Enter second string less than 20 chars \t");
    	gets(b);
    	for(i=0;a[i]!='\0';i++)
    	{
    		result[i]=a[i];
    	}
    	result[i]=' ';
    	i=i+1;
    	for(j=0;b[j]!='\0';j++,i++)
    	{
    		result[i]=b[j];
    	}
    	result[i]='\0';
    
    	printf("\n Final string is \t%s",result);
    	getch();
    
    	return 0;
    }
    
    
    OUTPUT :
    
    
    Enter first string less than 20 chars    Hi i am NAPSTER
    Enter second string less than 20 chars	 I am a new member	
    
    FINAL STRING IS		Hi i am NAPSTER I am a new member
    Now you can change the ARRAY size as desired by you.


    REALNAPSTER

  14. #14
    Registered User 00Sven's Avatar
    Join Date
    Feb 2006
    Posts
    127
    OK a few things are wrong with that. First of all you should never use gets. Replace
    Code:
    gets(a);
    with
    Code:
    fgets(a,sizeof(a),stdin);
    fgets takes 3 arguments. First is the same as the argument of gets. Second is the number of characters or the size of input to get. The last one is the stream to read from, in this case it is stdin. fgets will not overflow an array like gets will.
    Next thing to fix is that you do not need to do all of the for loops and stuff. Just change that all to-
    Code:
    int main(void){
         printf("Enter a string:");
         fgets(a,sizeof(a),stdin);
         printf("Enter another string:");
         fgets(b,sizeof(b),stdin);
         sprintf(result,"%s%s",a,b);
         printf("Final string is\t%s",result);
         getchar();
         return 0;
    }
    sprintf is like printf but it prints it to a string in the first argument.
    ~Sven
    Last edited by 00Sven; 04-24-2006 at 12:55 PM.
    Windows XP Home Edition - Dev-C++ 4.9.9.2
    Quote Originally Posted by "The C Programming Language" by Brian W. Kernignhan and Dennis M. Ritchie
    int fflush(FILE *stream)
    On an output stream, fflush causes any buffered but unwritten data to be written; On an input stream, the effect is undefined. It returns EOF for a write error, and zero otherwise. fflush(NULL) flushes all output streams.
    board.theprogrammingsite.com

  15. #15
    Registered User
    Join Date
    Feb 2006
    Location
    Great India
    Posts
    24
    Thanks 00seven for telling this new concept to me. IT's really cool to learn something from a agemate.


    REALNAPSTER

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Easier way of concatenating a lot of strings?
    By Evenstevens in forum C Programming
    Replies: 2
    Last Post: 05-09-2009, 01:29 PM
  2. Concatenating C style Strings Question
    By bengreenwood in forum C++ Programming
    Replies: 3
    Last Post: 03-19-2009, 03:19 PM
  3. concatenating strings?
    By liri in forum C Programming
    Replies: 11
    Last Post: 08-24-2007, 05:34 PM
  4. concatenating more than 2 strings
    By Grunt12 in forum C Programming
    Replies: 3
    Last Post: 11-25-2005, 05:31 PM
  5. Concatenating strings (dynamic array using pointers)
    By Tankndozer in forum C Programming
    Replies: 8
    Last Post: 07-01-2004, 07:27 AM