Thread: Combining 2 chars

  1. #1
    Registered User
    Join Date
    Jul 2022
    Posts
    50

    Combining 2 chars

    Hello,

    I am trying to combine to chars together, having a big of problem

    char char1;
    char char2;


    char1 = 'one';
    char2 = ' two';


    char1 = (char1[3] + char2[0];




    printf("%s",char1);
    what's the correct code?

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    'one' is not a single char, or a char string.

    You cannot use the '+' operator to combine two strings.

    Also, post code as CODE blocks not QUOTE blocks!

    Look at this code, but be cautious about buffer overflow. More code would need to prevent overflow.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
      char char1[20] = "one";
      char char2[10] = " two";
    
      // Or char *char2 = " two";   char2 is a pointer to a constant string
      // char1 must be a char array large enough to hold both strings
    
      strcat(char1, char2);
    
      printf("%s\n",char1);
    
      return 0;
    }
    You need to study a good up to date book on the C Programming Language before writing C code!
    Last edited by rstanley; 10-13-2023 at 01:05 PM.

  3. #3
    Registered User
    Join Date
    Jul 2022
    Posts
    50
    thanks,

    so what exactly is '#include <string.h>' used for?

    is it for '
    strcat' can be used?
    Last edited by WaterSerpentM; 10-13-2023 at 03:38 PM.

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Quote Originally Posted by WaterSerpentM View Post
    thanks,

    so what exactly is '#include <string.h>' used for?
    You need to include the correct header file for the declarations of Standard Library functions.

    In this case you need to #include string.h for the declaration of the string concatenation function, strcat().

    Please see the online manpage for strcat(), or if on Linux, use the command line command, "man strcat"

    Once again, if you study a good up to date book on the C Programming Language, you would learn all about this an all the other details of the language!

  5. #5
    Registered User
    Join Date
    Jul 2022
    Posts
    50
    Quote Originally Posted by rstanley View Post
    You need to include the correct header file for the declarations of Standard Library functions.

    In this case you need to #include string.h for the declaration of the string concatenation function, strcat().

    Please see the online manpage for strcat(), or if on Linux, use the command line command, "man strcat"

    Once again, if you study a good up to date book on the C Programming Language, you would learn all about this an all the other details of the language!
    thanks dude

  6. #6
    Registered User
    Join Date
    Jul 2022
    Posts
    50
    ive tried to combine itself, but dont work

    strcat(char3, char3);

  7. #7
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Quote Originally Posted by WaterSerpentM View Post
    ive tried to combine itself, but dont work
    This is legal but I don't know why you would concatenate char3 with char3. char 3 was not in your original code.

    You would need to post the entire code for me to comment on it!

    [CODE] blocks, NOT [QUOTE] blocks!

  8. #8
    Registered User
    Join Date
    Jul 2022
    Posts
    50
    [QUOTE=rstanley;1308879]This is legal but I don't know why you would concatenate char3 with char3. char 3 was not in your original code.

    You would need to post the entire code for me to comment on it!

    [CODE] blocks, NOT
    blocks!
    Code:
        char char3 [40] = "three";
        char char4 [10] = " four";
    
    
        strcat(char3, char4);
        printf("\n");
        printf("%s",char3);
    
    
        strcat(char3, char3);
    
    
        printf("\n");
        printf("%s",char3);
    Last edited by WaterSerpentM; 10-13-2023 at 04:41 PM.

  9. #9
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    You have an 's' at the end of the last printf() statement.

    A better way of doing this:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
      char char3 [20] = "three";
      char char4 [10] = " four";
      char char5[30] = "";   // Always initialize the local variables
    
      strcat(char3, char4);
      printf("\n");
      printf("%s",char3);
    
      strcpy(char5, char3); // A better method!
      strcat(char5, " ");   // Print a space inbetween
      strcat(char5, char3);
    
    
    //   printf("\n"); Not needed
      
      printf("%s\n",char5);
    
      return 0;
    }

  10. #10
    Registered User
    Join Date
    Jul 2022
    Posts
    50
    Quote Originally Posted by rstanley View Post
    You have an 's' at the end of the last printf() statement.

    A better way of doing this:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
      char char3 [20] = "three";
      char char4 [10] = " four";
      char char5[30] = "";   // Always initialize the local variables
    
      strcat(char3, char4);
      printf("\n");
      printf("%s",char3);
    
      strcpy(char5, char3); // A better method!
      strcat(char5, " ");   // Print a space inbetween
      strcat(char5, char3);
    
    
    //   printf("\n"); Not needed
      
      printf("%s\n",char5);
    
      return 0;
    }

    what does

    Code:
    strcpy
    do?

  11. #11
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    There is a function called sprintf() that could simplify this even more.

    I assume this is a homework assignment.

  12. #12
    Registered User
    Join Date
    Jul 2022
    Posts
    50
    Quote Originally Posted by rstanley View Post
    There is a function called sprintf() that could simplify this even more.

    I assume this is a homework assignment.
    its not an assignment, im learning how to calculate things right now

  13. #13
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    strcpy() copies an initial string into the destination, then strcat() adds strings to the end of the existing destination string.

    Again, you need to study a good book that I will recommend in a later message.

  14. #14
    Registered User
    Join Date
    Jul 2022
    Posts
    50
    Quote Originally Posted by rstanley View Post
    strcpy() copies an initial string into the destination, then strcat() adds strings to the end of the existing destination string.

    Again, you need to study a good book that I will recommend in a later message.
    yup, i see exactly how it works, just wanted to clarify it

    thanks

  15. #15
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    WaterSerpentM:

    If you are attempting to learn from painfully inadequate online tutorials, YouTube videos, or some book claiming to teach you in a short time, then THROW THEM AWAY!

    Short of taking a course in C Programming from a qualified instructor, you need to study a good book on the C Programming Language, cover to cover, and do all the exercises at the end of each chapter! Choose one of the three listed below:

    C Programming, A Modern Approach, 2nd Edition
    Author: K. N. King

    C Primer Plus, 6th Edition
    Stephen Prata

    C How to Program, 8/e
    Deitel & Deitel

    Studying one of these books, and writing code, you will have a much better understanding of the C Programming language.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers of chars and arrays of chars Question
    By shiroaisu in forum C++ Programming
    Replies: 9
    Last Post: 08-09-2010, 10:42 AM
  2. Combining several variables into one
    By h3ro in forum C++ Programming
    Replies: 39
    Last Post: 04-24-2008, 10:03 AM
  3. combining int's into single int
    By chris1985 in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 02:07 PM
  4. combining C and C++
    By volk in forum C Programming
    Replies: 7
    Last Post: 03-18-2003, 08:41 PM

Tags for this Thread