/*Judge palindrome execise*/
def is_palindrome(str,i,j,str_length)
if(str_length==0 ||str_length==1)
return true
end
if !(str[i]==str[j])
return false;
end
return is_palindrome(str,i+1,j-1,str_length-2)
end
str="abcba"
str_length=str.length
if is_palindrome(str,0,(str_length-1),str_length)
puts str+" is palindrome";
else
puts str+" is not palindrome";
end
#/*statistics every char count*/
def count(str)
s=[]
str.each_byte{|x|s<<"%c"%x}
num=Hash.new
for c in s do
if(num.has_key?(c))
num[c]=num[c]+1;
else
num[c]=1;
end
end
puts num
end
count("abcabcdefedsfsfd");
#/*convert the char case */
def upcase(str)
s=""
str.each_byte do |input_str|
if(input_str.chr>='a'&& input_str.chr<='z')
s << (input_str - 32).chr;
else
s << input_str
end
end
puts s
end
upcase('afedSfsfd');
#/*letter,number,space,other letter quantitative statistics*/
def character_statistics(str)
letter_count=0
number_count=0
space_count=0
other_count=0
str.each_byte do |str|
if(str.chr=~ /[A-Z]/ || str.chr=~ /[a-z]/)
letter_count+=1;
else if(str.chr=~ /\d/)
number_count+=1
else if(str.chr==' ')
space_count+=1
else
other_count+=1
end
end
end
end
puts "letter is "+letter_count.to_s
puts "number is "+number_count.to_s
puts "space is "+space_count.to_s
puts "other charator is "+other_count.to_s
end
character_statistics("ab19 ced s!SFd");