String Concat

This is a discussion on String Concat within the C Programming forums, part of the General Programming Boards category; Hi, I am trying to write a program to concat two strings. Its a simple one, but i am unable ...

  1. #1
    Registered User Silverdream's Avatar
    Join Date
    Feb 2002
    Posts
    53

    Unhappy String Concat

    Hi,
    I am trying to write a program to concat two strings. Its a simple one, but i am unable to indentify the error. Please help.

    #include <stdio.h>

    int main()
    {
    char s1[80],s2[80],s3[80];
    int len1,len2;
    printf("Enter the first string\n");
    scanf("%s",&s1);
    printf("\nEnter the second string to be appended to the first\n");
    scanf("%s",&s2);
    len1=strlen(s1);
    len2=strlen(s2);
    xstrcmp(s1,s2,len1,len2);
    }

    xstrcmp(char *s1,char *s2,int len1,int len2)
    {
    int i;
    s1=s1+len1;
    for(i=0;i<=len2;i++)
    {
    *s1=*s2;
    s1++;
    s2++;
    }
    printf("\nThe concatenated string is %s",s1);
    getch();
    }

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    Should
    scanf("%s",&s1);
    be
    scanf("%s",s1);?
    as a string is a pointer unless given an array ref

    Also some error check
    ie if(len1+len2<=(80-1))//if both strings are less than the total space

    I'm not sure about
    s1=s1+len1

    how about
    char *pS1,*pS2;

    pS1=s1[len1];//set to the end inc '\0'
    pS2=s2[0];//set to start
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    Registered User Silverdream's Avatar
    Join Date
    Feb 2002
    Posts
    53

    Unhappy

    No it still doesnt work, even without '&'.

  4. #4
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    Cool

    At first let me tell you, that you must get a book .

    Now let's go to the errors:

    1) You must include "<conio.h>" if you want to use "getch()" command and "<string.h>" if you want to use "strlen" command;

    2) you need a prototype for you function. In this case it would be "void xstrcmp (char *s1, char *s2, int len1, int len2);";

    3) main function must return 0 and you do not need s3[80];

    4) when you are entering string in scanf you must always write it like that "scanf("%s", string);";

    I write my own function for this. It's a little longer but it works.
    Try it !

    Code:
    void xstrcmp(char *s1, char *s2, int len1, int len2) 
    { 
    	char ConStr[80], *PtrConStr;
    
    	if (len1 + len2 < 80) // this is for error checking, two strings together must not exceed 79 characters
    	{
    		PtrConStr = ConStr; // you must "connect" pointer with the string (this is a direction of a pointer)
    		while (*s1 != '\0') // with this while loop I write from s1 into ConStr
    		{
    			*PtrConStr = *s1;
    			PtrConStr++;
    			s1++;
    		}
    		*PtrConStr = '\0'; // you must add this - it is defined as the end of the string
    		while (*s2 != '\0') // and with this while loop I write from s2 into ConStr
    		{
    			*PtrConStr = *s2;
    			PtrConStr++;
    			s2++;
    		}
    		*PtrConStr = '\0'; 
    		printf("\nThe concatenated string is %s", ConStr);
    	}
    	else printf("\nStrings are too long.");
    }
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  5. #5
    Registered User Silverdream's Avatar
    Join Date
    Feb 2002
    Posts
    53

    Talking

    Thanx a lot for the help once again. I am trying to read books but none of them give information in detail. Is there any book which goes into details(depth).

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    88
    mhm, pretty inefficient...

    try this one:

    Code:
    char* xstrncat(char* s1, char* s2, size_t len1, size_t len2)
    {
    	s1+=len1;
    	while(len2--)
    		*s1++=*s2++;
    	return s1;
    }
    @Silverdream:
    the problem in your function is the for-loop!
    the index goes from 0 to n-1 and not form 0 to n

    and why the hell you call the function xstrcmp?? it does not compare...
    Hope you don't mind my bad english, I'm Austrian!

  7. #7
    Registered User Silverdream's Avatar
    Join Date
    Feb 2002
    Posts
    53

    Talking

    Thanx Shade. By the way i just misspelled strcat as strcmp.

  8. #8
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Code:
    char* xstrncat(char* s1, char* s2, size_t len1, size_t len2)
    {
    	s1+=len1;
    	while(len2--)
    		*s1++=*s2++;
    	return s1;
    }
    This is good c but it lacks readability. Am I the only person here who thinks that code as above should read like this...
    Code:
    char* xstrncat(char* s1, char* s2, size_t len1, size_t len2)
    {
    	s1+=len1;
    	while(len2)
            {
    		*s1=*s2;
                     ++s1;
                     ++s2;
                     --len2;
            }
    	return s1;
    }
    I think virtually any compiler will produce the same assembly language for this so why go with the former over the more readable latter.
    Last edited by Stoned_Coder; 02-06-2002 at 12:50 PM.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please check my C++
    By csonx_p in forum C++ Programming
    Replies: 263
    Last Post: 07-24-2008, 10:20 AM
  2. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 01:56 PM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 12:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21