Thread: K&R problem !! String concatenation :(

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    8

    K&R problem !! String concatenation :(

    Here is another problem from K&R , I have been sittin with it for 4 hours

    This program tried to concatenate two strings. It aint doin it


    Code:
    #include "stdio.h"
    
    int  str_cat(char *, char *);
    
    int main()
    {
            char *str1 = "saguna";
            char *str2 = "karan";
             str_cat(str1,str2);
            return 0;
    } //main closes
    
    int   str_cat(char *st1, char *st2)
    {
            for(; *st1 != '\0'; st1++)
            {
                    if(*st1 == '\0')
                    {
                            printf("check ");          //not goin in if statement, it goes till s
                            for(;*st2 != '\0';st2++)
                            {
                             *st2 == *st1;
                            }
                            printf("The value of *st2=\n\n\n\n\n%c",*st2);
                             //return *st2;
                    }
                    else
                    {
                            printf("\nsomethings wronng\n");
                    }
            }
    } //str_cat closes
    Kindly help. I have tried my best

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    You will really need to use arrays here -- at least for str1 -- so you can write to it.
    Code:
            char *str1 = "saguna";
            char *str2 = "karan";
    Re-examine your logic here.
    Code:
            for(; *st1 != '\0'; st1++)
            {
                    if(*st1 == '\0')
                    {
                            printf("check ");          //not goin in if statement, it goes till s
                            for(;*st2 != '\0';st2++)
                            {
                             *st2 == *st1;
                            }
                            printf("The value of *st2=\n\n\n\n\n%c",*st2);
                             //return *st2;
                    }
                    else
                    {
                            printf("\nsomethings wronng\n");
                    }
            }
    You're only in the loop if you haven't reached the null terminator. In the loop you check to see if you are at the null terminator, which won't happen.

    String concatenation: find the end of the current destination (the null terminator), then copy the source to the end of the current destination until you reach the end of the source.

    [edit]And do the #include like this.
    Code:
    #include <stdio.h>
    Last edited by Dave_Sinkula; 08-09-2005 at 08:26 AM. Reason: Backwards logic!
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    Code:
    #include "stdio.h"
    
    void str_cat(char *, char *);
    
    int main()
    {
    	char *str1 = "saguna";
    	char *str2 = "karan";
    	str_cat(str1,str2);
    	return 0;
    } //main closes
    
    void str_cat(char *s1, char *st2)
    {       char *st1=s1;
    	for(; *st1 != '\0'; st1++);
    
    		if(*st1 == '\0')
    		{
    			printf("\ncheck ");          
    			for(;*st2 != '\0';)
    			{
    			 *st1++ = *st2++;
    			}
    			*st1='\0';
    			printf("The value of *s1=%s",s1);
    			 //return *st2;
    		}
    
    
    } //str_cat closes
    this code is according to your logic.
    check points=>
    1.for statement is ended by semicolon.because it will never be in if statement of your for loop.
    2.you have to add str2 in str1.so str1 will be in left of assignment operator.assignment is done by = operator,not by ==.
    3.to end the str1.you have put a null character '\0'
    4.your function returns nothing

    Your code contained some silly mistake thats it.well best of luck for the next time

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Just to make Dave's answer even clearer

    char *str1 = "saguna";
    Should be
    char str1[100] = "saguna";
    If you want something you can actually append data to.

    "string" constants like this are typically in read-only memory. As soon as you try and modify them, you'll get a segmentation fault.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    What's wrong with
    Code:
    while(*from) *to++ = *from++;
    *to = 0;
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Which can really just be:
    Code:
    while( *to++ = *from++ );
    There's no point in not copying the null over. This is also fine, because you're allowed to point one past the end of an array for comparison only. Although this alone is more of a strdup then a strcat, so you'd want to advance to the end of 'to' first.

    So, if we hide warnings with parenthesis, we should have something like this:
    Code:
    while( *to++ );
    to--;
    while( (*to++ = *from++) );
    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    more of a strdup then a strcat
    Do you mean strcat?

    Well, either way, it seems a little simpler than what was originally posted.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by dwks
    Do you mean strcat?
    And do you mean strcpy?
    If you understand what you're doing, you're not learning anything.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by dwks
    Do you mean strcat?

    Well, either way, it seems a little simpler than what was originally posted.
    No. I said what I meant. Your code as really just duping the origional into the new buffer. Thus, it's more of a strdup than a strcat.
    Quote Originally Posted by itsme86
    And do you mean strcpy?
    I suppose it's really more of a strcpy than a strdup, since it isn't actually allocating any more space, it's just filling another buffer.

    But in any case, it (dwks' code) wasn't what the origional had intented, as far as I saw it:
    Quote Originally Posted by karanmitra
    Here is another problem from K&R , I have been sittin with it for 4 hours

    This program tried to concatenate two strings. It aint doin it

    Code:
    #include "stdio.h"
    
    int  str_cat(char *, char *);
    Though it was a fragment, so had they advanced to the end of destination string first, it would have done the trick.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Aug 2005
    Posts
    12
    just try out this so u could find the answer for your previous problem
    Code:
    #include<stdio.h>
    main()
    {
    char *s1="Hello";
    for(;*s1!='\0';s1++)
    printf("%s",s1);
    }
    The Logic could be like this
    Code:
    str_cat(char *s1,char *s2)
    {
    char *s3;
    s3=s1;
    for(;*s1!='\0';s1++);
    for(;*s2!='\0';s2++,s1++)
    *s1=*s2;
    *s1='\0';
    printf("%s",s3);
    }
    Try it out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  4. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  5. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM