Concatenating strings (combining multiple strings into one).

To concatenate any String to a String, we need:

  • A string that is to appended with the string (say “ch”)

Below is the implementation of the above approach:

C++
// C++ Program for string
// concatenation using '+' operator
#include <iostream>
using namespace std;

// Driver code
int main()
{
    string init("this is init");
    string add(" added now");

    // Appending the string.
    init = init + add;

    cout << init << endl;
    return 0;
}
C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    char init[] = "this is init";
    char add[] = " added now";
    char* result = (char*)malloc(strlen(init) + strlen(add) + 1);

    strcpy(result, init);
    strcat(result, add);

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

    free(result);
    return 0;
}
Java
public class Main {
    public static void main(String[] args) {
        String init = "this is init";
        String add = " added now";

        // Appending the string.
        init = init + add;

        System.out.println(init);
    }
}
Python
def main():
    init = "this is init"
    add = " added now"

    # Concatenate strings
    result = init + add

    # Print the result
    print(result)

# Call the main function
if __name__ == "__main__":
    main()
JavaScript
// Define the main function
function main() {
    let init = "this is init";
    let add = " added now";

    // Appending the string.
    init = init + add;

    console.log(init);
}

// Call the main function
main();

Output
this is init added now

Basic String Operations with Implementation

In this post, we will look into some of the basic String operations such as:

  • Accessing characters by index in a string.
  • Inserting character into a String.
  • Modifying character in String
  • Deletion of Character in String
  • Concatenating strings (combining multiple strings into one).
  • Finding the length of a string
  • Comparing strings for equality or lexicographical order

Let us consider the basic String operations one by one.

Similar Reads

Accessing characters by index in a string.

To access any character in a String, we need:...

Inserting Character/String into an String.

To insert any Character/String in a String, we need:...

Modifying character in String

To modify any Character in a String, we need:...

Deletion of character in String

To delete any Character in a String, we need:...

Concatenating strings (combining multiple strings into one).

To concatenate any String to a String, we need:...

Finding the length/size of a string

To find the length of the String, we need:...

Comparing Strings for Equality

To compare strings, Define a function to compare values with the following conditions :...

Contact Us