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++ 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;
}
#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;
}
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);
}
}
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()
// 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.