Ruby :: Aufgabe #17
2 Lösungen

Text abwechselnd in Groß- und Kleinschreibung
Anfänger - Ruby
von Dome
- 29.12.2012 um 01:34 Uhr
Schreiben Sie ein Programm, welches einen eingegeben Text so manipuliert, das der Text abwechselnd in Groß- und Kleinschreibung auf den Bildschirm ausgegeben wird.
Konsolenausgabe:
Texteingabe: Beispieltext
Textausgabe: BeIsPiElTeXt
Lösungen:

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class GrossKleinSchreibung { public static void main(String[] args) throws IOException { System.out.println("Enter text: "); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String text = in.readLine(); System.out.println(manipulate(text)); } /** * makes all letters at position 2*k a lowerCase letter, all others upperCase. * All characters will be counted, but only letters will be modified. * @param text the text to manipulate * @return the manipulated String */ private static String manipulate(String text){ char[] chars = new char[text.length()]; //copy all chars as lowerCase into array text.toLowerCase().getChars(0, text.length(), chars, 0); //make every second letter a capital one for(int i=0; i<chars.length; i+=2) chars[i] = Character.toUpperCase(chars[i]); //rebuilt a String and return it return String.copyValueOf(chars); } }

#die "klassiche Lösung" print "Texteingabe: " text = gets.chop ausgabe = "" for i in 0..text.size-1 do if i % 2 == 0 then ausgabe += text[i].upcase else ausgabe += text[i].downcase end end puts "Textausgabe: " + ausgabe #es geht auch kürzer: #print "Texteingabe: " #text = gets.chop #print "Textausgabe: " #for i in 0..text.size-1 do # if i % 2 == 0 then # print text[i].upcase # else # print text[i].downcase # end #end #++++++++++++++++ #oder so: #print "Texteingabe: " #text = gets.chop #print "Textausgabe: " #i = 1 #text.each_char {|c| i+=1; print c.upcase if i%2 == 0; print c.downcase if i%2 != 0}