Thread: What is the wrong in this code?

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    11

    What is the wrong in this code?

    What is the wrong in this code?

    Code:
                       int strcon(char*s1,char*s2); //prototype
    
    
    int strcon(char*s1,char*s2){
    while(*s1!='\0'){
    s1++; }
    for(;*s1=*s2;s1++,s2++);{
    
     }return *s1 ; }

    this is a function to concatenate to strings
    pls help it prints NULL

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    It's because s1 is at the end of the string.
    You should save its initial value someplace and return that value.

  3. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    11
    can you write the code please?

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    Also here.

    Nobody will do you work for you. Give it a shot and post your attempt, along with any specific questions/problems you're having.
    Announcements - General Programming Boards

  5. #5
    Registered User
    Join Date
    Mar 2013
    Posts
    11
    Code:
    char * strcon(char*s1,char*s2){
    char *t=s1;
    while(*s1!='\0'){
    s1++; }
    for(;*s1=*s2;s1++,s2++);
    return t ; 
    }

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    When I said "give it a shot", I meant try writing it yourself - not just copying the code that was spoon-fed to you on your yahoo answers post (link above).

    Looks like I'm done here.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Mohamed Shokry View Post
    Code:
    char * strcon(char*s1,char*s2){
    char *t=s1;
    while(*s1!='\0'){
    s1++; }
    for(;*s1=*s2;s1++,s2++);
    return t ; 
    }
    M.S., if you don't understand this code, stop using it - it's like a shoe that just won't fit your foot.

    Write your own pseudo code for concatenating two string and if you don't want to use pointers, then use indices instead.

    You won't learn anything by having others code up simple exercises for you. It's not easy all the time, but you have to get in there, roll up your sleeves, and work at it - then study it - then work some more - then ask questions about the code or algorithm - then work some more.

    If you don't work at C, you can bet that C will not work for you.

  8. #8
    Registered User
    Join Date
    Mar 2013
    Posts
    11
    thanks all

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What's wrong with my code?
    By GigaRoid in forum C++ Programming
    Replies: 13
    Last Post: 02-06-2012, 10:50 AM
  2. What's wrong with my code?
    By x2x3i5x in forum C Programming
    Replies: 6
    Last Post: 09-28-2009, 11:52 AM
  3. What is wrong with my code?
    By mindofpoison in forum Game Programming
    Replies: 2
    Last Post: 12-15-2005, 08:12 PM
  4. What's wrong with this code?
    By Finchie_88 in forum Networking/Device Communication
    Replies: 10
    Last Post: 05-27-2005, 09:46 AM
  5. What's wrong with my code?
    By Kelvin in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 07-17-2002, 05:55 PM