how to capitalize first letter of a word

Hi guys I need a method to which if I ass a word it caitalize the first letter and send it back

for e.g. if I send alabama it sends me Alabama

Thanks
huzefaqAsked:
Who is Participating?
 
Ravi SinghSenior Software EngineerCommented:
Based on your PAQ's -- a Java method that returns the new string:

    public String capitalizeFirstLetter(String input)
    {
        if (input.trim().equals("")) return "";
        else return input.substring(0, 1).toUpperCase() + input.substring(1, input.length());
    }
0
 
lmedwilliamsCommented:
Try this, you can add trim() to clean up empty spaces, but this should do what you like.

//---------------------------------------------------------------------------
#include <string>
#include <iostream>
#pragma hdrstop
using namespace std;
//---------------------------------------------------------------------------

#pragma argsused

string CapitalizeFirst(string inStr)
{
  inStr[0]=toupper(inStr[0]);
  return(inStr);
}

int main(int argc, char* argv[])
{
  string outStr="alabama";
  cout << "Original Word=" << outStr << endl;
  outStr = CapitalizeFirst(outStr);
  cout << "Finished Word=" << outStr << endl;
  system("PAUSE");
  return 0;

}
//---------------------------------------------------------------------------

0
 
Arthur_WoodCommented:
what programming language are you using?

AW
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
joshpowersCommented:
what language would you like it in?

in c++ it would be something like

void TitleCase(char* word)
{
     char[0] &= 0xDF;
}

That should turn any lowercase letter into a captial letter and leave captial letters alone.  The same principle applies to other languages.
0
 
jhshuklaCommented:
@joshpowers
we need to check for valid range too. don't want to convert {|} to [\], etc. do we?
0
 
HonorGodSoftware EngineerCommented:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> Leading Cap </title>
<script type='text/javascript'>  
  function cap() {
    return arguments[ 0 ].toUpperCase();
  }

  function doit( obj ) {
    obj.value = obj.value.replace( /\b([a-z])/, cap );
  }
</script>  
</head>
<body>
  <input type='text' onchange='doit(this);return false;' />
</body>
</html>
0
 
joshpowersCommented:
@jhshukla

Clearly some checking of your input is often nessisary when programming.  However the question was how do I turn a lower case letter into an uppercase letter.  My solution is a one liner that will work in nearly any language without many modifications.
Anyways the main point is that if you dont know what your variables ranges are then check them, if you have already checked them at other points then dont.  It is all about managing the data.
0
 
leroopsinCommented:
Dear All,

If you want to do in VBScript, you give you a function in VBScript.

Function TitleCase(DaString)
    if not DaString = "" or IsNull(DaString) then
        dim DaTempString, DaArray, ConVStr
        DaString = lcase(DaString)
        DaArray = Split(DaString," ")
        For i = 0 to ubound(DaArray)
        ConVStr = UCase(Left(DaArray(i),1)) + mid(DaArray(i),2)
        DaTempString = DaTempString & " " & ConVStr
        next
    else
        DaTempString = "Nothing to Convert"
    end if
    TitleCase = DaTempString
End Function

Best regards,

Sin Le
0
 
alisadaqainCommented:
I guess Java is missing so here it is.

package whatever;

public class FirstCharChangeCase {

      public static void main(String[] args) {
            
            String Apple = "alabama";
            String First = null;
            String Rest = null;
            String appleAfter = null;
            
            //Break up string
            First = Apple.substring(0,1);
            Rest = Apple.substring(1);
            
            //Change First to UPPER
            First = First.toUpperCase();
            
            //Final Product
            appleAfter = First+Rest;
            
            //Test Print
            System.out.println(Apple);
            System.out.println(First);
            System.out.println(Rest);
            System.out.println(appleAfter);
      }

}

FYI...Used Java 1.6
0
 
mquirozCommented:
and c#

        string capitalize(string word)
        {
            return (word.Trim().Substring(0, 1) + word.Trim().Substring(1));
        }
0
 
mquirozCommented:
sorry, the best part was missing ;-P

        static string capitalize(string word)
        {
            return (word.Trim().Substring(0, 1).ToUpper() + word.Trim().Substring(1));
        }
0
 
m00958Commented:
In VB you could use:

Public Function Capitalise(Value as String) as String
    Capitalise = StrConv(Value, vbProperCase)
End Function
0
 
mquirozCommented:
it's a nice collection of different languages aproach to the same problem, some kind of programmers' rosetta stone ;-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.