String from prefix and suffix of given two strings

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 :
1. Get first l letters from string a, and last l letters from string b.
2. Combine both results, and this will be resultant string.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to 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));
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Program to 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.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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
?>

chevron_right



Output :

remuniesce


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Improved By : nitin mittal, Sam007, Rajput-Ji



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.