<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<form name="myform" method="post" action="<? echo $PHP_SELF; ?>">
<input type="text" name="txt" />
<input type="submit" name="submit" value="count" />
</form>
</body>
</html>
//this code is to get the length of a string
//how can i know the same characters in a string??
<?PHP
if(isset($_POST['submit'])){
$string=trim($_POST['txt']);
$count = strlen($string);
echo "<input type='text' value='$count'>";
}
?>
how to get the same characters in a string?how to get the same characters in a string?
Page 1 of 1
10 Replies - 1276 Views - Last Post: 09 October 2008 - 08:21 AM
#1
how to get the same characters in a string?
Posted 08 October 2008 - 09:15 PM
Replies To: how to get the same characters in a string?
#2
Re: how to get the same characters in a string?
Posted 08 October 2008 - 09:18 PM
I am sorry, what are you trying to do? Print out the string?
If so that can be accomplished like so:
Not exactly sure what you are asking...
If so that can be accomplished like so:
<?php echo $_POST['txt']; ?>
Not exactly sure what you are asking...
#3
Re: how to get the same characters in a string?
Posted 08 October 2008 - 09:26 PM
//example output:
//example output
$string="apple"
the output should be:
a=1
p=2
l=1
e=1
number of string = 5
Can you help me??
//example output
$string="apple"
the output should be:
a=1
p=2
l=1
e=1
number of string = 5
Can you help me??
#4
Re: how to get the same characters in a string?
Posted 08 October 2008 - 10:38 PM
I'd step through the string one character at a time, tallying each character in an associative array. You could then use a foreach loop to output the count for each letter in the array.
2¢
2¢
#5
Re: how to get the same characters in a string?
Posted 08 October 2008 - 10:52 PM
anyone can help me??
pls...
pls...
#6
Re: how to get the same characters in a string?
Posted 09 October 2008 - 12:33 AM
I just told you how to do it.
#7
Re: how to get the same characters in a string?
Posted 09 October 2008 - 05:19 AM
$chrcount[$string[$i]]++;
I'm sure you can do the rest.
#8
Re: how to get the same characters in a string?
Posted 09 October 2008 - 07:19 AM
This sounds suspiciously like a homework question . . .
What code have you written so far? We can help you from there.
What code have you written so far? We can help you from there.
#9
Re: how to get the same characters in a string?
Posted 09 October 2008 - 07:31 AM
Just to let you know, I wrote up two different ways of doing the same thing. One prints out the letters in alphabetical order
The other displays them in the order they appear in the string
We're not going to do your homework for you, but if you are using PHP you can use the following functions.
Method 1:
substr_count()
Reference string as array
Method 2:
substr_count()
in_array()
array_push()
strlen()
Show us your code and we can help you further. Also, if you don't know any of the functions I just gave you, look them up on the PHP documentation on php.net
Quote
a = 1
e = 1
l = 1
p = 2
e = 1
l = 1
p = 2
The other displays them in the order they appear in the string
Quote
a = 1
p = 2
l = 1
e = 1
p = 2
l = 1
e = 1
We're not going to do your homework for you, but if you are using PHP you can use the following functions.
Method 1:
substr_count()
Reference string as array
Method 2:
substr_count()
in_array()
array_push()
strlen()
Show us your code and we can help you further. Also, if you don't know any of the functions I just gave you, look them up on the PHP documentation on php.net
#10
Re: how to get the same characters in a string?
Posted 09 October 2008 - 07:52 AM
Another useful function here: array_keys()
#11
Re: how to get the same characters in a string?
Posted 09 October 2008 - 08:21 AM
I love problems like this, as they help me discover new functions in the PHP manual. I'm such a nerd.
Anyway, if you're looking to print the letters in the order they appear in the word, I found a third method that utilizes some other functions.
Method 3
If you have PHP5 installed, you can use the str_split() function. If you're using 4.x, you need to find another function, or just loop through each character of the string and array_push() it onto an array.
From there you can utilize the array_count_values() function. Then you take the resulting array, do a foreach loop on it, and echo out the key and value.
There you have it. Three different ways to do the same thing. Write up some code and let us know what you come up with. No excuse for not being able to do it now.
Anyway, if you're looking to print the letters in the order they appear in the word, I found a third method that utilizes some other functions.
Method 3
If you have PHP5 installed, you can use the str_split() function. If you're using 4.x, you need to find another function, or just loop through each character of the string and array_push() it onto an array.
From there you can utilize the array_count_values() function. Then you take the resulting array, do a foreach loop on it, and echo out the key and value.
There you have it. Three different ways to do the same thing. Write up some code and let us know what you come up with. No excuse for not being able to do it now.
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote







|