Jump to content
Updating IPB tonight Read more... ×

Archived

This topic is now archived and is closed to further replies.

stewarty77

Splitting a string then showing its alphabetical position

Recommended Posts

Hi

Say if the user was to input a string "hello". I need php to print the string by letter and give its numerical position in the alphabet:

h = 8
e = 5
l = 12
l = 12
o = 15

Any suggestions?

Share this post


Link to post
Share on other sites
Write a function to do it. It wouldn't take long..

[code]function returnPosInAlphabet($char) {
    switch ($char) {
        case 'a':
            return 1;
        case 'b':
            return 2;
    }
}[/code]

[=

Share this post


Link to post
Share on other sites
[!--quoteo(post=357975:date=Mar 24 2006, 05:12 PM:name=keeB)--][div class=\'quotetop\']QUOTE(keeB @ Mar 24 2006, 05:12 PM) [snapback]357975[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Write a function to do it. It wouldn't take long..

[code]function returnPosInAlphabet($char) {
    switch ($char) {
        case 'a':
            return 1;
        case 'b':
            return 2;
    }
}[/code]

[=
[/quote]

Ok got the function created

function returnPosInAlphabet($char) {

switch ($char) {
case 'A':
return 1;
case 'B':
return 2;
case 'C':
return 3;
case 'D':
return 4;
case 'E':
return 5;
case 'F':
return 6;
case 'G':
return 7;
case 'H':
return 8;
case 'I':
return 9;
case 'J':
return 10;
case 'K':
return 11;
case 'L':
return 12;
case 'M':
return 13;
case 'N':
return 14;
case 'O':
return 15;
case 'P':
return 16;
case 'Q':
return 17;
case 'R':
return 18;
case 'S':
return 19;
case 'T':
return 20;
case 'U':
return 21;
case 'V':
return 22;
case 'W':
return 23;
case 'X':
return 24;
case 'Y':
return 25;
case 'Z':
return 26;
}
}

I'm using str_split("string entered") to split the string entered but it returns Array, anymore suggestions what to do next, oh and I'm not to good at the programming as you might have guessed!

Share this post


Link to post
Share on other sites
[code]<?php
function returnPosInAlphabet($char) {
    switch ($char) {
    case 'A':
        return 1;
    case 'B':
        return 2;
    case 'C':
        return 3;
    case 'D':
        return 4;
    case 'E':
        return 5;
    case 'F':
        return 6;
    case 'G':
        return 7;
    case 'H':
        return 8;
    case 'I':
        return 9;
    case 'J':
        return 10;
    case 'K':
        return 11;
    case 'L':
        return 12;
    case 'M':
        return 13;
    case 'N':
        return 14;
    case 'O':
        return 15;
    case 'P':
        return 16;
    case 'Q':
        return 17;
    case 'R':
        return 18;
    case 'S':
        return 19;
    case 'T':
        return 20;
    case 'U':
        return 21;
    case 'V':
        return 22;
    case 'W':
        return 23;
    case 'X':
        return 24;
    case 'Y':
        return 25;
    case 'Z':
        return 26;
    default:
        return "space";
    }
}

$str = "Hello Friend";

$hi = str_split($str);

    for ($x = 0; $x <= count($hi); $x++) {
        print "' $hi[$x] ' = ";
        print returnPosInAlphabet(strtoupper($hi[$x]));
        print "<br>";
    }

?>[/code]

Share this post


Link to post
Share on other sites
Obviously from the "Why use 1 line of code when 50 will do" school of programming - do you charge per line of code ;-);

[code]function returnPosInAlphabet($char) {
         return ord(strtoupper($char))-64;
}[/code]

Share this post


Link to post
Share on other sites
I have been trying to post a similar solution for the past 2 hours but keep getting a "406 error -- Not Acceptible" error. :-(

Ken

(since this post worked, let's see if editing the post to add code will work)
(nope :-( )

Share this post


Link to post
Share on other sites
[!--quoteo(post=358059:date=Mar 24 2006, 10:23 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Mar 24 2006, 10:23 PM) [snapback]358059[/snapback][/div][div class=\'quotemain\'][!--quotec--]
I have been trying to post a similar solution for the past 2 hours but keep getting a "406 error -- Not Acceptible" error. :-(

Ken

(since this post worked, let's see if editing the post to add code will work)
(nope :-( )
[/quote]

Thanks your help with this matter is greatly appreciated!

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.