Q:

Write a C++ program to create a new string taking the first character from a given string and the last character from another given string. If the length of any given string is 0, use '#' as its missing character

0

Write a C++ program to create a new string taking the first character from a given string and the last character from another given string. If the length of any given string is 0, use '#' as its missing character

Sample Output:

Hi
PP
JS
C#

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <iostream>

using namespace std;

string test(string s1, string s2)
        {
            string lastChars = "";

            if (s1.length() > 0)
            {
                lastChars += s1.substr(0, 1);
            }
            else
            {
                lastChars += "#";
            }

            if (s2.length() > 0)
            {
                lastChars += s2.substr(s2.length() - 1);
            }
            else
            {
                lastChars += "#";
            }

            return lastChars;
        }
        
int main() 
 {
  cout << test("Hello", "Hi") << endl;  
  cout << test("Python", "PHP") << endl; 
  cout << test("JS", "JS") << endl; 
  cout << test("Csharp", "") << endl;   
  return 0;    
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now