how to get the same characters in a string?

how to get the same characters in a string?

Page 1 of 1

10 Replies - 1757 Views - Last Post: 09 October 2008 - 08:21 AM Rate Topic: -----

#1 jlhon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 02-October 08

how to get the same characters in a string?

Post icon  Posted 08 October 2008 - 09:15 PM

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


}
?>


Is This A Good Question/Topic? 0
  • +

Replies To: how to get the same characters in a string?

#2 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1200
  • View blog
  • Posts: 7,309
  • Joined: 07-September 06

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:
<?php
echo $_POST['txt'];
?>


Not exactly sure what you are asking...
Was This Post Helpful? 0
  • +
  • -

#3 jlhon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 02-October 08

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?? :)
Was This Post Helpful? 0
  • +
  • -

#4 DilutedImage  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 14
  • View blog
  • Posts: 646
  • Joined: 20-November 06

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
Was This Post Helpful? 0
  • +
  • -

#5 jlhon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 02-October 08

Re: how to get the same characters in a string?

Posted 08 October 2008 - 10:52 PM

anyone can help me??
pls...
Was This Post Helpful? 0
  • +
  • -

#6 DilutedImage  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 14
  • View blog
  • Posts: 646
  • Joined: 20-November 06

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.
Was This Post Helpful? 0
  • +
  • -

#7 engale  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 3
  • View blog
  • Posts: 594
  • Joined: 30-September 08

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.
Was This Post Helpful? 0
  • +
  • -

#8 akozlik  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 90
  • View blog
  • Posts: 797
  • Joined: 25-February 08

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.
Was This Post Helpful? 0
  • +
  • -

#9 akozlik  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 90
  • View blog
  • Posts: 797
  • Joined: 25-February 08

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

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
Was This Post Helpful? 0
  • +
  • -

#10 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3103
  • View blog
  • Posts: 10,898
  • Joined: 08-August 08

Re: how to get the same characters in a string?

Posted 09 October 2008 - 07:52 AM

Another useful function here: array_keys()
Was This Post Helpful? 0
  • +
  • -

#11 akozlik  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 90
  • View blog
  • Posts: 797
  • Joined: 25-February 08

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1