Skip to main content
5 of 11
added 742 characters in body
rayryeng
  • 1.6k
  • 1
  • 14
  • 19

MATLAB, 63 41 bytes

f=@(x)numel(x(+~isequal('google',x):end))

Unlike the other more elegant solutions, performing a division by zero operation in MATLAB will not give an error, but rather Inf. This solution finds the length of the string by numel, but more importantly, the beginning of where to access the string is produced by checking whether the input is equal to 'google'. If it isn't, this produces a 1 and we index into the string normally... as MATLAB starts indexing at 1. Should it be equal, the index produced is 0 and MATLAB will throw a zero-indexing error. The extra + is to ensure that the output is numerical rather than Boolean/logical... thus completing the code.

Example uses

>> f=@(x)numel(x(+~isequal('google',x):end)) %// Declare anonymous function

f = 

    @(x)numel(x(+~isequal('google',x):end))

>> f('bing')

ans =

     4

>> f('google')
Subscript indices must either be real positive integers or logicals.

Error in @(x)numel(x(+~isequal('google',x):end))

A more fun version, 83 bytes

f=@(x)eval('if isequal(''google'',x),web(''www.google.com/i'');else numel(x),end');

Abusing eval within anonymous functions, what the code does is that it checks to see if the input string is equal to 'google'... and if it is, this will open up MATLAB's built-in web browser and shows Google's 404 error page trying to access the subpage located at i when that doesn't exist. If not, we display the length of the string normally.

Example uses

>> f=@(x)eval('if isequal(''google'',x),web(''www.google.com/i'');else numel(x),end');
>> f('bing')

ans =

     4

>> f('google')
>> 

The last call using 'google' gives us this screen:

enter image description here

rayryeng
  • 1.6k
  • 1
  • 14
  • 19