1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

About string concatenation output.

Discussion in 'C' started by pulsar@c, Sep 16, 2010.

  1. pulsar@c

    pulsar@c New Member

    Joined:
    Sep 16, 2010
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    Doubt regarding this code please clear this. The following program giving strange output so please help what is happenig with this code what is error in this.

    Code:
    #include<stdio.h>
    #include<string.h>
    main()
    {
       char str1[100],str2[100];
      int i.length1,length2;
      printf("Enter the first string");
      gets(str1);
      length1 = strlen(str1);
      printf("Enter second string \n");
      gets(str2);
      length2 = strlen(str2);
      
      for(i=0;i<length2;i++) str1[length1++] = str2[i];
      str1[length1+length2] = '\0';
      printf("concatenated string is %s",str1);
      getch();     
    }
    
    And please also tell me why this & is not required while accespting string using scanf() statement.

    Regards
    Vivek Nayak
     
  2. NewsBot

    NewsBot New Member

    Joined:
    Dec 2, 2008
    Messages:
    1,267
    Likes Received:
    1
    Trophy Points:
    0
    First I really doubt your program will compile because it has some syntax errors.

    Second & is not required for strings because you are already passing the address of the character array
     
  3. techgeek.in

    techgeek.in New Member

    Joined:
    Dec 20, 2009
    Messages:
    572
    Likes Received:
    17
    Trophy Points:
    0
    Occupation:
    EOC (exploitation of computers)..i m a Terminator.
    Location:
    Not an alien!! for sure
    Home Page:

    you are not passing the address of character array here means that the name of an array is itself a pointer( refer to the property of an array)
     
  4. pulsar@c

    pulsar@c New Member

    Joined:
    Sep 16, 2010
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    As you said if & is not required to accept string array. then why do we need & sign for integer array. please kindly reply for this. in program what i sent for you it gets compiled but gives wrong output. please give the exact reason for the same. Thank you for the last reply.

    Regards
    Vivek Nayak
    Code:
    #include<stdio.h>
    #include<string.h>
    main()
    {
       char str1[100],str2[100];
      int i,length1,length2;
      printf("Enter the first string");
      gets(str1);
      length1 = strlen(str1);
      printf("Enter second string \n");
      gets(str2);
      length2 = strlen(str2);
      
      for(i=0;i<length2;i++) str1[length1++] = str2[i];
      str1[length1+length2] = '\0';
      printf("concatenated string is %s",str1);
      getch();     
    }
     

Share This Page