Q:

Write a Ruby program to create a string using the first two characters (if present) of a given string

0

Write a Ruby program to create a string using the first two characters (if present) of a given string if the first character is 'p' and second one is 's' otherwise return a blank string.

All Answers

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

def text_test(str)
   len = str.length();
   temp = "";
	if(len >= 1)
	   if(str.slice(0) == 'p')
			temp += str.slice(0);
		if(len >= 2)
			if(str.slice(1) == 's')
			temp += str.slice(1);
		    end	
	    end
	  end
	end 
  return temp;
end
print text_test("psabcd"),"\n"
print text_test("abcd")
Output:
ps

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