TCS Coding Practice Question | Concatenate 2 Strings
Last Updated :
11 Jan, 2023
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
#include <stdio.h>
#include <stdlib.h> /* atoi */
#include <string.h>
char * concat( char dest[], char src[])
{
strcat (dest, src);
return dest;
}
int main( int argc, char * argv[])
{
if (argc == 1)
printf ( "No command line arguments found.\n" );
else {
printf ( "%s\n" , concat(argv[1], argv[2]));
}
return 0;
}
|
Java
class GFG {
public static String concat(String dest, String src)
{
return (dest + src);
}
public static void main(String[] args)
{
if (args.length > 0 ) {
System.out.println(concat(args[ 0 ], args[ 1 ]));
}
else
System.out.println( "No command line "
+ "arguments found." );
}
}
|
Output:
- In C:
-
In Java:
Share your thoughts in the comments
Please Login to comment...