473,396 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to replace certain characters in a string and print each permutation?

Hi

I am looking to replace each "B" with "A" and each "D" with "C" in the string "BAD" but listing each permutation

i.e.

BAD
AAD
BAC
AAC

Hope this is clear as to what i am looking for.
I have not done any programming for many years and like php, so am trying to learn various functions and ways of going about solving problems. I am very rusty lol Please help. Much appreciated.
Regards
Adrian
Oct 13 '10 #1
7 2143
Hi,

Check out the below code..
Expand|Select|Wrap|Line Numbers
  1. echo $str = "BAD"; echo "<br>";
  2. $strlen = strlen($str);
  3. for($i=0; $i<$strlen; $i++) {
  4.     $ascii = ord($str[$i]);
  5.     if($ascii != 65) {
  6.         if($i == 1) {
  7.             $str[$i] = chr($ascii-1);
  8.         } else {
  9.             $str[0] = chr(ord($str[0])-1);
  10.         }
  11.     } else {
  12.         if($i == 1) {
  13.             $str[0] = chr(ord($str[0])+1);
  14.             $str[2] = chr(ord($str[2])-1);
  15.         }
  16.     }
  17.     echo $str.'<br>';
  18. }
Oct 14 '10 #2
mate thats spot on, thanks alot

now i just need to disect it and understand it lol

thanks again

Adrian B
Oct 15 '10 #3
kovik
1,044 Expert 1GB
Sounded like a homework assignment to me... You generally shouldn't give the code for trivial exercises like this one, since they are likely homework-related.
Oct 15 '10 #4
Was not a homework assignment at all. I am 35 and a drainage engineer. I did programming for homework many years ago when i was at college. Just fancy having ago at programming again in spare time. Like i said in my first post, i am a bit rusty.
FYI i am messing about with trying to make a word game in php that is similar to anagram solving etc.. thanks.
Oct 19 '10 #5
kovik
1,044 Expert 1GB
Oh. Okay then. Normally, people tell us what they are trying to do before asking such a vague question. My apologies.
Oct 19 '10 #6
Hi Adrian,

Have a look at this three functions in php to understand the snippet.
ord - Return ASCII value of character
chr - Return a specific character
strlen - Get string length

In a for loop using strlen function, I just converted each character of the string "BAD" to its ascii value(ord), then using the conditions i just rearranged the character positions, then converted it into its character using chr function and shown in a loop.
Oct 19 '10 #7
kovik
1,044 Expert 1GB
How about:
Expand|Select|Wrap|Line Numbers
  1. /**
  2.  * Get all permuations of a string based on an array of translations
  3.  *
  4.  * @author Kovik :) http://koviko.net/
  5.  * @param string $str
  6.  * @param array $rules
  7.  * @return array
  8.  */
  9. function get_all_permutations($str, array $rules) {
  10.     $rules_power_set = array(array());
  11.  
  12.     foreach ($rules as $from => $to) {
  13.         foreach ($rules_power_set as $current_set) {
  14.             $rules_power_set[] = array_merge(array($from => $to), $current_set);
  15.         }
  16.     }
  17.  
  18.     $permutations = array();
  19.  
  20.     foreach ($rules_power_set as $rules) {
  21.         $permutations[] = strtr($str, $rules);
  22.     }
  23.  
  24.     return $permutations;
  25. }
And use it like this:
Expand|Select|Wrap|Line Numbers
  1. $str = 'BAD';
  2. $rules = array(
  3.     'B' => 'A',
  4.     'D' => 'C',
  5. );
  6.  
  7. $permutations = get_all_permutations($str, $rules);
  8.  
  9. echo '<pre>' . print_r($permutations, true) . '</pre>';
Oct 19 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

17
by: Pikkel | last post by:
i'm looking for a way to replace special characters with characters without accents, cedilles, etc.
1
by: Phil Amey | last post by:
I would like to prevent certain characters from being input to some form cells, e.g. ~ # ' and so on. I'm currently using this format to check the input data of cells - if...
2
by: yearTiger2002 | last post by:
Our company's back end database server does not handle french accentuated characters well. We need to replace those characters with english letters when validating the form inputs. Any suggestions?...
9
by: Jan Danielsson | last post by:
Hello, I'd like to encode the string that outputs: Hello World! to 'Hello\x0aWorld!', and the string that outputs: Hello\World!
6
by: bobbie.matera | last post by:
I have the system setup to import a DomainList.csv file into a table called tblDmnLst. It contains a column called "NitchMarket" (datatype = text 75 character) where the user has discribed the...
7
by: Chris Brat | last post by:
Hi, Is there a better way to replace/remove characters (specifically ' and " characters in my case, but it could be anything) in strings in a list, than this example to replace 'a' with 'b': ...
8
by: Paul | last post by:
Hi, My VB is very rusty I'm afraid! What would be the most efficient way to remove the following ASCII characters from a string? à è ì ò ù À È Ì Ò Ù á é í ó ú ý Á É Í Ó Ú Ý â ê î ô û Â Ê Î Ô...
4
by: vvenk | last post by:
Hello: I have a string, "Testing_!@#$%^&*()". It may have single and double quotations as well. I would like to strip all chararcters others than a-z, A-Z, 0-9 and the comma. I came across...
12
pntkiran
by: pntkiran | last post by:
HI All. I want to write a code which replace words from string.without use of string function Example str = "This is string" remove "is" from string and replace with "was". so, finally str =...
26
by: Brad | last post by:
I'm writing a function to remove certain characters from strings. For example, I often get strings with commas... they look like this: "12,384" I'd like to take that string, remove the comma...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.