String program #8
(Combine
two strings)
Enter String1: imagination
Enter String2: hunt
Concatenated string: imaginationhunt
The program for the string is written
in C# programming language and will accept two strings as input. The logic is
to merge strings. There are two ways to merge strings:
1.
Using
in-built function “Concat”
2.
Using
‘+’ operator
Let’s find out a simple and easy way
to code the program.
Practical Implementation:
using System;
namespace patternProblem.String
{
class String8 //combine two strings
{
static void Main(string[] args)
{
Console.Write("Enter String1: ");
string str1 = Console.ReadLine();
Console.Write("Enter String2: ");
string str2 = Console.ReadLine();
//Method-1: Using in-build function
Console.WriteLine("Concatenated string: {0}", string.Concat(str1, str2));
//Method-2: Using '+' operator. '+' is also used to
concatenate two strings
Console.WriteLine("Concatenated string: " + str1 + str2);
Console.ReadKey();
}
}
}
Output:
The input
string1 here is “Imagination” and string2 is “hunt”. So, based on the input, program
will combine those string. The output is shown below:
Enter String1: imagination
Enter String2: hunt
Concatenated string: imaginationhunt
Press any key to continue . . .
For any
query, comment us below.
Skip to
Main Table String based program
Next – String based program
#8
Keep learning and sharing...
No comments:
Post a Comment