In programming, printing strings with a separator means displaying two strings on the same line with a specified symbol between them. This can be done using an output statement by printing the first string, the separator, and then the second string.
For example, if the strings are "Geeks" and "Coder", and the separator is "@", the output will be Geeks@Coder.
Given two strings s1 and s2, and a separator symbol separator, print s1 and s2 with separator between them, followed by a new line.
Examples:
Input: s1 = "Hello", s2 = "World", separator = "@"
Output: Hello@World
Explanation: s1 and s2 are printed with the @ symbol in between.Input: s1 = "Geeks", s2 = "For", separator = "-"
Output: Geeks-For
Explanation: s1 and s2 are printed with the - symbol in between.
Output Stream - O(|s1| + |s2| + |separator|) Time and O(1) Space
The idea is to print both strings with the given separator between them, followed by a newline.
Different Language Output Statements
- C: printf("%s%c%s\n", s1, separator, s2);
- C++: cout << s1 << separator << s2 << '\n';
- Java: System.out.println(s1 + separator + s2);
- Python: print(s1 + separator + s2)
- C#: Console.WriteLine(s1 + separator + s2);
- JavaScript: console.log(s1 + separator + s2);
#include <iostream>
using namespace std;
int main()
{
string s1 = "Hello", s2 = "World", separator = "@";
// Print both strings with the separator between them
cout << s1 << separator << s2 << '\n';
return 0;
}
#include <stdio.h>
int main()
{
char s1[] = "Hello", s2[] = "World", separator[] = "@";
// Print both strings with the separator between them
printf("%s%s%s\n", s1, separator, s2);
return 0;
}
public class GFG {
public static void main(String[] args)
{
String s1 = "Hello", s2 = "World";
String separator = "@";
// Print both strings with the separator between
// them
System.out.println(s1 + separator + s2);
}
}
s1 = "Hello"
s2 = "World"
separator = "@"
# Print both strings with the separator between them
print(s1 + separator + s2)
using System;
public class Program {
public static void Main()
{
string s1 = "Hello", s2 = "World", separator = "@";
// Print both strings with the separator between
// them
Console.WriteLine(s1 + separator + s2);
}
}
let s1 = "Hello", s2 = "World", separator = "@";
// Print both strings with the separator between them
console.log(s1 + separator + s2);
Output
Hello@World