Thread: concatenation

  1. #1
    1337
    Join Date
    Jul 2008
    Posts
    135

    concatenation

    Code:
    char mainStr[50], addStr[50], totalStr[100];
    gets(mainStr);
    scanf("%s", addStr);
        fflush(stdin); //gets() does not work, so  i used scanf.
    for (i = 0; totalStr[i] != '\0'; i++)
            {
                totalStr[i] = mainStr[i];
            }
            totalStr[i+1] = ' ';
            for (i = i + 2; totalStr[i] != '\0'; i++)
            {
                totalStr[i] = addStr[i];
            }
            printf("%s", totalStr);
    but when it prints, it only prints mainStr

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    This one is wrong:
    Code:
    for (i = i + 2; totalStr[i] != '\0'; i++)
    Try replacing it with:
    Code:
    for (i = i + 2; addStr[i] != '\0'; i++)
    There are of course other serious errors like no bounds checking anywhere. scanf("%s", addStr) is just as bad as gets().

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    Both the for loops are wrong actually. The first one should be:
    Code:
    for (i = 0; mainStr[i] != '\0'; i++)
    instead of
    Code:
    for (i = 0; totalStr[i] != '\0'; i++)
    Also, see this: Cprogramming.com FAQ > Why fflush(stdin) is wrong

  4. #4
    1337
    Join Date
    Jul 2008
    Posts
    135
    Code:
    for (i = 0; mainStr[i] != '\0'; i++)
            {
                totalStr[i] = mainStr[i];
            }
            totalStr[i] = ' ';
            for (j = 0; addStr[j] != '\0'; j++, i++)
            {
                totalStr[i+1] = addStr[j];
            }
            printf("%s", totalStr);
    It prints out "abc qwTHR-MINGW32"

    I entered "abc" as the first string and "qw" the second string. Thanks for helping.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    here you go


    Code:
    for (i = 0; mainStr[i] != '\0'; i++)
    {
         totalStr[i] = mainStr[i];
    }
    totalStr[--i] = ' ';
    
    for (j = 0; addStr[j] != '\0'; j++, i++)
    {
        totalStr[i+1] = addStr[j];
    }
    totalStr[i] = '\0';
    
    printf("%s", totalStr);
    i haven't run this, and you might have to adjust the i variable. point is, you didn't have the
    end of string char being put into place on the totalStr array. Also, you were off by one on
    the placement of the ' ' after the first loop.

    Please change your indentation style to match the above. It's crap otherwise, to read.
    Last edited by Adak; 01-17-2010 at 06:53 AM.

  6. #6
    1337
    Join Date
    Jul 2008
    Posts
    135
    Sorry for the indentation style, Actually thee code was so because i copied and pasted right away. But the actual code indentation is fine.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Concatenation Using Pointers No strcat()
    By oviang in forum C Programming
    Replies: 4
    Last Post: 12-07-2007, 10:31 AM
  2. printing arrays with concatenation
    By derek23 in forum C Programming
    Replies: 1
    Last Post: 07-17-2005, 03:02 AM
  3. Concatenation of strings as char pointers
    By sameerc in forum C Programming
    Replies: 11
    Last Post: 05-10-2005, 04:23 PM
  4. queue concatenation
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 04-02-2002, 06:35 AM
  5. integer concatenation with string again...
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 03-11-2002, 06:36 PM