Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 863
  • Last Modified:

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
0
huzefaq
Asked:
huzefaq
2 Solutions
 
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
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
jhshuklaCommented:
@joshpowers
we need to check for valid range too. don't want to convert {|} to [\], etc. do we?
0
 
HonorGodCommented:
<!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
 
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
 
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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now