Thread: Another String Question

  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, 04:05 AM
  2. Reusing a string pointer question
    By chiefmonkey in forum C++ Programming
    Replies: 3
    Last Post: 05-06-2009, 04:53 PM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. String array question
    By gogo in forum C++ Programming
    Replies: 6
    Last Post: 12-08-2001, 06:44 PM