Another String Question

This is a discussion on Another String Question within the C Programming forums, part of the General Programming Boards category; Can i do this: AString = (String1 + "and" + String2); ?? Or is there an other way? Thanks...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    10

    Another String Question

    Can i do this:

    AString = (String1 + "and" + String2);

    ??

    Or is there an other way?
    Thanks

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You can't write it that way in C. C has a very basic concept of string type, and almost every manipulation on a string requires a function call. In this case, you need strcat() to "concatenate" two strings.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Not in C. To add strings together use strcat, and remember that you need to set enough space aside to hold the resulting string. Example:
    Code:
    #include <stdio.h>
    #include <string.h> // for strcat
     
    int main()
    {
        char result[255] = {""};
        char a[] = "String1";
        char b[] = "String2";
    
        strcat(result, a);
        strcat(result, " and ");
        strcat(result, b);
    
        printf("&#37;s", result);
        getchar();
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 05:05 AM
  2. Reusing a string pointer question
    By chiefmonkey in forum C++ Programming
    Replies: 3
    Last Post: 05-06-2009, 05:53 PM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 04:58 PM
  4. String array question
    By gogo in forum C++ Programming
    Replies: 6
    Last Post: 12-08-2001, 06:44 PM

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