Jump to content
GarryOne

problema php, cum extrag vocalele dintr-un string/array

Recommended Posts

Posted

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?

Posted


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().

Posted

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")
3

Java


import 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.

Posted

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!

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...