Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 131,789 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,754 people online right now. Registration is fast and FREE... Join Now!




how to get the same characters in a string?

 
Reply to this topicStart new topic

how to get the same characters in a string?, how to get the same characters in a string?

jlhon
post 8 Oct, 2008 - 08:15 PM
Post #1


New D.I.C Head

*
Joined: 2 Oct, 2008
Posts: 8

CODE

<!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'>";


}
?>

User is offlineProfile CardPM

Go to the top of the page

BetaWar
post 8 Oct, 2008 - 08:18 PM
Post #2


#include <soul.h>

Group Icon
Joined: 7 Sep, 2006
Posts: 1,980



Thanked 77 times

Dream Kudos: 1175
My Contributions


I am sorry, what are you trying to do? Print out the string?

If so that can be accomplished like so:
CODE
<?php
echo $_POST['txt'];
?>


Not exactly sure what you are asking...
User is offlineProfile CardPM

Go to the top of the page

jlhon
post 8 Oct, 2008 - 08:26 PM
Post #3


New D.I.C Head

*
Joined: 2 Oct, 2008
Posts: 8

//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?? smile.gif
User is offlineProfile CardPM

Go to the top of the page

DilutedImage
post 8 Oct, 2008 - 09:38 PM
Post #4


D.I.C Addict

Group Icon
Joined: 20 Nov, 2006
Posts: 642



Thanked 6 times

Dream Kudos: 25
My Contributions


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.

User is offlineProfile CardPM

Go to the top of the page

jlhon
post 8 Oct, 2008 - 09:52 PM
Post #5


New D.I.C Head

*
Joined: 2 Oct, 2008
Posts: 8

anyone can help me??
pls...
User is offlineProfile CardPM

Go to the top of the page

DilutedImage
post 8 Oct, 2008 - 11:33 PM
Post #6


D.I.C Addict

Group Icon
Joined: 20 Nov, 2006
Posts: 642



Thanked 6 times

Dream Kudos: 25
My Contributions


I just told you how to do it.
User is offlineProfile CardPM

Go to the top of the page

engale
post 9 Oct, 2008 - 04:19 AM
Post #7


D.I.C Addict

Group Icon
Joined: 30 Sep, 2008
Posts: 532



Thanked 2 times

Dream Kudos: 50
My Contributions


CODE

$chrcount[$string[$i]]++;

I'm sure you can do the rest.
User is offlineProfile CardPM

Go to the top of the page

akozlik
post 9 Oct, 2008 - 06:19 AM
Post #8


D.I.C Addict

Group Icon
Joined: 25 Feb, 2008
Posts: 586



Thanked 22 times

Dream Kudos: 750
My Contributions


This sounds suspiciously like a homework question . . .

What code have you written so far? We can help you from there.
User is offlineProfile CardPM

Go to the top of the page

akozlik
post 9 Oct, 2008 - 06:31 AM
Post #9


D.I.C Addict

Group Icon
Joined: 25 Feb, 2008
Posts: 586



Thanked 22 times

Dream Kudos: 750
My Contributions


Just to let you know, I wrote up two different ways of doing the same thing. One prints out the letters in alphabetical order

QUOTE

a = 1
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


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
User is offlineProfile CardPM

Go to the top of the page

CTphpnwb
post 9 Oct, 2008 - 06:52 AM
Post #10


D.I.C Regular

***
Joined: 8 Aug, 2008
Posts: 331



Thanked 19 times
My Contributions


Another useful function here: array_keys()

User is offlineProfile CardPM

Go to the top of the page

akozlik
post 9 Oct, 2008 - 07:21 AM
Post #11


D.I.C Addict

Group Icon
Joined: 25 Feb, 2008
Posts: 586



Thanked 22 times

Dream Kudos: 750
My Contributions


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.
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/20/08 03:51PM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month