#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    3
    Rep Power
    0

    String concatenation help


    Hello Programmers

    I have a problem by understanding this kind of concatenation.
    Case 1:
    String 1: devshed

    String 2: programmer

    Concatenated string: dr ee vm sm ha er dg o r p

    Case 2:

    String 1: Programmer

    String 2: Devshed

    concatenated string: pd re oh gs rv ae md m e r

    As you can see the first word from first string and last word from 2nd string is being printed untill all the words in the 2nd string are completed and then the left words are printed.

    I would be very much thank full if some body could post logic for this Thank you all in advanvce
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,377
    Rep Power
    1871
    You already have the logic given to you.

    I suggest you first write two simpler programs.

    1. Print each letter forwards.
    Eg.
    Input: Hello
    Output:
    H
    e
    l
    l
    o


    2. Print each letter backwards.
    Eg.
    Input: World
    Output:
    d
    l
    r
    o
    W

    When you know how to do those two things, you'll know enough to do both of them together.
    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
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    3
    Rep Power
    0
    Sure once i get succeded I will post the code here
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    3
    Rep Power
    0
    I Just tried it with this code it shows no errors not even any output

    #include <stdio.h>
    #include <string.h>

    char *p = "hello";
    char *k = "Sachin";
    int main(void)
    {
    register int t;
    register int s;

    //printf(p);
    for(s=strlen(k)-1;t<-1;t++)
    for( t = strlen( p ) - 1; t > -1; t--)

    printf("%c", p[ s ]);

    return 0;
    }
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    3
    Rep Power
    0

    Cool Using strrev


    Code:
    int main(int argc, char *argv[])
    {
      char *str1="DevShed", *str2="Programmer";
      int nchars1=strlen(str1), nchars2=strlen(str2), ii;
      strrev(str2);
      for (ii=0;ii<min(nchars1, nchars2);++ii)
      {
        putchar(str1[ii]); putchar(str2[ii]); putchar(' ');
      }
      for (ii=nchars1;ii<nchars2;++ii)
      {
        putchar(str2[ii]); putchar(' ');
      }
      for (ii=nchars2;ii<nchars1;++ii)
      {
        putchar(str1[ii]); putchar(' ');
      }
      return 0;
    }
    We reverse str2 and put out the characters until we exhaust one or the other string. Then we do the remaining characters, either catching them in the 2nd for loop if str2 is longer than str1, or in the 3rd for loop if str1 is longer than str2.

    HTH, regards,
    Graphic.
  10. #6
  11. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,377
    Rep Power
    1871
    @nikhilesh1166
    Keep it simple, and keep testing as you go.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    char *p = "hello";
    int main(void)
    {
      register int t;
    
      for( t = strlen( p ) - 1; t > -1; t--)
        printf("%c", p[ t ]);
    
      return 0;
    }
    
    
    $ gcc baz.c
    $ ./a.out 
    olleh$ 
    $

    @GraphicEqualis
    1. strrev() is not a standard function.
    2. *str2="Programmer"; may be a pointer to a string in read-only memory. As such, you may not be able to modify a string in place.
    3. We aim to help people learn how to code for themselves, not dump complete alternative answers on demand.
    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
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    3
    Rep Power
    0

    Red face Not using strrev then!


    Code:
    int main(int argc, char *argv[])
    {
      char *str1="DevShed", *str2="Programmer";
      int nchars1=strlen(str1), nchars2=strlen(str2), ii;
      for (ii=0;ii<min(nchars1, nchars2);++ii)
      {
        putchar(str1[ii]); putchar(str2[nchars2-ii-1]); putchar(' ');
      }
      for (ii=nchars1;ii<nchars2;++ii)
      {
        putchar(str2[nchars2-ii-1]); putchar(' ');
      }
      for (ii=nchars2;ii<nchars1;++ii)
      {
        putchar(str1[ii]); putchar(' ');
      }
      return 0;
    }
    Sorry, but I think, speak and listen directly in "code". That's just me! Tolerate me please!
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    40
    Rep Power
    19
    Originally Posted by nikhilesh1166
    Hello Programmers

    I have a problem by understanding this kind of concatenation.
    Case 1:
    String 1: devshed

    String 2: programmer

    Concatenated string: dr ee vm sm ha er dg o r p

    Case 2:

    String 1: Programmer

    String 2: Devshed

    concatenated string: pd re oh gs rv ae md m e r

    As you can see the first word from first string and last word from 2nd string is being printed untill all the words in the 2nd string are completed and then the left words are printed.

    I would be very much thank full if some body could post logic for this Thank you all in advanvce
    Case 1 and case 2 have the same pattern.

    1. get a character from string 1, from left to right
    2. put it in concatenated string
    3. get a character from string 2, from right to left
    4. put it in concatenated string
    5. put a space in concatenated string
    6. repeat from 1. to 5. until no more character form string 1 and string 2

    Implementing in pseudo code is simple
    Code:
       for (i=0; i<maxlenght; i++)
       {
          if(i<n1)
              put string1[i] in concatenated string       /* Forward */
          if(n2-1-i >=0) 
              put string2[n2-1-i] in concatenated string  /* Backward */
          put ' ' in concatenated string
       }
    in C look like this
    Code:
       char str1[]="DevShed", 
            str2[]="Programmer";
       
       char *buffer, *runner;
       int i, n1, n2, maxlen;
       
       n1 = strlen(str1);
       n2 = strlen(str2);
       maxlen = (n1 > n2) ? n1 : n2;
       
       puts(str1);
       puts(str2);
    
       /* reserve place for str1, str2, space and '\0' */
       buffer = (char *) malloc (3*maxlen+1);  
    
       runner = buffer;
       for (i = 0; i < maxlen; i++){
          if(i < n1)
             *runner++ = str1[i];         /* Forward */
          if(n2-1-i >= 0) 
             *runner++=str2[n2-1-i]; /* Backward */
          *runner++=' ';                  /* Space */
       }
       *runner='\0';
    
       puts(buffer);
    P.S. This code is not safe. Be careful to use it :D

IMN logo majestic logo threadwatch logo seochat tools logo