how do I combine two strings together

This is a discussion on how do I combine two strings together within the C Programming forums, part of the General Programming Boards category; how do I combine two strings together if two is defined ouput = "one" + two ???...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    12

    Question how do I combine two strings together

    how do I combine two strings together if two is defined

    ouput = "one" + two ???

  2. #2
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    Are you talking about attaching one string to another?

    if so, then:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    main()
     {
       char str_output[20]={0}, str_two[]="two";
    
       strcpy(str_output, "one");       // copies "one" into str_output
       strcat(str_output, str_two);    //  attaches str_two to str_output
    
       printf("The Output string is: %s", str_output);
     }
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    21

    program to concat without using strcpy and strcat and using functions instead

    /*program to concat without using strcpy and strcat functions */
    #include<stdio.h>
    #include<string.h>
    main()
    {char a[]="hello world"; /*ur 1st string goes here*/
    char b[]="how are you"; /*sencond string goes here*/
    func1(b,a);
    getchar();
    }
    func1(char *b,char *a)
    {
    int k,j;
    k=strlen(a);
    for(j=0;j<=strlen(b);j++)
    {a[k+1]=b[j];
    k++;
    }
    for(j=0;j<=((strlen(a)));j++)
    printf("%c",a[j]);
    }
    "The New Testament offers the basis for modern computer coding theory, in the
    form of an affirmation of the binary number system. But let your communication
    be Yea, yea; nay, nay: for whatsoever is more than these cometh of evil."
    - Matthew 5:37

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You've been here for 20 posts so far, and you still are too lazy to actually use code tags. You'll want to correct that.

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

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Also, main() is no longer valid C.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    Registered User
    Join Date
    Jun 2003
    Posts
    3
    Code:
    //program to Combine Two Strings  Using Pointers 
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    void combine(char*,char*,char*);
    char *a="",*b="Hello",*c="World";
    combine(a,b,c);printf("Combined String Is %s",a);
    getch();return 0;
    }
    
    void combine(char *res,char *str1,char *str2)
    {
    while(*str1!='\0')
    *res++=*str1++;
    while(*str2!='\0')
    *res++=*str2++;
    *res='\0';
    }
    Last edited by Rose_Flowers; 06-26-2003 at 11:50 PM.

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Your code is wrong, Rose_Flowers. You're accessing memory without allocating it (char *a). It should be char a[100]; or something like that. Or maybe use the new operator to allocate some memory and delete it later. There is no reason at all to not use the strcat() functions as described above.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Speedy5
    Or maybe use the new operator to allocate some memory and delete it later. There is no reason at all to not use the strcat() functions as described above.
    Wrong forum. C has no 'new' and 'delete' operators.

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

  9. #9
    Registered User
    Join Date
    Jun 2003
    Posts
    3

    Wink

    Lolzzzzz
    it does work when simply you access members
    of any array using address operator particularly
    in your own functions of void type but if you use
    strcpy strcat or such any other function to deal with strings and arrays in your self made function it will certainly not work, in that case you will have to return some
    value or to assign it so some variable
    I dont know why it is so but it work correctly even if u dont
    return any value it will assign returning value to first comment
    of that void type function.
    if u dont agree with it just compile it with ur TC Compiler and then post ur reply
    Last edited by Rose_Flowers; 06-26-2003 at 04:17 PM.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Rose_Flowers
    Lolzzzzz
    it does work when simply you access members
    of any array using address operator particularly
    Please actually know what you're talking about here. Granted, I tend to stick my foot in my mouth from time to time, but I usually recognize when I'm wrong.

    You cannot overrun your array boundaries without adverse side effects. Maybe not now, maybe not visible to you, but it is wrong.

    Furthermore, you cannot modify string literals.

    It is flat out wrong to modify data you have not implicitly allocated. Yes, your compiler will let you make a pointer point to any memory space you want. Yes, you can try to blindly write data to that location. No, it is not correct. It is wrong. It will crash your program, or some one else's program, but it is wrong. End of discussion.

    Learn how to correctly access and allocate memory, or don't use pointers.

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

  11. #11
    Registered User
    Join Date
    Jun 2003
    Posts
    14
    Sorry I'm kind of highjacking the thread but I've been wondering about this for some time:


    Furthermore, you cannot modify string literals.
    say I write
    foo1("Hello World");
    and somewhere else in the same module:
    foo2("Hello World");

    Will the compiler optimize those two calls to pass the same address, only writting "Hello World" once in the assembly code?

    I cannot see any other reason for this rule, so enlighten me if there is . I realise that you would be writting in the code section of the memory for your process anyway, but after all if you do not write out of bounds...

    I think, as is, this rule is bad from a syntactic point of view, it makes no sens, at least to a beginner's understanding .

    allowing only:

    const char* foo = "bar"

    and not:
    char* foo = "bar"

    Would make things much better I think (I'm sure you all impatiently wait for the day I'll get my own C standard out ) .

    Thanks

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by P.Phant
    Sorry I'm kind of highjacking the thread but I've been wondering about this for some time:

    say I write
    foo1("Hello World");
    and somewhere else in the same module:
    foo2("Hello World");

    Will the compiler optimize those two calls to pass the same address, only writting "Hello World" once in the assembly code?
    Most likely, yes. My understanding of it is that it builds a string table which it stores all string literals in. Any identical string will most likely just be treated as the same entry.

    In MSVC++, with no optimizations, here's what I get from the following code example:
    Code:
    #include <stdio.h>
    
    char * foostr = "Hello World";
    
    int main( void )
    {
        char *foostr2 = "Hello World";
    
        printf("%p, %p\n", foostr, foostr2 );
        return 0;
    }
    
    /*
        Output:
        0042001c, 0042001c
    */
    When in doubt, try it out.

    [edit]
    I modified printf to use %p instead of %x so it's "more correct" from a learning point of view. :P (Though the end result is really the same.)

    So to answer your final question, yes, this really is the reason you can't modify string literals. Because they're optimized into a string table which would basicly break it if you modified one of the "Hello World" strings some place.
    [/edit]

    Quzah.
    Last edited by quzah; 06-26-2003 at 06:36 PM.
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User
    Join Date
    Jun 2003
    Posts
    14
    Thanks quzah.

    When in doubt, try it out.


    Why didn't I think of outputting the pointer? oh well...

  14. #14
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Originally posted by Salem
    > if u dont agree with it just compile it with ur TC Compiler and then post ur reply
    Not everyone uses a fossil compiler on a broken OS which lets you spray data all over memory without a care in the world.
    It ain't broken, it works exactly as designed And it does care, is just doesn't BSOD


    Code:
    //program to Combine Two Strings  Using Pointers 
    #include<stdio.h>
    main()
    {
        void combine(char *, char *, char *);
        char *a = NULL, *b = "Hello", *c = "World";
        combine(a, b, c);
        printf("Combined String Is %s", a);
        return 0;
    }
    Oh, and before you spout about how you just had char *a, remember that you were pointing at a random memory location, which would be randomly used by something else. The fact that your TC compiled program didn't crash was luck.
    Well, not really random -- it points to the exact spot that the "Divide by Zero" interrupt address is...
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  15. #15
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Originally posted by P.Phant
    Will the compiler optimize those two calls to pass the same address, only writting "Hello World" once in the assembly code?
    It may or may not.
    ISO/IEC 9899:1999 (E): 6.5.2.5p8 String literals, and compound literals with const-qualified types, need not designate distinct objects.82)
    82) This allows implementations to share storage for string literals and constant compound literals with the same or overlapping representations.
    I'm not sure about C90.
    Code:
    #include <stdio.h>
    
    void foo(const char *s)
    {
       printf("%p \"%s\"\n", s, s);
    }
    
    int main(void)
    {
       foo("Hello world");
       foo("Hello world");
       return 0;
    }
    
    /* Borland C++ 5.0 for Win32 / Microsoft Windows 2000 [Version 5.00.2195]
    0040A131 "Hello world"
    0040A13D "Hello world"
    */
    And my output for Quzah's code was this.
    Code:
    00407078, 00407084
    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.*

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 07:37 AM
  4. How to combine 2 strings ?
    By The-Guy in forum C++ Programming
    Replies: 2
    Last Post: 07-03-2002, 10:10 PM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 12:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21