GarryOne Posted July 3, 2011 Report Posted July 3, 2011 vreau sa fac o functie care atunci cand este apelata sa returneze numarul vocalelor dintr-un string/array si vocalele in sine care au fost declarate.Cum pot face? Quote
totti93 Posted July 3, 2011 Report Posted July 3, 2011 function vocale($str){ $ret[0] = 0; $k = 0; $voc = array('a', 'e', 'i', 'o', 'u'); for ($i = 0; $i < strlen($str); $i++) for ($j = 0; $j < sizeof($voc); $j++) if ($voc[$j] == $str[$i]){ $ret[0]++; if (!in_array($voc[$j], $ret)){ $k++; $ret[$k] = $voc[$j]; } } return $ret; }Returneaza un array (numar_vocale, vocala1, vocala2, ...)Vocalele care se repeta in $str, va fi returnata doar o singura data.Pentru array poti folosi in loc de strlen() <-> sizeof(). Quote
cmiN Posted July 3, 2011 Report Posted July 3, 2011 Py>>> voc = "aeiou">>> def func(arg):... tmp = 0... if arg[0] in voc:... tmp = 1... if len(arg) > 1:... return tmp + func(arg[1:])... else:... return tmp...>>> f = lambda x: func(x.lower())>>> f("GarryOne")3Javaimport java.io.*;public class Voc { static String vowels = "aAeEiIoOuU"; static int process(String word) { int cnt = 0; boolean tmp; for (int i = 0; i < word.length(); ++i) { tmp = false; for (int j = 0; j < vowels.length(); ++j) { if (word.charAt(i) == vowels.charAt(j)) { tmp = true; break; } } if (tmp) { ++cnt; } } return cnt; } public static void main(String[] args) throws IOException { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Input: "); String word = input.readLine(); System.out.println(process(word)); }}In C nu mai e nevoie .. ai mai sus .Pentru vocalele aparute iti faci o lista sau multime (daca le vrei exact asa cum apar sau doar identitatea) pe care o golesti de fiecare data inainte sa apelezi functia si mai bagi o linie in functia respectiva prin care adaugi un element in obiect in momentul cand ai un pozitiv. Quote
endemic Posted July 3, 2011 Report Posted July 3, 2011 remarcati lungimea intre python si java. Quote
swappie Posted July 4, 2011 Report Posted July 4, 2011 Salut GarryOne,Uite un exemplu mai jos luat de pe: PHP: str_replace - Manual<?php// Provides: <body text='black'>$bodytag = str_replace("%body%", "black", "<body text='%body%'>");// Provides: Hll Wrld f PHP$vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U");$onlyconsonants = str_replace($vowels, "", "Hello World of PHP");// Provides: You should eat pizza, beer, and ice cream every day$phrase = "You should eat fruits, vegetables, and fiber every day.";$healthy = array("fruits", "vegetables", "fiber");$yummy = array("pizza", "beer", "ice cream");$newphrase = str_replace($healthy, $yummy, $phrase);// Provides: 2$str = str_replace("ll", "", "good golly miss molly!", $count);echo $count;?>Sper sa-ti fie de folos! Daca vrei sa inveti PHP, cauta Zend Certified Engineer Study Guide Bafta multa! Quote