Home AMX User Forum AMX General Discussion

Capitalize 1st Char/s

I know there are functions floating around for this but I never seem to find them when I want them and sometimes it's quicker and usually more fun just to write functions from scratch. Since posting my version of functions can sometimes start a healthy debate and I might learn something I otherwise wouldn't have, here are the two functions. I thought of combining them into one and pass in another param to select the output but decided against that for now.
DEFINE_FUNCTION CHAR[MAX_512]fnSet_1stChar_Capital(CHAR iStr[])//changes by reference and return, original can be preserved if line commented out then no change by ref.

     {
     STACK_VAR CHAR cStrCopy[MAX_512];
               
     cStrCopy = iStr;
     if(cStrCopy[1] > 96 && cStrCopy[1] < 123)
      {
      cStrCopy[1] = cStrCopy[1]-32;
      }
     iStr = cStrCopy;//comment this out to maintain original and then no change by ref
     RETURN cStrCopy;
     }
     
DEFINE_FUNCTION CHAR[MAX_8192]fnSetAll_1stChars_Capital(CHAR iStr[])//changes by reference and return, original can be preserved if line commented out then no change by ref.

     {
     STACK_VAR CHAR cStrCopy[MAX_8192];
     STACK_VAR INTEGER nFBS;
               
     cStrCopy = iStr;
     if(cStrCopy[1] > 96 && cStrCopy[1] < 123)
      {
      cStrCopy[1] = cStrCopy[1]-32;
      }
     nFBS = find_string(cStrCopy,' ',1);
     WHILE(nFBS)
      {
      if(cStrCopy[nFBS+1] > 96 && cStrCopy[nFBS+1] < 123)
           {
           cStrCopy[nFBS+1] = cStrCopy[nFBS+1]-32;
           }
      nFBS = find_string(cStrCopy,' ',nFBS+1);  
      }
     iStr = cStrCopy;//comment this out to maintain original and then no change by ref
     RETURN cStrCopy;
     }

Comments

  • GregGGregG Posts: 251
    I have a set of functions for this that are mostly used to chew up data from web sources and regurgitate it into a nice format for touchpanel display.

    First the incoming string goes through ReplaceCRLF() and then StripExtraSpaces() (Shown at the end), then it gets sent to one of the capitalizing routines depending on the type of data.

    These functions will force non-leading letters to be lowercase, since they just create a merge of the upper_string() and lower_string() versions of the incoming data.
    /////////////////////////////////////////////////////////////
    // Char[16384] CapitalizeFirstLetters(Char cString[])
    // Takes a normal word string, separated by single spaces,
    // and capitalizes the first letter of every word. Best practice is
    // to run the string through StripExtraSpaces() first, so the word
    // boundaries line up as expected.
    //
    // eg- CapitalizeFirstLetters('hello world') gives 'Hello World'
    //
    /////////////////////////////////////////////////////////////
    Define_Function Char[16384] CapitalizeFirstLetters(Char cString[])
    {
    Stack_Var Char cOutput[16384]
    Stack_Var Char cAllCaps[16384]
    Stack_Var Integer j
    
        cAllCaps = Upper_String(cString)
        cOutput = Lower_String(cString)
    
        If(cOutput[1] <> ' ')
        {
            cOutput[1] = cAllCaps[1]
            j = 2
        }
        Else
        {
            j = 1
        }
    
        While(Find_String(cOutput,' ',j))
        {
            j = Find_String(cOutput,' ',j)+1
            cOutput[j] = cAllCaps[j]
        }
        Return cOutput
    }
    
    /////////////////////////////////////////////////////////////
    // Char[16384] CapitalizeSentences(Char cString[])
    // Takes a string of normal sentences, separated by ". "
    // and capitalizes the first letter of the first word in each sentence.
    //
    // Expects single spaces after periods. I know it's non-standard,
    // but it looks fine on the panels and works with the output
    // from StripExtraSpaces()
    //
    // eg- CapitalizeSentences('hello out there. world.') gives 'Hello out there. World.'
    //
    /////////////////////////////////////////////////////////////
    Define_Function Char[16384] CapitalizeSentences(Char cString[])
    {
    Stack_Var Char cOutput[16384]
    Stack_Var Char cAllCaps[16384]
    Stack_Var Integer j
    
        cAllCaps = Upper_String(cString)
        cOutput = Lower_String(cString)
    
        If(cOutput[1] <> ' ')
        {
            cOutput[1] = cAllCaps[1]
            j = 2
        }
        Else
        {
            j = 1
        }
    
        While(Find_String(cOutput,'. ',j))
        {
            j = Find_String(cOutput,'. ',j)+2
            cOutput[j] = cAllCaps[j]
            j++
        }
        Return cOutput
    }
    /////////////////////////////////////////////////////////////
    // ReplaceCRLF(Char cString[])
    // takes a long string and replaces any and all ascii 13 or 10 values with spaces
    //
    // StripCRLF("'Line of text.',13,10,'New line of text.',13,10") gives 'Line of text.  New line of text.'
    //
    /////////////////////////////////////////////////////////////
    Define_Function Char[16384] ReplaceCRLF(Char cString[])
    {
    Stack_Var j
    Stack_Var Char cOutput[16384]
    Stack_Var Char t;
    
        t = GET_BUFFER_CHAR(cString)
        While(t)
        {
            If((t<>10) and (t<>13))
                cOutput = "cOutput,t"
            Else
                cOutput = "cOutput,' '"
            t = GET_BUFFER_CHAR(cString)
        }
        Return cOutput
    }
    
    /////////////////////////////////////////////////////////////
    // StripExtraSpaces(Char cIn[])
    // Removes adjacent, multiple space " " characters from an input string
    //
    // eg- StripExtraSpaces('Why   does that happen   ') returns 'Why does that happen '
    //
    /////////////////////////////////////////////////////////////
    Define_Function Char[16384] StripExtraSpaces(Char cIn[]) // Removes adjacent multiple spaces
    {
    Stack_Var Char cTemp[16384]
    Stack_Var Char cOut[16384]
        cTemp = cIn
        While(Length_String(cTemp))
        {
            If( !((cTemp[1] = ' ') and (Right_String(cOut,1) = "' '")) )
                cOut = "cOut,cTemp[1]"
            Get_Buffer_Char(cTemp)
        }
        Return cOut
    }
    
    /////////////////////////////////////////////////////////////
    // Char[66][300] BreakLines(Char cString[], Integer nMaxLineLen, Integer nSpacesOnly)
    // takes a long string and breaks it into an array of lines with
    // a max length of nMaxLineLen in any one line.
    //
    // eg- BreakLines('You have to go to the park and play now.',20)
    // returns { {'You have to go to'}, {'the park and play'}, {'now.'} }
    //
    // Breaks will happen on spaces (prefered) or punctuation ',.?/;:[]{}()-',
    // if any exists within the limit length, otherwise a forced hyphenation will occur
    // at the exact limit point and it may not be an even remotely proper location
    // for one, since we don't have any dictionary refences here.
    //
    // If nSpacesOnly is non-zero, the function will only accept spaces to break lines on.
    //
    /////////////////////////////////////////////////////////////
    Define_Function Char[66][300] BreakLines(Char cString[], Integer nMaxLineLen, Integer nSpacesOnly)
    
  • a_riot42a_riot42 Posts: 1,624
    Why have a special function to remove CRLF? Why not just make a replaceChar function and pass in what you want replaced and/or with what?
    Paul
  • GregGGregG Posts: 251
    I have the generic version also, it's more of a semantic convenience, so the code is easier to read with less need for comments. Having an extra unused function with a slightly different name doesn't hurt performance in any way that I have noticed.
Sign In or Register to comment.