TCS Coding Practice Question | Concatenate 2 Strings
Given two Strings, the task is to concatenate the two Strings using Command Line Arguments.
Examples:
Input: str1 = "hello", str2 = "world" Output: helloworld Input: str1 = "Geeks", str2 = "World" Output: GeeksWorld
Approach:
- Since the Strings are entered as Command line Arguments, there is no need for a dedicated input line
- Extract the input Strings from the command line argument
- Concatenate the given strings using the respective methods.
- Print or return the concatenated strings
Program:
C
// C program to concatenate the two Strings // using command line arguments #include <stdio.h> #include <stdlib.h> /* atoi */ #include <string.h> // Function to concatenate the Strings char* concat(char dest[], char src[]) { // Appends the entire string // of src to dest strcat(dest, src); // Return the concatenated String return dest; } // Driver code int main(int argc, char* argv[]) { // Check if the length of args array is 1 if (argc == 1) printf("No command line arguments found.\n"); else { // Get the command line arguments // and concatenate them printf("%s\n", concat(argv[1], argv[2])); } return 0; } |
chevron_right
filter_none
Java
// Java program to concatenate the two Strings // using command line arguments class GFG { // Function to concatenate the String public static String concat(String dest, String src) { // Return the concatenated String return (dest + src); } // Driver code public static void main(String[] args) { // Check if length of args array is // greater than 0 if (args.length > 0) { // Get the command line arguments // and concatenate them System.out.println(concat(args[0], args[1])); } else System.out.println("No command line " + "arguments found."); } } |
chevron_right
filter_none
Output:
Recommended Posts:
- TCS Coding Practice Question | Check Odd or Even
- TCS Coding Practice Question | HCF or GCD of 2 Numbers
- TCS Coding Practice Question | LCM of 2 Numbers
- TCS Coding Practice Question | Reverse a Number
- TCS Coding Practice Question | Average of 2 Numbers
- TCS Coding Practice Question | Greatest of 3 Numbers
- TCS Coding Practice Question | Swap two Numbers
- TCS Coding Practice Question | Factorial of a Number
- TCS Coding Practice Question | Palindrome String
- TCS Coding Practice Question | Reverse a String
- TCS Coding Practice Question | Sum of Digits of a number
- TCS Coding Practice Question | Fibonacci Series
- TCS Coding Practice Question | Prime Numbers upto N
- TCS Coding Practice Question | Checking Leap Year
- TCS Coding Practice Question | Check Palindrome Number
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.





