Jump to content

Splitting a string then showing its alphabetical position


stewarty77

Recommended Posts

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]

[=
Link to comment
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!
Link to comment
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]
Link to comment
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!
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.