-2

Some languages have easy ways of doing this, but my question revolves in C and C++.

I wanna do something like this in Java:

public class sandbox {
    public static void main(String[] args) {
        System.out.println("Thank" + " you!");
    }
}

And transfer it in C:

#include <stdio.h>

int main() {
    /* The easiest way is like this:
        char *text1 = "Thank";
        char *text2 = " you";
        printf("%s%s\n", text1, text2);
    */
    printf("Thank" + " you."); // What I really want to do
}

How do I concatenate strings in a language like this?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 2
    what is wrong in the commented part of C file? – Deviprasad Sharma Sep 13 '20 at 08:49
  • 1
    C++ and C are different things. – foragerDev Sep 13 '20 at 08:51
  • Why not use `std::string` when you have tagged this question as C++ as well? That supports string concatenation. – Rohan Bari Sep 13 '20 at 08:52
  • Nothing is wrong with the commented part, I want to know how to concatenate strings the usual way. –  Sep 13 '20 at 08:52
  • 1
    The answer is different if we are either talking about C or C++ – Roberto Caboni Sep 13 '20 at 08:56
  • 1
    The answer for C is `strcat` (or `strncat` function) but it requires the knowledge about what a string is and how it is stored in memory. Basically a string is a nul-terminated array of chars, and the destination buffer of the concatenation must have enough room for the new string. There's no automatic reallocation in C. – Roberto Caboni Sep 13 '20 at 09:00
  • Is this question already answered here? https://stackoverflow.com/questions/8465006/how-do-i-concatenate-two-strings-in-c – Kevin Boone Sep 13 '20 at 10:22

5 Answers5

2

You use just nothing:

puts ("Thank" " you.");
Roberto Caboni
  • 7,252
  • 10
  • 25
  • 39
1

This is a concatenation, but is a constant or compile time concatenation, you can't concatenate strings like that, but in case you need to split a string constant in multiple parts is ok:

...
    printf("Thank"  " you."); // What I really want to do
...

For dynamic, runtime concatenation you need strcat like

strcat(text1, text2);

First you must assure that you have enough memory in target string, see this link http://www.cplusplus.com/reference/cstring/strcat/
Ok, that was the C way, but C++ has STL with std::string

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string str1 = "hello ", str2 = "world";
    cout<< str1 + str2<< endl;
    return 0;
}

armagedescu
  • 1,758
  • 2
  • 20
  • 31
1

What I have understood from your question, hope the below solution will answer your question.

#include <stdio.h>

int main() {
  char s1[100] = "Thank ", s2[] = "You";
  int length, j;

  // store length of s1 in the length variable
  length = 0;
  while (s1[length] != '\0') {
    ++length;
  }

  // concatenate s2 to s1
  for (j = 0; s2[j] != '\0'; ++j, ++length) {
    s1[length] = s2[j];
  }

  // terminating the s1 string
  s1[length] = '\0';

  printf("After concatenation: %s",s1);


  return 0;
}

In C++, you can easily concatenate two string it by adding two string with a + operator.

#include <iostream>
using namespace std;

int main()
{
    string s1, s2, result;

    cout << "Enter string s1: ";
    cin>>s1;

    cout << "Enter string s2: ";
    cin>>s2;

    result = s1 + s2;

    cout << "After concatenation: = "<< result;

    return 0;
}
Shovo
  • 133
  • 2
  • 9
1

Concatenating strings is not that easy in C unfortunately, here's how to do it most succinctly:

char *text1 = "Thank";
char *text2 = " you";

char *text_concat = malloc(strlen(text1) + strlen(text2) + 1);
assert(text_concat);

text_concat = strcpy(text_concat, text1);
text_concat = strcat(text_concat, text2);

printf("%s\n", text_concat);

free(text_concat);
Peter
  • 2,919
  • 1
  • 16
  • 35
0

It is not possible in C to do something like printf("Thank" + " you."); because C doesn't support Operator Overloading Unlike C++. You can refer Is it possible to overload operators in C?

Wasit Shafi
  • 854
  • 1
  • 9
  • 15