Solved

how to capitalize first letter of a word

Posted on 2006-11-30
13
839 Views
Last Modified: 2012-06-27
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
Comment
Question by:huzefaq
13 Comments
 
LVL 1

Expert Comment

by:lmedwilliams
ID: 18049638
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
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 18050090
what programming language are you using?

AW
0
 
LVL 2

Expert Comment

by:joshpowers
ID: 18050132
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Expert Comment

by:jhshukla
ID: 18050293
@joshpowers
we need to check for valid range too. don't want to convert {|} to [\], etc. do we?
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 18050710
<!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
 
LVL 18

Accepted Solution

by:
Ravi Singh earned 150 total points
ID: 18050901
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
 
LVL 2

Expert Comment

by:joshpowers
ID: 18051412
@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
 

Expert Comment

by:leroopsin
ID: 18053925
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
 

Assisted Solution

by:alisadaqain
alisadaqain earned 100 total points
ID: 18058559
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
 
LVL 7

Expert Comment

by:mquiroz
ID: 18074075
and c#

        string capitalize(string word)
        {
            return (word.Trim().Substring(0, 1) + word.Trim().Substring(1));
        }
0
 
LVL 7

Expert Comment

by:mquiroz
ID: 18074131
sorry, the best part was missing ;-P

        static string capitalize(string word)
        {
            return (word.Trim().Substring(0, 1).ToUpper() + word.Trim().Substring(1));
        }
0
 
LVL 1

Expert Comment

by:m00958
ID: 18080117
In VB you could use:

Public Function Capitalise(Value as String) as String
    Capitalise = StrConv(Value, vbProperCase)
End Function
0
 
LVL 7

Expert Comment

by:mquiroz
ID: 18097891
it's a nice collection of different languages aproach to the same problem, some kind of programmers' rosetta stone ;-)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question