Jump to content


Photo

Splitting a string then showing its alphabetical position


  • Please log in to reply
6 replies to this topic

#1 stewarty77

stewarty77
  • Members
  • Pip
  • Newbie
  • 6 posts

Posted 24 March 2006 - 03:29 PM

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?

#2 keeB

keeB
  • Staff Alumni
  • Advanced Member
  • 1,078 posts
  • LocationCalifornia

Posted 24 March 2006 - 05:12 PM

Write a function to do it. It wouldn't take long..

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

[=

Come visit my site to see my latest projects
http://nick.stinemates.org/wordpress/


#3 stewarty77

stewarty77
  • Members
  • Pip
  • Newbie
  • 6 posts

Posted 24 March 2006 - 06:22 PM

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

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

[=
[/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!

#4 keeB

keeB
  • Staff Alumni
  • Advanced Member
  • 1,078 posts
  • LocationCalifornia

Posted 24 March 2006 - 06:47 PM

<?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>";
    }

?>

Come visit my site to see my latest projects
http://nick.stinemates.org/wordpress/


#5 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 24 March 2006 - 09:18 PM

Obviously from the "Why use 1 line of code when 50 will do" school of programming - do you charge per line of code ;-);

function returnPosInAlphabet($char) {
         return ord(strtoupper($char))-64;
}

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#6 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 24 March 2006 - 09:23 PM

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 :-( )


#7 stewarty77

stewarty77
  • Members
  • Pip
  • Newbie
  • 6 posts

Posted 26 March 2006 - 05:51 PM

[!--quoteo(post=358059:date=Mar 24 2006, 10:23 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Mar 24 2006, 10:23 PM) View Post[/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!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users