来自给定两个字符串的前缀和后缀的字符串
给定两个字符串a和b ,通过组合字符串a的前缀和字符串b的后缀,从这些字符串中形成一个长度为 l 的新字符串。
例子 :
Input : string a = remuneration
string b = acquiesce
length of pre/suffix(l) = 5
Output :remuniesce
Input : adulation
obstreperous
6
Output :adulatperous
方法 :
1. 从字符串a 中获取前 l 个字母,从字符串b 中获取后 l 个字母。
2. 结合两个结果,这将是结果字符串。
C++
// CPP code to form new string from
// pre/suffix of given strings.
#include
using namespace std;
// Returns a string which contains first l
// characters of 'a' and last l characters of 'b'.
string GetPrefixSuffix(string a, string b, int l)
{
// Getting prefix of first
// string of given length
string prefix = a.substr(0, l);
// length of string b
int lb = b.length();
// Calculating suffix of second string
string suffix = b.substr(lb - l);
// Concatenating both prefix and suffix
return (prefix + suffix);
}
// Driver code
int main()
{
string a = "remuneration" ,
b = "acquiesce";
int l = 5;
cout << GetPrefixSuffix(a, b, l);
return 0;
}
Java
// Java Program to form new string
// from pre/suffix of given strings
import java.io.*;
class GFG
{
// Returns a string which contains first l
// characters of 'a' and last l characters of 'b'.
public static String prefixSuffix(String a,
String b,
int l)
{
// Calculating prefix of first
// string of given length
String prefix = a.substring(0, l);
int lb = b.length();
// Calculating suffix of second
// string of given length
String suffix = b.substring(lb - l);
return (prefix + suffix);
}
// Driver code
public static void main(String args[])
throws IOException
{
String a = "remuneration" ,
b = "acquiesce";
int l = 5;
System.out.println(prefixSuffix(a, b, l));
}
}
Python3
# Python code to form new from
# pre/suffix of given strings.
# Returns a string which contains first l
# characters of 'a' and last l characters of 'b'.
def GetPrefixSuffix(a, b, l):
# Getting prefix of first
# of given length
prefix = a[: l];
# length of string b
lb = len(b);
# Calculating suffix of second string
suffix = b[lb - l:];
# Concatenating both prefix and suffix
return (prefix + suffix);
# Driver code
a = "remuneration";
b = "acquiesce";
l = 5;
print(GetPrefixSuffix(a, b, l));
# This code contributed by Rajput-Ji
C#
// C# Program to form new string
// from pre/suffix of given strings.
using System;
class GFG
{
// Returns a string which contains first l
// characters of 'a' and last l characters of 'b'.
public static String prefixSuffix(String a,
String b,
int l)
{
// Calculating prefix of first
// string of given length
String prefix = a.Substring(0, l);
int lb = b.Length;
// Calculating suffix of second
// string of given length
String suffix = b.Substring(lb - l);
return (prefix + suffix);
}
// Driver Code
public static void Main()
{
String a = "remuneration" ,
b = "acquiesce";
int l = 5;
Console.Write(prefixSuffix(a, b, l));
}
}
// This code is contributed by Nitin Mittal.
PHP
Javascript
输出:
remuniesce