Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Function for Converting Plural into Singular

Function for Converting Plural into Singular

Scheduled Pinned Locked Moved C#
help
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    KBM73
    wrote on last edited by
    #1

    Hi all, I am trying to write a function which converts plural words into singular words. Below is the function which i have created but it is not working properly for the words which ends with -se like lease, prise etc. Means when it comes to conver these words from plural to singular it is not giving proper output.It is converting "Leases" to "Leas" and "phrases" to "Phras". Please help me as soon as possible! private void _StripPlural(string strValue) { if (strValue.Length > 1) { if (strValue.EndsWith("s")) { strValue = strValue.Substring(0, strValue.Length - 1); string strPluralRule="s,z,x,ch,sh,ss"; bool blnFlag = false; char[] arrchDelimeter ={ ',' }; string[] arrstr = strPluralRule.Split(arrchDelimeter); for (int i = 0; i < arrstr.Length; i++) { string strTest = arrstr[i] + "e"; if (strValue.EndsWith(strTest) && !strValue.EndsWith("sse")) { blnFlag=true; return; } } if (blnFlag == true) { strValue = strValue.Substring(0, strValue.Length - 1); } if (strValue.EndsWith("ie")) { strValue = strValue.Substring(0, strValue.Length - 2) + "y"; } if (strValue.EndsWith("sse")) { strValue = strValue.Substring(0, strValue.Length - 1); } } } //return strValue; } Regards,

    L O 2 Replies Last reply
    0
    • K KBM73

      Hi all, I am trying to write a function which converts plural words into singular words. Below is the function which i have created but it is not working properly for the words which ends with -se like lease, prise etc. Means when it comes to conver these words from plural to singular it is not giving proper output.It is converting "Leases" to "Leas" and "phrases" to "Phras". Please help me as soon as possible! private void _StripPlural(string strValue) { if (strValue.Length > 1) { if (strValue.EndsWith("s")) { strValue = strValue.Substring(0, strValue.Length - 1); string strPluralRule="s,z,x,ch,sh,ss"; bool blnFlag = false; char[] arrchDelimeter ={ ',' }; string[] arrstr = strPluralRule.Split(arrchDelimeter); for (int i = 0; i < arrstr.Length; i++) { string strTest = arrstr[i] + "e"; if (strValue.EndsWith(strTest) && !strValue.EndsWith("sse")) { blnFlag=true; return; } } if (blnFlag == true) { strValue = strValue.Substring(0, strValue.Length - 1); } if (strValue.EndsWith("ie")) { strValue = strValue.Substring(0, strValue.Length - 2) + "y"; } if (strValue.EndsWith("sse")) { strValue = strValue.Substring(0, strValue.Length - 1); } } } //return strValue; } Regards,

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      KBM73 wrote:

      string[] arrstr = strPluralRule.Split(arrchDelimeter);

      That will eat your delimiters, how will you add it again? You will have to think of another way :)

      xacc.ide - now with TabsToSpaces support
      IronScheme - 1.0 alpha 4 out now (27 May 2008)

      L 1 Reply Last reply
      0
      • L leppie

        KBM73 wrote:

        string[] arrstr = strPluralRule.Split(arrchDelimeter);

        That will eat your delimiters, how will you add it again? You will have to think of another way :)

        xacc.ide - now with TabsToSpaces support
        IronScheme - 1.0 alpha 4 out now (27 May 2008)

        L Offline
        L Offline
        led mike
        wrote on last edited by
        #3

        leppie wrote:

        You will have to think

        :laugh::laugh: Doesn't the OP pretty much rule that out? ;P

        led mike

        1 Reply Last reply
        0
        • K KBM73

          Hi all, I am trying to write a function which converts plural words into singular words. Below is the function which i have created but it is not working properly for the words which ends with -se like lease, prise etc. Means when it comes to conver these words from plural to singular it is not giving proper output.It is converting "Leases" to "Leas" and "phrases" to "Phras". Please help me as soon as possible! private void _StripPlural(string strValue) { if (strValue.Length > 1) { if (strValue.EndsWith("s")) { strValue = strValue.Substring(0, strValue.Length - 1); string strPluralRule="s,z,x,ch,sh,ss"; bool blnFlag = false; char[] arrchDelimeter ={ ',' }; string[] arrstr = strPluralRule.Split(arrchDelimeter); for (int i = 0; i < arrstr.Length; i++) { string strTest = arrstr[i] + "e"; if (strValue.EndsWith(strTest) && !strValue.EndsWith("sse")) { blnFlag=true; return; } } if (blnFlag == true) { strValue = strValue.Substring(0, strValue.Length - 1); } if (strValue.EndsWith("ie")) { strValue = strValue.Substring(0, strValue.Length - 2) + "y"; } if (strValue.EndsWith("sse")) { strValue = strValue.Substring(0, strValue.Length - 1); } } } //return strValue; } Regards,

          O Offline
          O Offline
          Old Nic
          wrote on last edited by
          #4

          See a java solution here using regular expressions http://snippets.dzone.com/posts/show/4110[^] This is a simple port to C# using the System.Text.RegularExpressions namespace.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups