String from prefix and suffix of given two strings Last Updated : 18 Nov, 2022 Comments Improve Suggest changes Like Article Like Report Given two strings a and b, form a new string of length l, from these strings by combining the prefix of string a and suffix of string b. Examples : Input : string a = remuneration string b = acquiesce length of pre/suffix(l) = 5 Output :remuniesce Input : adulation obstreperous 6 Output :adulatperous Approach : Get first l letters from string a, and last l letters from string b. Combine both results, and this will be resultant string. Implementation: C++ // CPP code to form new string from // pre/suffix of given strings. #include<bits/stdc++.h> 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 <?php // PHP code to form new string from // pre/suffix of given strings. // Returns a string which contains // first l characters of 'a' and // last l characters of 'b'. function GetPrefixSuffix($a, $b, $l) { // Getting prefix of first // string of given length $prefix = substr($a, 0, $l); // length of string b $lb = strlen($b); // Calculating suffix of // second string $suffix = substr($b, $lb - $l); // Concatenating both // prefix and suffix return ($prefix.$suffix); } // Driver code $a = "remuneration"; $b = "acquiesce"; $l = 5; echo GetPrefixSuffix($a, $b, $l); // This code is contributed by Sam007 ?> JavaScript <script> // JavaScript Program to form new string // from pre/suffix of given strings // Returns a string which contains first l // characters of 'a' and last l characters of 'b'. function prefixSuffix(a, b, l) { // Calculating prefix of first // string of given length var prefix = a.substring(0, l); var lb = b.length; // Calculating suffix of second // string of given length var suffix = b.substring(lb - l); return (prefix + suffix); } // Driver code var a = "remuneration" , b = "acquiesce"; var l = 5; document.write(prefixSuffix(a, b, l)); // This code contributed by shikhasingrajput </script> Outputremuniesce Time Complexity: O(n + m), where n and m are the lengths of the given string.Auxiliary Space: O(n + m), where n and m are the lengths of the given string. Comment More infoAdvertise with us Next Article String from prefix and suffix of given two strings H HGaur Follow Improve Article Tags : Misc Strings Competitive Programming DSA Practice Tags : MiscStrings Similar Reads Minimum count of prefixes and suffixes of a string required to form given string Given two strings str1 and str2, the task is to find the minimum number of prefixes and suffixes of str2 required to form the string str1. If the task is not possible, return "-1".Example: Input: str1 = "HELLOWORLD", str2 = "OWORLDHELL"Output: 2Explanation: The above string can be formed as "HELL" + 10 min read Find the Longest Non-Prefix-Suffix Substring in the Given String Given a string s of length n. The task is to determine the longest substring t such that t is neither the prefix nor the suffix of string s, and that substring must appear as both prefix and suffix of the string s. If no such string exists, print -1. Example: Input: s = "fixprefixsuffix"Output: fix 7 min read Counting common prefix/suffix strings in two lists Given two Lists of strings s1 and s2, you have to count the number of strings in s2 which is either a suffix or prefix of at least one string of s1. Examples: Input: s1 = ["cat", "catanddog", "lion"], s2 = ["cat", "dog", "rat"]Output: 2Explanation: String "cat" of s2 is a prefix of "catanddog"string 4 min read Count of distinct substrings of a string using Suffix Array Given a string of length n of lowercase alphabet characters, we need to count total number of distinct substrings of this string. Examples: Input : str = âababaâ Output : 10 Total number of distinct substring are 10, which are, "", "a", "b", "ab", "ba", "aba", "bab", "abab", "baba" and "ababa"Reco 15+ min read Length of all prefixes that are also the suffixes of given string Given a string S consisting of N characters, the task is to find the length of all prefixes of the given string S that are also suffixes of the same string S. Examples: Input: S = "ababababab"Output: 2 4 6 8Explanation: The prefixes of S that are also its suffixes are: "ab" of length = 2"abab" of le 10 min read Like